Using cin in a for or while loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phenix151

    Using cin in a for or while loop

    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
  • John Harrison

    #2
    Re: Using cin in a for or while loop


    "phenix151" <mcrudd@netzero .net> wrote in message
    news:6d65dccd.0 309030459.357e7 cb9@posting.goo gle.com...[color=blue]
    > I am tring to read numbers from the command line. they are to be input[/color]
    like this:[color=blue]
    >
    > 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[/color]
    the data.[color=blue]
    >
    > 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[/color]

    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


    Comment

    Working...