Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-11-2004, 12:17 PM   #1 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Mark Reader Problem

Hey guys... I'm encountering a damn error that won't go away... Whenever I run this program, Windows tells me that the program encountered and error and had to shut down, blah blah blah...

Anywayz, if you can tell me what the problem may be... Please don't change the structure to the program as this is how I need to keep it... I know something is causing an error but I just can't figure it out... Here is the code and the file it is reading from:

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

using namespace std;

void wait_for_enter();

struct studinfo
{
    string last;
    string first;
    float final, mid;
    float homework[10];  
};

int main()
{
    // declare all my variables
    string tfirst, tlast;
    float tmid, tfin, thome[10], gohome;
    int i(0);
    
    ifstream in_file( "c:/temp/students.txt" );  // open a file at this location
       
    if( !in_file.is_open() )  // if the program cannot find the file specified
    {
        cout << "An error occurred while opening the file." << endl;
        wait_for_enter();
        return 0;
    }
    
    vector <studinfo> studentlist;
    while( !in_file.eof() )  // will read through each line until end of file
    {
        in_file >> tlast;
        in_file >> tfirst;
        in_file >> tfin;
        in_file >> tmid;
        in_file >> gohome;
        
        while( gohome != -1 )
        {
            thome[i] = gohome;
            in_file >> gohome;
            i++;
        }
        
        studinfo tempstud;
        tempstud.last = tlast;
        tempstud.first = tfirst;
        tempstud.final = tfin;
        tempstud.mid = tmid;
        
        i = 0;
        
        while( i < 10 )
        {
            tempstud.homework[i] = thome[i];
            i++;
        }
        
        studentlist.push_back(tempstud);
    }
        
    wait_for_enter();	
    return 0;
}

void wait_for_enter()  // for OS independency
{
    cout << "Press <ENTER> to continue...";
    // Reset...
    cin.clear();
    string line;
    getline( cin, line );
}
File that is being read:

Code:
Olive Chris 80 75 60 60 60 60 50 -1
Jingo Puts 95 60 56 57 59 89 89 89 89 76 30 89 -1
Right now, the program is not outputting anything (i will get this in as soon as this stupid windows error goes away)... It is supposed to read the person's last name, then first name, then their final mark, midterm mark, and homework marks, and once it gets to -1, it starts the loop over again...
gamehead200 is offline   Reply With Quote
Old 11-12-2004, 04:13 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Here is a starter:

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

using namespace std;

//Optional: IDE dependency. Remove if not needed. Remove in final release.
void wait_for_enter();

struct studinfo
{
   string last;
   string first;
   float final;
   float mid;
   float homework[10];
};

int main()
{
   unsigned index;
   
   ifstream in_file( /*"c:/temp/ */ "students.txt" );  // open a file at this location
   if( !in_file )  // if the program cannot find the file specified
   {
      cout << "An error occurred while opening the file." << endl;
      wait_for_enter();
      return 0;
   }
   
   vector <studinfo> studentvec;
   studinfo temp;
   //Assign all elements in studinfo::homework[] to -2 for neater output.
   //Later when you know what constructors are, things are getting easier.
   //Or use vector<> instead :)
   unsigned SIZE = sizeof(temp.homework) / sizeof(*temp.homework);
   for(int i=0; i<SIZE; ++i)
   {
      temp.homework[i] = -2;
   }
   while( in_file >>temp.last )  // Must read through each line until end of file
   {
      in_file >> temp.first;
      in_file >> temp.final;
      in_file >> temp.mid;

      index = 0;
      int tempgrade(0);
      //The line below guarantees for the failbit to be set. We will clear it later.
      while( in_file>>tempgrade )
      {
         if(tempgrade != -1)
         {
            //Assumes size of studinfo::homework[] is large enough.
            temp.homework[index] = tempgrade;
            ++index;
         }
      }
      studentvec.push_back(temp);
      //Reset the streamstate to make next iteration possible.
      in_file.clear();
   }

   unsigned studentvecSize = studentvec.size();
   for(unsigned i=0; i != studentvecSize; ++i)
   {
      cout<<studentvec[i].last<<" "<<
      studentvec[i].first<<" "<<
      studentvec[i].final<<" "<<
      studentvec[i].mid<<" ";
      
      for(unsigned j=0; j<SIZE; ++j)
      {
         cout<<studentvec[i].homework[j]<<" ";
      }
      cout<<endl;
   }

   wait_for_enter();
   return 0;
}

void wait_for_enter()  // for OS independency
{
    cout << "Press <ENTER> to continue...";
    // Reset streamstate just in case.
    cin.clear();
    string line;
    getline( cin, line );
}
__________________
Valmont is offline   Reply With Quote
Old 11-12-2004, 10:18 AM   #3 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Valmont, I tried that code... But it doesn't exactly do what I wanted it to do... Can you just review my above code and see what is causing it do shutdown prematurely? It compiles, but it doesn't run the executable file...
gamehead200 is offline   Reply With Quote
Old 11-12-2004, 12:37 PM   #4 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Debugger says segmentation fault.

Lemme check some more.
It happens way at the end of the program.
kaeli69 is offline   Reply With Quote
Old 11-12-2004, 12:41 PM   #5 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Okay, the seg fault is here:
studentlist.push_back(tempstud);

I don't know why yet.
kaeli69 is offline   Reply With Quote
Old 11-12-2004, 01:02 PM   #6 (permalink)
kaeli69
Registered User
 
kaeli69's Avatar
 
Join Date: Apr 2003
Posts: 30
kaeli69 is an unknown quantity at this point
Okay, the problem is actually with the code or your infile. Your code doesn't check that there are only 10 fields, but everything assumes it. The last line in your infile actually has 15 fields.
Either change the code to check the number of fields or modify your infile.

Code worked fine with this infile:
Code:
Olive Chris 80 75 60 60 60 60 50 -1
Jingo Puts 95 60 56 89 76 30 89 -1
I'm going home now.
kaeli69 is offline   Reply With Quote
Old 11-12-2004, 06:24 PM   #7 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Weird... Oh... I see... I'll fix it up in a little while!

Thanks!
gamehead200 is offline   Reply With Quote
Old 11-13-2004, 12:16 AM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Gamehead what does your code differently from mine?
__________________
Valmont is offline   Reply With Quote
Old 11-13-2004, 08:17 AM   #9 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
I have no idea... Valmont, did you try compiling my code? Does it crash on you?

Edit:

OK, I've tracked down what's crashing my program:

Code:
        while( i < 10 )
        {
            tempstud.homework[i] = thome[i];
            i++;
        }
Anyone see any problems with that?

Last edited by gamehead200; 11-13-2004 at 08:55 AM.
gamehead200 is offline   Reply With Quote
Old 11-14-2004, 02:29 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
What I mean is:
What do you want your program to do? That is not clear to me.
You said my program doesn't exactly do what is needed. So what does it need to do?


For example, do you want to store the -1 marker as well? Or should it be ignored.
My version ignores the -1.
But if there are less then 10 homework assignments then the rest of the array will be full of garbage, wich will be ugly in output. So I load the empty cells with "-2", wich means "no assignment for homework".

You have to be more clear. From your code I can't discover the domain of the problem, so I don't know how to fix it. Yes it crashes indeed.
__________________
Valmont is offline   Reply With Quote
Old 11-14-2004, 02:50 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll show you your version (which does exactly as mine). But I removed the errors. Then I will explain the errors.

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

using namespace std;

void wait_for_enter();

struct studinfo
{
    string last;
    string first;
    float final, mid;
    float homework[10];
};

int main()
{
   // declare all my variables
   string tfirst, tlast;
   float tmid, tfin, thome[10], gohome;
   int i(0);
   ifstream in_file( "students.txt" );  // open a file at this location

   if( !in_file.is_open() )  // if the program cannot find the file specified
   {
      cout << "An error occurred while opening the file." << endl;
      wait_for_enter();
      return 0;
   }

   vector <studinfo> studentlist;
   while( !in_file.eof() )  // will read through each line until end of file
   {
      in_file >> tlast;
      in_file >> tfirst;
      in_file >> tfin;
      in_file >> tmid;
      in_file >> gohome;

      i=0;
      while( gohome != -1 )
      {
         thome[i] = gohome;
         in_file >> gohome;
         i++;
      }

      studinfo tempstud;
      tempstud.last = tlast;
      tempstud.first = tfirst;
      tempstud.final = tfin;
      tempstud.mid = tmid;
      
      i = 0;
      while( i < 10 )
      {
         tempstud.homework[i] = thome[i];
         i++;
      }
      studentlist.push_back(tempstud);
   }

   //Print the vector (file).
   unsigned size = studentlist.size();
   for(unsigned i=0; i != size; ++i)
   {
      cout<<studentlist[i].last<<" "<<
      studentlist[i].first<<" "<<
      studentlist[i].final<<" "<<
      studentlist[i].mid<<" ";
      for(unsigned j=0; j<10; ++j)
      {
         cout<<studentlist[i].homework[j]<<" ";
      }
      cout<<endl;
   }


    wait_for_enter();
    return 0;
}

void wait_for_enter()  // for OS independency
{
    cout << "Press <ENTER> to continue...";
    // Reset...
    cin.clear();
    string line;
    getline( cin, line );
}
- You didn't reset "i" to 0 in the next "while" code block.
You should change these while-loops to "for statements" to make the type of loop explicit. That's why these loops are for.
Just watch this code block:
Code:
i = 0;
      while( i < 10 )
      {
         tempstud.homework[i] = thome[i];
         i++;
      }
Do you see that the loop is guaranteed to end if the code "i++" is reached 9 times. This is a typical hint to use for-loops instead! Try to change them into for loops yourself. Or come back for help. No problem :).

- Also look at my code. I am using less variables and use less memory.
- And I loath the usage of "eof()" in these cases. You are processing past the true end of file now. Never forget that eof() is defined as a file-pointer PAST the true end in C++! In simple occasions that isn't a problem. But nevertheless it is not correct. See how I do it. Or perhaps I could make a mini-mini-mini tutorial in this thread for you to see what's going on with this.

- Don't call your vector "studentlist". A vector is not a list. Call it "studentvec" or something like that.

- By the way.
In my first code I used...
unsigned SIZE = sizeof(temp.homework) / sizeof(*temp.homework);
...instead of the number "10" for arrays. This way you can change the size of the array in the struct without changing all the "10-s" :).
In your corrected code I didn't do that.

What you should try:
1) Try to do it without this:
Code:
string tfirst, tlast;
   float tmid, tfin, thome[10], gohome;
2) Change that while-loop into a for-loop.
3) Change the name of studentlist.
4) The eof()-loop we can do together. So nevermind that for now.
5) See if you're interested in changing float homework[10]; into vector<unsigned> homework.
__________________

Last edited by Valmont; 11-14-2004 at 03:58 AM.
Valmont is offline   Reply With Quote
Old 11-14-2004, 06:08 AM   #12 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Thanks, captain! Will try that out when I get the chance today... I'm on my way out right now...
gamehead200 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
omfg... well, i may need help just understanding this problem... .pakmon. Standard C, C++ 3 01-08-2004 08:44 AM
Web Design Problem elusionsdesign HTML, XML, Javascript, AJAX 2 11-09-2003 07:01 PM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 06:17 AM


All times are GMT -8. The time now is 10:50 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting