2

Sorry, this code seems silly but I just want to get the logic.

I am trying to end the program when the user enters quit as an input.

I wrote name != "quit" && sname != "quit" as the while condition, so if any of them is quit, the loop would end (right?).

But when I write quit as name, it still waits for sname input, if I write quit also for the sname input, then it finally ends. But shouldn't it end when it see quit for the very first time since I wrote the conditions with &&?

In addition, I get the same output when I wrote the condition with ||. Why is this so?

 int main() { string name="",sname=""; cout << "enter your name and surname \n"; cout << ">>name="; getline(cin,name); cout << ">>surname="; getline(cin,sname); while (name != "quit" && sname != "quit") { cout << ">>name="; getline(cin,name); cout << ">>surname="; getline(cin,sname); } return 0; } 

Output:

enter your name and surname >>name=John >>surname=Wick >>name=quit >>surname= 

4 Answers 4

5

Good question! This has to do with how while loops work.

In C++, a while loop works as follows:

  • First, it checks if the condition in the parenthesized statement in the while loop is true.
  • If so, it executes the entire body of the while loop, then jumps back to that first step.

In other words, a while loop won't stop running as soon as the condition is false. Rather, it stops running once it gets to the point where it reevaluates the condition and finds that it's false.

This is why it doesn't matter whether you're using && or || here. The loop won't exit until the condition gets checked again, and that won't happen until the current loop iteration finishes running.

This leaves you with a few options about what to do.

  1. You could add an explicit check into the while loop after reading the first string to confirm that the string isn't "quit", only executing the rest of the loop if you need to.

  2. You could add a break statement into the middle of the loop to tell the loop to stop running. This might be done in combination with option (1).

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that helped a lot and I fixed the code. But I have one more confusion. do-while guarantees at least one execution of the statement, even if the condition is false. And in this case, while is also doing the execution until it reevaluates the condition, so again one execution is completed even if the condition was false in the first place. So how are they different practically?
2

In the while loop, it's doing a check every time after inputting both first name and surname, that means it will only check after you enter the surname as well. You can do this:

while (true) { getline(cin, name); if (name == "quit") break; getline(cin, sname); if (sname == "quit") break; } 

something like that. :)

Comments

1

Like as doc say:

istream& getline (istream& is, string& str, char delim); istream& getline (istream& is, string& str); 
  1. Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).

  2. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.

  3. If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).

  4. Note that any content in str before the call is replaced by the newly extracted sequence.

So:

Use if conditions for break the loop at any time. Also specify end character in getline (is optional but write it). At the end, trim the string:

#include <boost/algorithm/string.hpp> int main() { string name = "", sname = ""; cout << "enter your name and surname \n"; while (true) { cout << "name: "; getline(cin, name, "\n"); name = trim(name); if (name == "quit") break; cout << "surname: "; getline(cin, sname, "\n"); sname = trim(sname); if (sname == "quit") break; }; return 0; } 

Comments

0

Because when you enter the name = "quit" that last time while loop runs. And all the statements inside the while loop executes. so The surname prints.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.