Move to next line in a file C++

16,924

use getline(infile,"string variable")

Using get line sometime may cause error while reading next line because it sometimes takes "end line" as next statement to read so use cin.ignore(); before or after your getline statement depending upon where it works best for you.

Share:
16,924
Morgan
Author by

Morgan

I'm sorry I ask really dumb questions. I am in an online computer science class, live 2 hours away from the school, and work 40 hours a week. I dont have the opportunity for my classmates to look at the code and I dont have any friends that are doing this so sometimes I just need someone to look over my code to tell me I'm missing a semi colon. Thanks for all your help, you guys are the best.

Updated on June 04, 2022

Comments

  • Morgan
    Morgan almost 2 years

    Here is an incomplete fraction of a program I am writing. To summarize, the program takes info from an input file ( a name and ID then a series of numbers) does some math and then prints the answers in an output file. Right now the program prints the name and the ID, and then it prints the numbers over and over and over and over. I would like it to move to the next line, print the name and ID and repeat until the file is over

    Professor says that I should be using getline, but that doesnt make any sense to me

    do
    {
      infile >> name >> Id;
      cout<< name << Id << std::endl;
      hworkgrade = CalHworkGrade(grade1, infile);
      printRecord(name, Id, outfile);
    }
    while(!infile.eof());
    

    input:

    Morgan 12388671 100 100 100 
    John 67894 100 100 100 
    

    output:

    Vagts,Morgan
    100100
    100100
    100100
    100100
    100100
    (300,000 more times)
    

    Update:

    do
    {
      while (getline (infile, line))
      {
      istringstream iss(line);
      iss >> name >> Id;
      cout<< name << Id;
      hworkgrade = CalHworkGrade(grade1, infile);
      printRecord(name, Id, outfile);
      }
    }
    

    while(!infile.eof());