02-5-2014, 01:14 PM | #1 |
Batch Manager
Game Manager, Song Release Coordinator
Join Date: Mar 2008
Location: USA
Age: 29
Posts: 14,860
|
Instream not working for file inputs (C++)
Fission and I had quite a long talk about this and we couldn't figure out why the code was not working. It builds with no errors but when I run it I get this. I continually built it after suggested changes, but each time it was just blank when I ran it.
The catch here is to read each line from the input.txt file and then print it out. Here's the data from the input.txt file: Following spoiler shows the code that should have printed out the contents above, but I got nothing. Of course later on I want to read the characters after the : symbol which I assume can be done by changing the while loop to while (getline(in, data, ':')) But right now nothing is getting stored into the data string variable. |
02-5-2014, 01:21 PM | #2 |
🥓<strong><span style="col
Resident Overseer
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
|
Re: Instream not working for file inputs (C++)
I looked into errors with it online and maybe this would be helpful:
Code:
3. Lastly, and perhaps more tricky. After reading the employee id using fin>>id; there will be a newline character remaining in the input buffer. This causes the next getline() to retrieve an empty line, and throws everything out of sync. To fix this, insert after the opening brace at line 59: fin.ignore(); // ignore newline character
__________________
Last edited by Zageron; 02-5-2014 at 01:34 PM.. |
02-5-2014, 01:28 PM | #3 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,586
|
Re: Instream not working for file inputs (C++)
I took your code.
The only thing I found wrong with it, was that the input.txt had to be in the correct folder, depending on how you launch it. If you're using cmdline, it should be right next to the executable. If you're using visual studio, it should be in the project directory. Your code works as intended. Tip: bool isopen = in.is_open();
__________________
|
02-5-2014, 02:31 PM | #4 |
Batch Manager
Game Manager, Song Release Coordinator
Join Date: Mar 2008
Location: USA
Age: 29
Posts: 14,860
|
Re: Instream not working for file inputs (C++)
Alright, now there's another problem. Pastebin of the code: http://pastebin.com/YPB4Sgq7
Since the text file is structured as follows: - Name\n - 4 quiz grades\n - Newline Gap to the next name There is a call to getline for the name first, then the for loop calls getline four times again for the quizzes, then getline is called after the loop to make up for that newline gap. But I'm getting garbage for output. Edit: Ok so adding endl to the end of the cout statements made the difference between Code:
C:\computing3\streamtest\streamtest\dist\Debug\Cygwin-Windows>streamtest.exe 85e Shmoe Code:
C:\computing3\streamtest\streamtest\dist\Debug\Cygwin-Windows>streamtest.exe John Doe 60 75 80 90 Jane Roe 80 85 70 75 Joe Shmoe 90 85 87 85
__________________
Last edited by DossarLX ODI; 02-5-2014 at 02:34 PM.. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|