Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 02-5-2014, 01:14 PM   #1
DossarLX ODI
Batch Manager
Game Manager, Song Release Coordinator
Game ManagerSimfile JudgeFFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
DossarLX ODI's Avatar
 
Join Date: Mar 2008
Location: USA
Age: 29
Posts: 14,860
Default 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.
__________________
Quote:
Originally Posted by hi19hi19 View Post
oh boy, it's STIFF, I'll stretch before I sit down at the computer so not I'm not as STIFF next time I step a file
DossarLX ODI is offline   Reply With Quote
Old 02-5-2014, 01:21 PM   #2
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default 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..
justin_ator is offline   Reply With Quote
Old 02-5-2014, 01:28 PM   #3
Zageron
Zageron E. Tazaterra
RRR Developer & DevOps Support
AdministratorDeveloperFFR Veteran
 
Zageron's Avatar
 
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,586
Default 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();
__________________
Zageron is offline   Reply With Quote
Old 02-5-2014, 02:31 PM   #4
DossarLX ODI
Batch Manager
Game Manager, Song Release Coordinator
Game ManagerSimfile JudgeFFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
DossarLX ODI's Avatar
 
Join Date: Mar 2008
Location: USA
Age: 29
Posts: 14,860
Default 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
__________________
Quote:
Originally Posted by hi19hi19 View Post
oh boy, it's STIFF, I'll stretch before I sit down at the computer so not I'm not as STIFF next time I step a file

Last edited by DossarLX ODI; 02-5-2014 at 02:34 PM..
DossarLX ODI is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 08:54 AM.


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