0

I'm using Visual C++ 6.0 and currently created a program that will print the output stored in string.

The problem is when I entered words with space, only the first word is visible in the output.

Example:

Enter your address: new york
new
Press any key to continue

I want this:

Enter your address: new york
new york
Press any key to continue

Also, I tried to use getline but when I entered words, It will first print blank space then stored the last output before the current one.

Here's my code:

#include <iostream>
#include <string>

using namespace std;

void main()
{
  string address1;
  cout<<"Enter your address:";
  cin>> address1;
  // getline(cin, address1); code when using getline
  cout<<address1<<"\n";
}
iqstatic
  • 2,322
  • 3
  • 21
  • 39

4 Answers4

1

Use std::getline (std::cin, address1);, not cin. Because cin takes space as delimiter.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • yes, it displays the whole words, but the problem is that when I enter "new york", it will first print extra space below then enter again to see the results. It always need to double click the enter before it shows the result. –  Aug 28 '14 at 00:11
1

What about this one? Kind of getline concept, presuming newline character is '\n', change as required according to your platform, unix or windows etc

int main()
{
  string addrpart, address1;
  cout<<"Enter your address:";
  cin>> addrpart;
  while (addrpart != "x") {
    address1 += addrpart + " ";
    addrpart = "x";
    cin>> addrpart;
  }
  cout<<address1<<"\n";
}
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • yes, it displays the whole words, but the problem is that when I enter "new york", it will first print extra space below then enter again to see the results. It always need to double click the enter before it shows the result. –  Aug 28 '14 at 00:12
1

Here you go

#include <iostream>
#include <string>

using namespace std;

int main( int argc, char** argv )
{
  string userinput;

  cout << "Enter your address:";
  getline ( cin, userinput );
  cout << userinput;

  return 0;
}

$ g++ a.cpp -o app
$ ./app
Enter your address:new york
new york
Vimal
  • 436
  • 1
  • 4
  • 14
  • yes, it displays the whole words, but the problem is that when I enter "new york", it will first print extra space below then enter again to see the results. It always need to double click the enter before it shows the result. –  Aug 28 '14 at 00:14
1

you are doing it correct, but the main problem is that you are using cin while you should avoid it and use getline(cin,address1) because cin will only take a single word and it will not take anyhting which you type after space. On the other hand getline(cin,address1) can take a complete sentence along with spaces

Read the comments and use this code

#include <iostream>
#include <string>

using namespace std;

int main()//using int main()
{

  string address1;

  cout<<"Enter your address:";

  //cin>> address1; Don't use it

  getline(cin, address1);//use this

  cout<<address1<<"\n";

  return 0;//returning an integer 0
}
Taimour
  • 459
  • 5
  • 21
  • yes, it displays the whole words, but the problem is that when I enter "new york", it will first print extra space below then enter again to see the results. It always need to double click the enter before it shows the result. –  Aug 28 '14 at 00:12
  • Copy my code in your compiler and run it. After typing "new york" you should press enter once and "new york" will be printed in the next line. If problem continues then try it on a different compiler. – Taimour Aug 28 '14 at 05:29