c++: input unknown amount of int from file

Discussion in 'Web Design & Coding' started by dubstar, Apr 22, 2007.

  1. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    i'm pulling information from a file -- in this order -- first name, last name, scores (different amount of scores for each line).

    example:
    john doe 44 100 80 33 -1
    jane johnson 33 -4
    hooka smoker -9
    etc etc 0 44 99 -44

    the negative score is supposed to tell the code to stop, read it, but stop that line and move to the next line.

    problem code:
    Code:
    [SIZE=2]in >> score;[/SIZE]
    [SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] (score >= 0)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]cout << score;[/SIZE]
    [SIZE=2]}[/SIZE]
    
    or

    Code:
    [SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i=0;i<10;i++)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]in >> score[i + 0];[/SIZE]
    [SIZE=2]cout << score[i + 0];[/SIZE]
    [SIZE=2]}[/SIZE]
    
    here is the whole function code:
    Code:
    [SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] ReadRecord()[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]ifstream in; [/SIZE][SIZE=2][COLOR=#008000]// input file[/COLOR][/SIZE]
    [SIZE=2]string fname; [/SIZE][SIZE=2][COLOR=#008000]// first name[/COLOR][/SIZE]
    [SIZE=2]string lname; [/SIZE][SIZE=2][COLOR=#008000]// last name[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] score;[/SIZE]
    [SIZE=2][COLOR=#008000]//OPEN FILES[/COLOR][/SIZE]
    [SIZE=2]in.open([/SIZE][SIZE=2][COLOR=#a31515]"c:\\in_infoproc.txt"[/COLOR][/SIZE][SIZE=2], ios::in); [/SIZE][SIZE=2][COLOR=#008000]// opens in.txt file for input[/COLOR][/SIZE]
     
     
    [SIZE=2][COLOR=#008000]//READ RECORD[/COLOR][/SIZE]
    [SIZE=2]ql_header(); [/SIZE][SIZE=2][COLOR=#008000]// program header (includes clear screen)[/COLOR][/SIZE]
    [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\n\n\n\n\t\tRead Records\n\n\n"[/COLOR][/SIZE][SIZE=2];[/SIZE]
     
    [SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] (in.eof() == [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2])[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]getline(in, fname, [/SIZE][SIZE=2][COLOR=#a31515]' '[/COLOR][/SIZE][SIZE=2]); [/SIZE][SIZE=2][COLOR=#008000]// first name[/COLOR][/SIZE]
    [SIZE=2]getline(in, lname, [/SIZE][SIZE=2][COLOR=#a31515]' '[/COLOR][/SIZE][SIZE=2]); [/SIZE][SIZE=2][COLOR=#008000]// last name[/COLOR][/SIZE]
    [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\t\tYour name is:\t"[/COLOR][/SIZE][SIZE=2] << fname << [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] << lname << endl;[/SIZE]
    [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\t\tYour score is:\t"[/COLOR][/SIZE][SIZE=2];[/SIZE]
    [SIZE=2]in >> score;[/SIZE]
    [SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] (score >= 0)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]cout << score;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i=0;i<10;i++)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]in >> score[i + 0];[/SIZE]
    [SIZE=2]cout << score[i + 0];[/SIZE]
    [SIZE=2]}[/SIZE]
     
    [SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\n\t\t==============================================\n\n"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
    [SIZE=2]}[/SIZE]
     
     
    [SIZE=2][COLOR=#008000]//CLOSE FILES[/COLOR][/SIZE]
    [SIZE=2]in.close();[/SIZE]
     
     
    [SIZE=2][COLOR=#008000]//RETURN TO MENU[/COLOR][/SIZE]
    [SIZE=2]Menu(0);[/SIZE]
     
     
    [SIZE=2][COLOR=#008000]// ----------------------------------------------------------------------------------------------------------- END READ RECORD FUNCTION[/COLOR][/SIZE]
    [SIZE=2]}[/SIZE]
    

    i've tried this tons of different ways but cant seem to find one that works properly. eventually i have to put this in to an array, having fname and lname the arrays.. but if i dont understand the first parts, i'm not ready to put it into an array.
     
    Last edited: Apr 23, 2007
  2. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    Well getline looks like its reading until it gets a \r\n or any other combination of those two characters.

    Seems to me you just need to check for a negative score in your variable, either strip it or not and move on. I don't really see what your problem is...
     
    dubstar likes this.
  3. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    for the first record it spits out the first and last name, no score (its negative)
    the second record becomes aligned improperly, and then the first number (positive) is repeated over and over.... never stops


    edit: you helped. all it takes is waking up. thanks man.

    problem was that i wasnt reintroducing the new variable. in << score in to the loop.
     
    Last edited: Apr 23, 2007
  4. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    another alternative to getting the line data might be to use a std::istringstream.

    I know that wasn't the question but it might make the algorithm simpler and easier to debug
     
    dubstar likes this.
  5. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    thanks geffy, i'll look into that when i get the program fully functional.

    :)