C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 30th, 2006, 10:23 PM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Loop problem

Hi ,
I am coding a 'test corrector' machine.

I need to read a name from a file .
The student name is delimitated by the '#' sign . After the name is a space and then the response to the test in form of T (true)and F(false).

I want to read the name of the student, followed by his answer, followed by the test score.

Each correct answer counts 2 points, each wrong answer -1 point, and no answer 0 point.

My problem is my loop reads 1 more line and prints garbage.

for example, the file is like this:
TFFTFFTTTTFFTFTFTFTF // test key
John Doe# TFTFTFTT TFTFTFFTTFT // note the blank in the response!
Chis Fro# TFTTTFFTTFFTTTFTTFFT

my output should be:
John Doe TFTFTFTT TFTFTFFTTFT score=8
Chris Fro TFTTTFFTTFFTTTFTTFFT score=9

I have something weird with my output.
It is like the program reads one more line and compute a score .

It is like this:

John Doe TFTFTFTT TFTFTFFTTFT score=8
Chris Fro TFTTTFFTTFFTTTFTTFFT score=9
--------(this is all blank)----------score=13// this the 'invisible line'

I don't know why it is doing this. I checked my program and I dont see where it should read one more line.
I know that the problem comes from the EOF but I cannot fix it.


I don't know why my score is wrong. Here is my code:
Code:
#include<fstream>
#include <iostream>
#include <cstdlib>

using namespace std;
const int SIZE=100;

int main()
{
    ifstream fin;
    ofstream fout;
    
    fin.open("input.txt");
    if(fin.fail())
    {
        cerr<<" Input file opening failed.\n";
        exit(1);
    }          


    fout.open("out1.txt");
    if(fout.fail())
    {
        cerr<<" output file opening failed.\n";
        exit(1);
    }    
    
    char name[SIZE]={0};     
    char key[20];// key of the test
    char test[20];// response of student
    char next;// char used to adjust see below
    
    // read key
    for(int i=0;i<20;i++)
    {
       fin>>key[i];
    }  
    // display   key
    for(int i=0;i<20;i++)
    {
       cout<<key[i];
    } 
    cout<<endl;    
    
    int index=0;
    int point=0;
    static int score=0; 
         
    while( fin)
    {
        fin.get(name,SIZE,'#');
        cout << name << endl;
        point=strlen(name);// check to kow where the file cursor is!
       
       
        fin.get(next);// adjust beginning student response array
        fin.get(next);
        
        fin.get(test,100,'\n');
        cout<<test;
        
        for(int i=0;i<20;i++)// score calculation
              {  
                  //code to compute score   
         
              }// end for

              cout<<" "<<score;
          
    }
    
    fin.close();
    fout.close();
    
    return 0;
}

Reply With Quote
  #2  
Old September 30th, 2006, 10:45 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
I think this has somethign to do with the fact that the error bit in ifstream is only set after input has failed. So it might be OK, and you enter the loop for the last time and read a newline or something, and then an "invisible line" appears. This might help (note that feof() is similar to ifstream::eof() ).

Reply With Quote
  #3  
Old October 1st, 2006, 10:18 AM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Quote:
Originally Posted by ubergeek
I think this has somethign to do with the fact that the error bit in ifstream is only set after input has failed. So it might be OK, and you enter the loop for the last time and read a newline or something, and then an "invisible line" appears. This might help (note that feof() is similar to ifstream::eof() ).


thank you Ubergeek. I see what you are saying but can find a way to code it.
I believe I need to have a "check" after each line. and I dont know how I can use feof() as you suggested me to fix my problem.

Reply With Quote
  #4  
Old October 1st, 2006, 04:27 PM
costas costas is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 407 costas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 3 h 19 m 11 sec
Reputation Power: 3
I think you need to put a while loop when you read each line. Something like that:

Code:
while (!fin.eof())
{
     fin.get(test, 100, '\n');
     cout<<test<<endl;
}


Hope I helped!

Costas

Reply With Quote
  #5  
Old October 1st, 2006, 04:44 PM
costas costas is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 407 costas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 3 h 19 m 11 sec
Reputation Power: 3
Hey, I tried and it doesn't work. Try it though! Sorry I can't help, but file I/O isn't my best part.

Reply With Quote
  #6  
Old October 1st, 2006, 05:10 PM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Quote:
Originally Posted by costas
Hey, I tried and it doesn't work. Try it though! Sorry I can't help, but file I/O isn't my best part.

Yes, I tried this too.
Thanks for help though!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Loop problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT