2

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());

Morgan
  • 153
  • 1
  • 2
  • 14
  • 1
    Why don't you read *all* the data on the line? And if you don't want it then why not use [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) to read one line at a time? – Some programmer dude Feb 20 '17 at 06:40
  • I tried that earlier but it didn't work. I probably did it wrong. Could you give me a hand? – Morgan Feb 20 '17 at 06:42
  • It's simple: `while (std::getline(infile, line)) { /* put line in an std::istringstream and extract the data from that string stream */ }` – Some programmer dude Feb 20 '17 at 06:44
  • @Someprogrammerdude suggests the best way to approach this problem. When you do `infile >> name >> Id`, the problem is that each line has more things following it (`100 100 100` in your example). So either declare more variables to stash the scores in, or just work with `isstringstream` and process each line. If you want this to be able to support more scores in the future, you'll have to implement that anyway – svenevs Feb 20 '17 at 06:49
  • now it skips over the first line and prints the name from the second line twice. I have tried rearranging the order of the loop and clearing the variable at the start of each loop as well. – Morgan Feb 20 '17 at 06:55
  • hey morgan, it's pretty much impossible to understand how to help you without seeing updated code. you'll get there, just be patient. i think you should stop trying to write to any files or anything, and just print out results. additionally, search for related problems. For example, [adapt this csv parsing approach](http://stackoverflow.com/questions/16446665/c-read-from-csv-file) to, instead of working with `,` as your delimiter, work with spaces. Or even better, since CSV is supported by many programs and websites, use that format instead – svenevs Feb 20 '17 at 07:09
  • @sjm324 This is a much cleaner code thank you. Except now it will not move to the next line. It only prints the first line once. – Morgan Feb 20 '17 at 07:12
  • 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()); – Morgan Feb 20 '17 at 07:13

1 Answers1

3

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.

Saqib Javed
  • 120
  • 9