|
-
October 29th, 2003, 08:49 PM
#1
simple console i/o question
Given these requirements for a console application:
Enter a series of integers seperated by whitespace. The first integer represents the quantity of integers to follow.
Ex. 3 5 18 21
or. 5 17 -46 3 198 2
I have been attempting the following to read the integers and store them in a vector:
scanf("%d", &cnt);
for(int i = 1; i <= cnt + 1; i++)
{
scanf("%d", &n);
aInts.push_back(n);
}
This works fine, unless someone enters fewer integers than the initial integer indicates. (ex. 3 19 5)
How can I test to see if the end of the input stream has been reached?
I have attempted the same type of logic using cin, to no avail.
Thank you in advance for your help.
-
October 29th, 2003, 09:26 PM
#2
Roger - your question contains a paradox.
A program that accepts input must wait for the user to take action. In the case of text, the program needs to know when the user's input is complete.
If the user can enter a variable number of arguements, then why have them specify the number of arguements to begin with? Instead, have the user type something to indicate they are done typing.
This could be a period, semi-colon, special keyword, or a blank line.
Ex: 5 17 -46 3 198 2 ;
-rick
-
October 29th, 2003, 11:35 PM
#3
You can use getline and use " " as a delimiter to seperate each into a part of a vector
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|