string input problem

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
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');
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
The default delimiter is already '\n'.

http://www.cplusplus.com/reference/istream/istream/getline/

cplusplus_com wrote:
The delimiting character is the newline character ('\n') for the first form
> For T=3, 4 is the same
Can you describe your problem better?
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.)
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
closed account (48T7M4Gy)
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
@kemort
My example is complete.
closed account (48T7M4Gy)
My example is complete.

OK, post it when you're ready so we can have a look too.
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
closed account (48T7M4Gy)
And T, ws & str?
> And T, ws & str?
What do you mean, @kemort?
closed account (48T7M4Gy)
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
@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.
Now I know what you mean. This line should be :
cout << "String you entered : " << endl;

:D
closed account (48T7M4Gy)
Yep it's complete. Well done.
Topic archived. No new replies allowed.