Flash Flash Revolution

Flash Flash Revolution (http://www.flashflashrevolution.com/vbz/index.php)
-   Technology (http://www.flashflashrevolution.com/vbz/forumdisplay.php?f=74)
-   -   Instream not working for file inputs (C++) (http://www.flashflashrevolution.com/vbz/showthread.php?t=135139)

DossarLX ODI 02-5-2014 01:14 PM

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:

Code:

Student: John Doe
quiz 1: 60
quiz 2: 75
midterm exam: 80
final exam: 90

Student: Jane Roe
quiz 1: 80
quiz 2: 85
midterm exam: 70
final exam: 75

Student: Joe Shmoe
quiz 1: 90
quiz 2: 85
midterm exam: 87
final exam: 85



Following spoiler shows the code that should have printed out the contents above, but I got nothing.

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main() {

    ifstream in("input.txt");
    string data; // Store string from file to display.

    while (getline(in, data)) {
        cout << data << endl;
        // endl is like inserting \n at the end of the cout.
        // It flushes the output stream as well.
    }
   
    return 0;
}



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.

justin_ator 02-5-2014 01:21 PM

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


Zageron 02-5-2014 01:28 PM

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

DossarLX ODI 02-5-2014 02:31 PM

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

to

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



All times are GMT -5. The time now is 11:03 PM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution