Jul 23, 2016 at 8:56am UTC
Hi!
when I execute this and entered these inputs: 2
Hello
world
it doesn't get the second string and execution stoped
why?????????/
you know when i press enter to give second sring it stopped
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include<iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
int T=0;
char str[10000];
cin>>T;
for (int i=1;i<=T;i++)
{
cin.getline(str,10000);
cout<<str;
}
getch();
return 0;
}
Last edited on Jul 23, 2016 at 9:03am UTC
Jul 23, 2016 at 9:03am UTC
Can you let us know your program output when T is 3 or when T is 4?
This line :
cin.getline(str, 10000);
Should be :
cin.getline(str, 10000, '\n' );
Jul 23, 2016 at 9:10am UTC
for T=3,4 is the same
i include cin.getline(str, 10000, '\n' );
now it goes to next line but it doesn't get next
Jul 23, 2016 at 9:23am UTC
> For T=3, 4 is the same
Can you describe your problem better?
Jul 23, 2016 at 9:28am UTC
Try it with T= 10. Let us know how many strings you have entered.
(You should see if your Enter keyboard key is at fault.)
Jul 23, 2016 at 9:39am UTC
Try this :
1 2 3 4 5 6 7 8
cin >> T;
cin >> ws; // Tells the program to eats all remaining whitespaces
for (int i = 0; i < T; i++)
{
cout << "Enter a string : " ;
cin.getline(str, 10000, '\n' );
cout << str << endl;
}
And let us know your program output when T is 10 or T is 15.
Last edited on Jul 23, 2016 at 11:18am UTC
Jul 23, 2016 at 10:20am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include<iostream>
#include <string>
using namespace std;
int main()
{
int T = 0;
char str[10000];
cin >> T;
cin.ignore(); // <--
for (int i=1;i<=T;i++)
{
cout << i << ": " ;
cin.getline(str,10000);
cout<< str << endl;
}
return 0;
}
2
1: first line
first line
2: second line of text
second line of text
Exit code: 0 (normal program termination)
Last edited on Jul 23, 2016 at 10:21am UTC
Jul 23, 2016 at 10:26am UTC
@kemort
My example is complete.
Jul 23, 2016 at 10:38am UTC
Try this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter the number of strings you enter : " ;
cin >> T;
cin >> ws;
for (int i = 0; i < T; i++)
{
cout << "Enter a string : " ;
cin.getline(str, 10000, '\n' );
cout << "String you entered : " << str << endl;
}
return 0;
}
Last edited on Jul 23, 2016 at 11:14am UTC
Jul 23, 2016 at 10:43am UTC
> And T, ws & str?
What do you mean, @kemort?
Jul 23, 2016 at 10:45am UTC
Well, @ca etc, you said 'try this' and I did, but this time your code is incomplete. Why not just show the one you said was complete? But there again, showing a new one is OK even if it's not complete. :)
Make that T and str.
Last edited on Jul 23, 2016 at 11:10am UTC
Jul 23, 2016 at 11:12am UTC
@kemort
> Why not just show the one you said was complete?
The example of mine I've just shown is already complete. Nothing more can be added to the example.
Jul 23, 2016 at 11:16am UTC
Now I know what you mean. This line should be :
cout << "String you entered : " << endl ;
:D
Jul 23, 2016 at 11:18am UTC
Yep it's complete. Well done.