0
#include<iostream>
using namespace std;
int main()
{
string p;
int n,i;
cin>>n;
for(i=1;i<=n;i++)
{
    cin>>p;
    cout<<p<<"\n";
}
return 0;
}

hiii.. i wanna take two strings and then print them one by one as in prog. but when i take n=2 and input the string "I wanna go"
it gives the output :

i
wanna

and it didn't ask me for second string.it is taking the string until it gets a whitespace.what should i do to resolve this?

KAPIL
  • 3
  • 1
  • Use [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline). – Chnossos Apr 15 '14 at 19:02
  • 1
    The for loop will repeat twice. std::cin reads input until the white space. If you want the entire line, use std::getline. – Ben Apr 15 '14 at 19:03
  • http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces Check the std::getline answer over there. – M2tM Apr 15 '14 at 19:04
  • A related, more generalized Q&A article about input of whitespace separated strings: [**Why does reading a struct record fields from file fail, and how can I fix it?**](http://stackoverflow.com/questions/23047052/why-does-reading-a-struct-record-fields-from-file-fail-and-how-can-i-fix-it) – πάντα ῥεῖ Apr 15 '14 at 19:23

3 Answers3

2

You have to change the initial value of your iteration variable i in you for statement to the following one:

for(i=0;i<=n;i++)
Christos
  • 53,228
  • 8
  • 76
  • 108
  • u r right but i m not getting why to do so...will u please tell me the reason? – KAPIL Apr 15 '14 at 19:13
  • @user3537331 sure thing. Give me please a few minutes. – Christos Apr 15 '14 at 19:17
  • 1
    Your loop runs WHILE it is LESS THAN or EQUAL TO n. Your loop runs once where i = 1, and again where i = 2. Once i = 3, the loop stops running. Changing i to initially = 0 means it runs thrice. Once where i = 0, once where i = 1, and once again where i = 2. – Ben Apr 15 '14 at 19:17
  • @user3537331 I din't make it to answer you before Ben. Ben explains as I would. Thanks Ben ! – Christos Apr 15 '14 at 19:19
  • ya sir u r absolutely right. but when i take n=2, for loop should run twice.. one for i=1, and then for i=2 as 2<=2 is true so it should take 2 strings but it is taking just one and then terminates...... why so? – KAPIL Apr 15 '14 at 19:23
  • @user3537331 What happens there is that actually when you write for instance I wanna go it and the press enter considers it as three strings and for that reason it prints one word after the other in a newline and terminates. These are three words 1) I, 2) wanna and 3) go. So if you start with i=1 and i<=2 and you type I wanna go and then enter it will print you only the first two words in the string. You can check it actually this if you insert one word then enter then insert the other then enter and so on. If it doesn't make sense this please let me know what is ambiguous. Thanks – Christos Apr 15 '14 at 19:31
  • ohh ... i think u got it wrong...it is taking only one string when i have used std::string p ... etc. and the problem of whitespaces has been solved... i will also search for this... – KAPIL Apr 15 '14 at 21:41
1

Consider using std::getline.

std::string name;
std::getline(std::cin, name);

The above example is summarized from: std::cin input with spaces?

Community
  • 1
  • 1
M2tM
  • 4,415
  • 1
  • 34
  • 43
  • thanx.. its working but when i m taking n=4 it is taking only 3 strings why so? – KAPIL Apr 15 '14 at 19:11
  • try this `for(int n=0; n<4; n++){ std::string s; std::getline(std::cin, s); std::cout < s << std::endl; }` – Quest Apr 15 '14 at 19:34
0

Instead of operator >> you should use function std::getline. For example

#include <iostream>
#include <limits>

int main()
{
   int n;

   std::cin >> n;

   std::cin.ignore( std::numeric_limits<std::streamsize>::max() );
   // or simply std::cin.ignore();

   for ( int i = 1; i <= n; i++ )
   {
      std::string p;
      std::getline( std::cin, p );
      std::cout << p << "\n";
   }

   return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335