Quantcast

Maximum PC

It is currently Tue Jul 22, 2014 10:58 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Issue reading from file (C++)
PostPosted: Mon Dec 06, 2010 7:06 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
I have a pretty simple programming project where I need to process test grades from a file. Everything is working spot on, except for one issue. Here is the part of my code that parses the file:
Code:
  fs.open("tests.dat");
  while (!fs.eof())
    {
      for (int i=0; i<20; i++)
        {
          fs >> studentAnswer[i];
          if (answerKey[i] == studentAnswer[i])
            studentResult[i] = true;
          else
            studentResult[i] = false;
        }
      printResults(studentResult);
    }

and here is the contents of tests.dat
Code:
B B A A C B B A D C B C D A D C C B B C
B D A A C A B A C D B C D A D C C B D A
B B A A C C B A C D B C D A C C C B D A
A B A A C A A A C C B C D A C C C B D A
A B C D A B C D A B C D A B C D A B C D
B D A A C A A A C D B C D A D C C B D A
B D A A C A B A D C B C D A D C C B D A


I do not know how many test grades I need to process, so I need to process until the end of the file.

The problem is, the program processes the last line of answers twice and I have absolutely no idea why. I'd appreciate any help, let me know if you need more info.


Last edited by gamerfreak on Mon Dec 06, 2010 9:30 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Mon Dec 06, 2010 7:37 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Have you tried just reading it in and just printing the values out?


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Mon Dec 06, 2010 7:50 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
CrashTECH wrote:
Have you tried just reading it in and just printing the values out?

It prints out all the letters less white space. I threw a counter in the loop and it fires 141 times (there are 140 characters).


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Mon Dec 06, 2010 9:47 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Change you code from
Code:
while (!CONDITION) {
...
}


to

Code:
do {
...
} until (CONDITION)


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Mon Dec 06, 2010 10:12 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
Jip, I had already tried to put it in a do-while. Unfortunately it has the same result.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Mon Dec 06, 2010 10:17 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
gamerfreak wrote:
Jip, I had already tried to put it in a do-while. Unfortunately it has the same result.


Weird .. I can't see any error in your logic otherwise.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Tue Dec 07, 2010 5:54 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Did this in VS 2010... Eclipse was being ridiculous.

Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <assert.h>

using namespace std;

void readFileMethod1();

int main(void) {

    char key;
   
    readFileMethod1();
   
    cout << "q for exit";
    cin >> key;

    return EXIT_SUCCESS;
}

void readFileMethod1()
{
    ifstream indata;
    char val;
    int cnt = 0;
   
    indata.open("tests.dat");
    if(!indata) {
        cout << "Error: file could not be opened" << endl;
        return;
    }
   
    indata >> val;
    while ( !indata.eof() ) {
        cnt++;
        indata >> val;
    }
    indata.close();

    cout << "End-of-file reached... " << cnt << endl;
}


This shows 140.

How are you setting up your arrays?


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Tue Dec 07, 2010 6:05 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Problem solved??

Code:
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#include <cassert>

using namespace std;

int main()
{
    vector<string> text_file;
    char key;

    ifstream ifs( "tests.dat" );
    string temp;

    if(ifs.fail())
    {
        cerr<<"\nThe file could not be opened.";
        return -1;
    }

    cout << "\nContents of file as read in:\n";
    while( getline(ifs, temp ) )
    {
        cout << temp << endl;
        text_file.push_back( temp );
    }

    cout << "\n\nContents of vector:\n";

    for (int i=0; i<text_file.size(); i++)
    {
        cout << text_file[i] << endl;
    }

    cin >> key;
}


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Tue Dec 07, 2010 11:41 am 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
I asked my professor about it today. He said it was a bug in the eof function in the copy of the header file we are using it, and not to worry about it.

Problem solved :D


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Tue Dec 07, 2010 11:54 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Thank god .. this has been bugging the crap out of me. :P


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Tue Dec 07, 2010 12:51 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
Jipstyle wrote:
Thank god .. this has been bugging the crap out of me. :P

Same, I tried so many ridiculous things to fix it.

Thanks for the help Jip and Crash.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Tue Dec 07, 2010 7:16 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
That would have annoyed the crap out of me!


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 3:40 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Damn... I would have figured that these kinds of issues with C++ libraries would have been gone for five years now.

Do you guys remember all the goofy scoping issues with C++ in VS? The most common one was...
Code:
void foo (int n)
{
  for (int i = 0; i < n; i++);
  cout << i << endl;  //prints i = n despite not being in scope
}

That was the surest way to know that students didn't follow directions and compile their code on the school Sun system. I probably took more points off for that "error" than everything else combined.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 8:59 am 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
Gadget wrote:
Damn... I would have figured that these kinds of issues with C++ libraries would have been gone for five years now.

Do you guys remember all the goofy scoping issues with C++ in VS? The most common one was...
Code:
void foo (int n)
{
  for (int i = 0; i < n; i++);
  cout << i << endl;  //prints i = n despite not being in scope
}

That was the surest way to know that students didn't follow directions and compile their code on the school Sun system. I probably took more points off for that "error" than everything else combined.

We are probebly using a dated version of g++. The server is running SunOS 5.10.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 9:06 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
That is beyond sad lol.

Even the local community college I went to had a more recent version lol.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 10:06 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Gadget wrote:
Damn... I would have figured that these kinds of issues with C++ libraries would have been gone for five years now.

Do you guys remember all the goofy scoping issues with C++ in VS? The most common one was...
Code:
void foo (int n)
{
  for (int i = 0; i < n; i++);
  cout << i << endl;  //prints i = n despite not being in scope
}

That was the surest way to know that students didn't follow directions and compile their code on the school Sun system. I probably took more points off for that "error" than everything else combined.


That is hilarious. I bet you had a bunch of angry undergrads in your office every week, complaining that they lost points when their code works 'just fine'.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 10:07 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
CrashTECH wrote:
That is beyond sad lol.

Even the local community college I went to had a more recent version lol.


I'm predicting that within the next 10 years, we'll invent a new form of punctuation .. something that symbolises the 'lol' with which everyone ends their sentences now.

lol


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 12:16 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
ucwutididthur? ^_^


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 12:49 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
CrashTECH wrote:
That is beyond sad lol.

Even the local community college I went to had a more recent version lol.

Yea, but has unix or emacs really changed all that much in the past 10 years? Especially for the purpose of compiling elementary c++ programs, if it works - leave it.


Top
  Profile  
 
 Post subject: Re: Issue reading from file (C++)
PostPosted: Wed Dec 08, 2010 1:17 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
CrashTECH wrote:
ucwutididthur? ^_^


Inorite? lol


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group