Exit loop unless input not given

  • A+
Category:Languages
int main() { string name; while(cin>>name) {     if(name=='/n')        break;     else     {         cout<<name;     } } cout<<"Exited"; } 

Here I need to get input till the user didn't give input or skips with the new line. I am unable to complete the code.


Thisngs to watch:

  1. Lets add the correct headers.
  2. Lets not do the using namespace std; as it causes problems.
  3. Let use getline() rather than operator>> so we can see a whole line
  4. Lets use std::string rather than a C-array of char.
  5. Can check for empty line as part of the while test.
  6. Lets add '/n' so we can see the output.
  7. Lets do some nice formatting.

Result:

#include <iostream> #include <string> int main() {     std::string name;     while(std::getline(std::cin, name) && name != "")     {         std::cout << name << "/n";     }     std::cout << "Exited/n"; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: