Read multiple data for one declared variable

This is a discussion on Read multiple data for one declared variable within the C++ Programming forums, part of the General Programming Boards category; How can I read multiple datas from outside text file that have the same data type by declaring only one ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Unhappy Read multiple data for one declared variable

    How can I read multiple datas from outside text file that have the same data type by declaring only one variable?
    For example:
    numbers below are scores and they are double, the negative number is used a "break" jump to the next record

    10 76 50 -1
    63 13 24 70 -1
    -44
    94 70 80 -3

    Here is my code:
    Code:
    double score=double();
    while (score>0)
    {
    in score; 
    }
    cout << score << endl;
    The problem is that each record has different number of score and I can't find a way to get each score.
    Note that I used "in" instead of "cin" cuz I read outside data file and then view it on the screen....
    Any advice? ....

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You still need two loops. The outer loop reads a line at a time. The inner loop reads until it finds a negative number.

    Your current code has a typo (a missing >>). Otherwise it will read in the values and do nothing with them for a single line. You need to put all that code in another loop to read each line, and you need to do something with each score.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It will do absolutely nothing at all. You initialize score to 0, then run a loop while it is greater than 0. Obviously, the loop won't ever be entered.

    You could do this instead:
    Code:
    while((in >> score) && (score > 0)) {
      // do something with score
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    77
    The example loop I gave you in the other one should have worked just fine, as long as you took the "c"s out of the "cin"s. But they're all correct. You need two loops.

    Edit: And again, you can't have more than one value for a single variable unless you plan on printing it immediately after it is set. The way you're trying to set it up, you need to have an array, not a single variable.
    Last edited by Molokai; 11-04-2007 at 01:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  4. data read zeros?
    By Question in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2003, 12:15 PM
  5. Client-Server Data Read Problem
    By bob2509 in forum C Programming
    Replies: 8
    Last Post: 11-06-2002, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21