Thread: a file problem !!

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Unhappy a file problem !!

    hi i have a small problem here.
    i have written a program ( it is supposed to be easy and simple ) . it computes numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: each line contains a student's last name, then one space, then the student's first name, then one space, then ten quiz scores all in one line. The quiz scores are whole numbers and are separated by one space. the program will take its input from this file and send its output to a second file. The data in the output file will be exactly the same as the data in the input file except that there will be one additional number(of type double) at the end of each line. This number will be the average of the student's ten quiz scores.
    well, every thing worked fine except for the average. so, this is my code and thank you

    note:
    i only took the basics in C++ i took functions and files (I/O streams)... i didn't take strings,structures, classes, pointers, linked lists, or arrays..

    Code:
     
    #include<iostream>
    #include<fstream>
    #include<cstdlib> 
    using namespace std;
    
    void check ( ifstream& in_f, ofstream& out_f );
    // This function checks if the files are successfully opened...
     
    void copy ( ifstream& in_file , ofstream& out_file ) ;
    // This function copies the information from the input file to the output file
    // as well as computing and writing the average for each student....
    
    int main ()
    
    
    {
    	ifstream in_stream ;
    	ofstream out_stream ;
    
    		cout << " Hello *_* welcome to my                  program ... \n" ;
    		check ( in_stream , out_stream );
    		copy ( in_stream , out_stream );
        	                in_stream.close();
    		out_stream.close();
    		cout << " That's all for now *_* see you later \n";
    
    		return 0 ;
    }
    
    
    void check (ifstream& in_f, ofstream& out_f )
    {
    	in_f.open("in_file.dat") ;
    		if ( in_f.fail() )
    		{ 
              cout << " Input file opening failed ...\n" ;
    		  exit (1) ;
    		}
    
    		out_f.open("out_file.dat");
    		if ( out_f.fail() )
    		{ 
              cout << " Output file opening failed ...\n" ;
    		  exit (1) ;
    		}
    
    
    }
    
    void copy ( ifstream& input_file , ofstream& output_file )
    
    {
        char symbol;
        int sum,count;
    	double average ;
    
      while ( ! input_file.eof() )
      {
        
    
    	input_file.get(symbol);
    	while ( ! isdigit(symbol) )
    
    	{
    		output_file.put(symbol);
    		input_file.get(symbol);
    	}
    
    	sum=0;
    	count=1;
    	 while (symbol !=('\n')) 
    
    	  {	output_file.put(symbol);
    
    		if (isspace(symbol))
    		
    			count = count + 1 ;
    			
    		if  (isdigit(symbol))
    
    			sum = sum + symbol - 48;
    
    		input_file.get(symbol);
    	  }
    
    	 
    	   output_file<< "   (** sum= "<< sum << ", count: " << count ;
    
    	output_file.setf(ios::fixed);
    	output_file.setf(ios::showpoint);
    	output_file.precision(2);
    
    	average = double(sum) / count ;
    	output_file << ", average: "<< sum << " / " << count      << " = " << average 
    		        << " **) "<<endl<<endl ;
    	
      }
    have a nice day

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    well, every thing worked fine except for the average
    The average is calculated by adding all the values together then dividing by the number of values added.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Unhappy

    Well it won't work!! becsuse i'm using the put and get functions!!!

    i.e: i'm taking every character and add it to the previous one therefore i have strange numbers ... what can i do??
    have a nice day

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try (double)sum / (double)count instead.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    actually, since you said that the grades are whole numbers in the file, why subtract 48.

    sum += symbol . and you also check if it is a digit. so just add, then / by count. not sure if you want to increment count for every space. try incrementing when adding a grade.
    Last edited by alpha; 04-14-2003 at 07:12 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    25
    ok.. my program works if the grades are from 0-9 else no!! (i'm writing like a program ....

    ( sum=0 ) what if the first grade was 12 the program will check if 1 is a digit then it will sum it with 0, after that it will check 2 and sum it with 1 .so the sum will be 3 and this is wrong. i want the program to take 12 as whole and sum it to 0...

    what should i do ??
    have a nice day

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    take advantage of the properties of the istream >> operator. >> will terminate input at any whitespace, of which the space character is one example. It will also put the information it reads into any primitive type like char, C style string, int, double, and float without you needing to do a thing, other than declaring appropriate variables to store the information in. And since ifstreams are derived from istreams the >> operator is available to them as well. Forget about all that put/get stuff and ASCII conversions, etc. It's not needed here, unless your instructions were to use it specifically as a learning tool.

    You already know that each line will have exactly the same number of entries. You don't know how many lines there is, nor what the values for each name and score will be. so you will probably need one loop with a second loop embedded in it.

    >> last name
    << last name
    >> first name
    << first name
    >> score
    << score
    total += score
    repeat these three lines nine times
    calculate average
    << average
    go to next line.

    beware that the eof() function when used as a conditional is likely to overread your file by one line causing some erroneous output for the last line of the output file. There are many discussions of this phenomenon on the board so I won't redo them here.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Your basic flaw is that you take evrything, name and grades, as characters. For the names this will not give you any trouble but for the grades it will only get you the first out of two possible numbers. The best option would therefor be to have two character variables BufferOne, BufferTwo. You then, for the name, get into BufferOne and output that variable into the next file till you get to the grades. Once at the grades you get one value into BufferOne then you get one into BufferTwo, you then check if BufferTwo is a space. If it is a space you do the following:

    Code:
    sum = sum + (int)BufferOne;
    if it is not a space we can assume it is the second digit of the number so the following code would apply:

    Code:
    sum = sum + ((int)BufferOne) * 10) + (int)BufferTwo;
    you would then eat the space using a get(BufferOne) and start over again getting the first number into BufferOne. THis method would involve quite a lof of if statements and some looping.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    I was bored so here would be a basic rewrite of your copy function, please do note that i did not feel like debugging it so there might be some elementary mistakes in there like typo's etc, you should be able to pick em outta there.

    Code:
    void copy ( ifstream& input_file , ofstream& output_file )
    {
        	char symbol = '\0';
        	char Buffer = '\0';
        
        	int sum, count;
    	double average ;
    
      	while ( ! input_file.eof() )
      	{
        
    
    		input_file.get(symbol);
    		while ( ! isdigit(symbol) )
    	
    		{
    			output_file.put(symbol);
    			input_file.get(symbol);
    		}
    	
    		sum=0;
    		count=1;
    	
    		while(symbol != '\n')
    		{
    			output_file.put(symbol);
    			input_file.get(symbol);
    			
    			if(isspace(symbol))
    				count = count + 1;
    				
    			else if(isdigit(symbol))
    			{
    				input_file.get(Buffer);
    				
    				if(isdigit(Buffer))
    				{
    					sum = sum + ( (int)symbol * 10 ) + (int)Buffer;
    					input_file.get(symbol);
    				}
    				
    				else
    					sum = sum + ( (int)symbol );
    			}
    		}
    			
    			
    	
    		 
    		output_file<< "   (** sum= "<< sum << ", count: " << count ;
    	
    		output_file.setf(ios::fixed);
    		output_file.setf(ios::showpoint);
    		output_file.precision(2);
    	
    		average = (double)sum / (double)count ;
    		output_file << ", average: "<< sum << " / " << count      << " = " << average 
    			        << " **) "<<endl<<endl ;
    		
    }

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    25
    hello i used another way in the copy function and i asked my teacher to use this way and she said it is ok... The problem is ( i think ) how to move from one line to another after copying it from the input file and adding the average to it???? this is my function i know it looks stupid but what can i do???

    Code:
    void copy ( ifstream& input_file , ofstream& output_file )
    
    {
        char    first_name[20] , last_name[20] ;
        int       sum,num1,num2,num3,num4,num5 ;
    	int num6,num7,num8,num9,num10;
       double    average ;
    
      while ( ! input_file.eof() )
      {
         
    
    	   input_file >> last_name ;
                       output_file << " " << last_name ;
    
                        input_file >> first_name ;
    	   output_file << " " << first_name ;
    
    	   input_file >> num1  ; output_file << " " << num1  ;
    	   input_file >> num2  ; output_file << " " << num2  ;
    	   input_file >> num3  ; output_file << " " << num3  ;
    	   input_file >> num4  ; output_file << " " << num4  ;
    	   input_file >> num5  ; output_file << " " << num5  ;
    	   input_file >> num6  ; output_file << " " << num6  ;
    	   input_file >> num7  ; output_file << " " << num7  ;
    	   input_file >> num8  ; output_file << " " << num8  ;
    	   input_file >> num9  ; output_file << " " << num9  ;
    	   input_file >> num10 ; output_file << " " << num10 ;
    	   
    	output_file.setf(ios::fixed);
    	output_file.setf(ios::showpoint);
    	output_file.precision(2);
    
    	sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
    	average = double(sum) / 10 ;
        output_file << " Average = " << average << endl << endl ; 
    
    	
    
    	 	
      }   
    }
    have a nice day

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Unhappy changed the function but still !!!!

    hello i used another way in the copy function and i asked my teacher to use this way and she said it is ok... The problem is ( i think ) how to move from one line to another after copying it from the input file and adding the average to it???? this is my function i know it looks stupid but what can i do???

    Code:
    void copy ( ifstream& input_file , ofstream& output_file )
    
    {
        char    first_name[20] , last_name[20] ;
        int       sum,num1,num2,num3,num4,num5 ;
    	int num6,num7,num8,num9,num10;
       double    average ;
    
      while ( ! input_file.eof() )
      {
         
    
    	   input_file >> last_name ;
                       output_file << " " << last_name ;
    
                        input_file >> first_name ;
    	   output_file << " " << first_name ;
    
    	   input_file >> num1  ; output_file << " " << num1  ;
    	   input_file >> num2  ; output_file << " " << num2  ;
    	   input_file >> num3  ; output_file << " " << num3  ;
    	   input_file >> num4  ; output_file << " " << num4  ;
    	   input_file >> num5  ; output_file << " " << num5  ;
    	   input_file >> num6  ; output_file << " " << num6  ;
    	   input_file >> num7  ; output_file << " " << num7  ;
    	   input_file >> num8  ; output_file << " " << num8  ;
    	   input_file >> num9  ; output_file << " " << num9  ;
    	   input_file >> num10 ; output_file << " " << num10 ;
    	   
    	output_file.setf(ios::fixed);
    	output_file.setf(ios::showpoint);
    	output_file.precision(2);
    
    	sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
    	average = double(sum) / 10 ;
        output_file << " Average = " << average << endl << endl ; 
    
    	
    
    	 	
      }   
    }
    have a nice day

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    Re: changed the function but still !!!!

    Code:
    while()
    {
      //deal with names for this line
      >> last name
      << last name
      >> first name
      << first name
      //deal with scores for this line
      for()
      {
         >> score
         << score
         total += score
      }
       
       calculate average for this line
       << average
    }//go to next line
    because >> ignores whitespace such as space character and newline character you will automatically go to the next line once you have read everything in from this line. This technique works great when you know that each line has exactly the same number of fields. It doesn't always work under other conditions however.


    NOTE: if you use eof() as the terminating condition for the while loop in this psuedocode there will likely be a duplication of the last line of the input file in the output file. This bug is handled extensively on the board under other posts. Do a search using file handling, or eof() as the search term and I'm sure you will find some of them.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    25
    ok DirX, i have tried your function but the output wasn't right!!!
    elad, how can i go to the new line ??? it doesn't work!!! and if eof() isn't good in this case what am i suppose to use ?? i know nothing else!!!
    have a nice day

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    25
    hello
    i have tried my last code again and guess what ?? it worked except that i have an extra line in the output file and that line contains the same grades as well as the average for the last student ...why is this happening is it because the eof() ??? i don't get it !!
    have a nice day

  15. #15
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    why is this happening is it because the eof() ??? i don't get it !!
    This is what happens, the principle is exactly the same.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM