Using cin in a for or while loop

P

phenix151

I am tring to read numbers from the command line. they are to be input like this:

12.34 14.67 82 227 -17

I know how many numbers are to be input by asking.

I want to put this in a for or a do/while loop, but it never reads all of the data.

here is the code snippit:

cout<<"enter the data"<<endl;

for (i =0; i <= intuserInput; i++)
{
cin>>dblVar1; //reads the line one at a time
if(i==0)
{dblLgst = dblVar1; // the first number read is the largest
}
if(dblVar1 == dblLgst) //check for duplicates
dup++;
if(dblVar1 > dblLgst) //
dblLgst = dblVar1;
}

Thanks
Mike
 
J

John Harrison

phenix151 said:
I am tring to read numbers from the command line. they are to be input like this:

12.34 14.67 82 227 -17

I know how many numbers are to be input by asking.

I want to put this in a for or a do/while loop, but it never reads all of the data.

here is the code snippit:

cout<<"enter the data"<<endl;

for (i =0; i <= intuserInput; i++)
{
cin>>dblVar1; //reads the line one at a time
if(i==0)
{dblLgst = dblVar1; // the first number read is the largest
}
if(dblVar1 == dblLgst) //check for duplicates
dup++;
if(dblVar1 > dblLgst) //
dblLgst = dblVar1;
}

Thanks
Mike

What do you mean it never reads all the data? How do you know? What happens
that is different from what you expect? Give us a hand here.

The one thing that strikes me about you code though is the for loop. Don't
you really mean

for (i =0; i < intuserInput; i++)

that would read intuserInput numbers, your loop reads intuserInput + 1
numbers.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top