GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Aug-2007, 10:31
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: using void functions; reading from file


CPP / C++ / C Code:
     if (!inData)
     {
          cout << "File not found" << endl;
          getch();
     }
1. If I enter a filename that doesn't exist I get "file not found". If I put some characters in the file the program reads I get "file not found". That's not quite right is it? It's better to put something like this before you try to read from the files. I usually put this test right after opening a file:
CPP / C++ / C Code:
if (!inData.is_open())
{
    cerr << "Unable to open file " << filename;
}
Then you can replace the error message on test "if (!inData)" with something more descriptive, like "Read/Write error" or whatever.

Now look through your program. Think about where do the initial values of t1, t2 etc. change. More precisely, look at the values before and after the call to GetData().

I didn't look through the whole code, just about up to the call to GetData().
  #2  
Old 01-Aug-2007, 14:51
bad_baby_87 bad_baby_87 is offline
New Member
 
Join Date: Jul 2007
Posts: 7
bad_baby_87 is on a distinguished road

using void functions; reading from file


the program i am compiling requires me to read from a file with student id's and their test 5 scores. here is a sample of the data in green:

123 90 98 80 79 70

there are 9 lines of data. i am supposed to use void functions to read and print the data from the file to another file, find each student's lowest test grade, find the average of the remaining test grades, total up the number of students, then find the average test grade for the students. This needs to be printed to another file "results" in a neat little table. i'm getting the table, but it's not coming out right. The program ignores the student id, then says everything else is 0. here is what the output looks like:

0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
0 0 0 0 0 0.00
In a class of 9 the average test score was0.00.

it does calculate the number of students correctly! i think it has something to do with my function GetData. i don't think this is the correct way to retrieve the info from the file, but i'm not sure of another way. i have tried to use the getline function to get the string studentID, but i think that takes the entire line and considers all of those numbers a studentID. and then i can't use get(studentID) because it's not a character. is there another way i can retrieve the info? the compiler gives me a warning that t1, t2, etc are not initialized.... i think it all has to tie in with GetData.

sorry i've written a book, but i know that you who are looking at this want some background to go off of. thanks for any advice!





CPP / C++ / C Code:
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

void Initialize(int&, double&);
void GetData(ifstream&, string, int, int ,int, int, int);
void FindLowest(int, int, int, int, int, int&);
void CalcAverage(int, int, int, int, int, int, double&);
void PrintResults(ofstream&, string, int, int, int, int, int, double);
void AccumulateClassInfo(int&, double, double&);
void CalcClassAverage(int, double, double&);
void PrintClassInfo(ofstream&, int, double);

int main()
{
     ifstream inData;           // input file
     ofstream outData;          // output file
     string studentID;          // student ID number
     int numStudents;           // number of students in file
     int t1;                    // test 1 score
     int t2;                    // test 2 score
     int t3;                    // test 3 score
     int t4;                    // test 4 score
     int t5;                    // test 5 score
     int lowestTest;            // lowest test score
     double average;            // average of 4 highest tests
     double sumOfAverages;      // sum of the averages of all students
     double classAverage;       // average for class


     inData.open("C:\\Documents and Settings\\AG\\Desktop\\Data1.txt");       //CHANGE THE PATH FOR YOUR STORAGE DEVICE
     outData.open("C:\\Documents and Settings\\AG\\Desktop\\results.txt");    //CHANGE THE PATH FOR YOUR STORAGE DEVICE

     outData << fixed << showpoint;
     Initialize(numStudents, sumOfAverages);

     GetData(inData, studentID, t1, t2, t3, t4, t5);
     if (!inData)
     {
          cout << "File not found" << endl;
          getch();
     }
     else
     {
          while (inData)
          {
               FindLowest(t1, t2, t3, t4, t5, lowestTest);
               CalcAverage(t1, t2, t3, t4, t5, lowestTest, average);
               PrintResults(outData, studentID, t1, t2, t3, t4, t5, average);
               AccumulateClassInfo(numStudents, average, sumOfAverages);
               GetData(inData, studentID, t1, t2, t3, t4, t5);
          }
     }
     CalcClassAverage(numStudents, sumOfAverages, classAverage);
     PrintClassInfo(outData, numStudents, classAverage);

  inData.close();
  outData.close();
	 
	 return 0;
}
//**********************************************************************
void Initialize(int& numStudents, double& sumOfAverages)  

//Preconditions: none
//Postconditions: The counter and accumulator are set to zero
{
    numStudents = 0;
	sumOfAverages = 0.00;
}
//**********************************************************************
void GetData(ifstream& inData, string studentID, int t1, int t2, int t3, int t4, int t5)

//Preconditions: The file is open and ready to be read from
//Postconditions: The studentID and the five test grades have been read
{
	inData >> studentID >> t1 >> t2 >> t3 >> t4 >> t5;
		
}
//**********************************************************************
void FindLowest(int t1, int t2, int t3, int t4, int t5, int& lowestTest)

//Preconditions:  The five test grades have been read
//Postconditions: The lowest grade is found
{
        if (t1 < t2 && t1 < t3 && t1 < t4 && t1 < t5)
			lowestTest = t1;

		else if (t2 < t1 && t2 < t3 && t2 < t4 && t2 < t5)
			lowestTest = t2;

		else if (t3 < t1 && t3 < t2 && t3 < t4 && t3 < t5)
			lowestTest = t3;

		else if (t4 < t1 && t4 < t2 && t4 < t3 && t4 < t5)
			lowestTest = t4;

		else 
			lowestTest = t5;
}
//**********************************************************************
void CalcAverage(int t1, int t2, int t3, int t4, int t5, int lowestTest, double& average)

//Preconditions: The five test grades have been read
//               The lowest test grade is known
//Postconditions:  The average of the four best grades is calculated
{
        if (lowestTest = t1)
			average = (t2 + t3 + t4 + t5)/ 4;
		
		if (lowestTest = t2)
			average = (t1 + t3 + t4 + t5)/ 4;

		if (lowestTest = t3)
			average = (t1 + t2 + t4 + t5)/ 4;

		if (lowestTest = t4)
			average = (t1 + t2 + t3 + t5)/ 4;

		else 
			average = (t1 + t2 + t3 + t4)/ 4;
}
//**********************************************************************
void PrintResults(ofstream& outData, string studentID, int t1, int t2, int t3, int t4, int t5, double average)

//Preconditions:  The output file is open and ready to be written to
//                The student ID and the five test grades have been read
//                The average of the four best test grades has been calculated
//Postconditions: The student ID, the five test grades, and the calculated
//                average (with 2 decimal points) have been written to a file
{
	outData << setprecision(2);
	outData << studentID << setw(4) << t1 << setw(4) << t2 << setw(4) << t3 << setw(4) << t4 << setw(4) << t5 
		<< setw(6) << average << endl;       
}
//**********************************************************************
void AccumulateClassInfo(int& numStudents, double average, double& sumOfAverages)
//Preconditions:  The current number of students is known
//                The current accumulated average is known
//Postconditions: The number of students is incremented
//                The new average is added to the total
{
        numStudents++;
		sumOfAverages = sumOfAverages + average;
}
//**********************************************************************
void CalcClassAverage(int numStudents, double sumOfAverages, double& classAverage)

//Preconditions:  All data has been process and the number of students
//                and the sum of class averages is known
//Postconditions: The class average is calculated
{
        classAverage = sumOfAverages / numStudents; 
}
//**********************************************************************
void PrintClassInfo(ofstream& outData, int numStudents, double classAverage)

//Preconditions:  The output file is open and all data has been processed
//                The number of students and the class average is known
//Postconditions: The number of students and class average (with 2 decimal
//                places) has been written to the file
{
	outData << "In a class of " << numStudents << " the average test score was"
		<< setprecision(2) << classAverage << "." << endl;
}

  #3  
Old 01-Aug-2007, 22:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: using void functions; reading from file


After your call to GetData(), try displaying the data to the screen to see if the function worked.
__________________

Age is unimportant -- except in cheese
  #4  
Old 02-Aug-2007, 12:18
bad_baby_87 bad_baby_87 is offline
New Member
 
Join Date: Jul 2007
Posts: 7
bad_baby_87 is on a distinguished road

Re: using void functions; reading from file


ah. so my problem is that i didn't really understand reference variables. when i change the parameters to (string&, int&,...) etc. the program works, because i am changing the value of the variable in the function (at least, i think i know what i am talking about!).

thanks Walt P and Kimmo for your advice.
  #5  
Old 02-Aug-2007, 13:21
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: using void functions; reading from file


Quote:
Originally Posted by bad_baby_87
ah. so my problem is that i didn't really understand reference variables. when i change the parameters to (string&, int&,...) etc. the program works, because i am changing the value of the variable in the function (at least, i think i know what i am talking about!).
References are kind of like pointers as function arguments. When you pass a value by reference to a function, the function works with the original variable. For example, you could make something like this to illustrate:
CPP / C++ / C Code:
#include <iostream>

int & intRefRef(int & n){ return n; }

void doStuff(int & n) { n = 1234; }

int main()
{
    using std::cout;
    using std::endl;

    int value1 = 0;
    int value2 = 1;

    cout << &value1 << endl;
    cout << &intRefRef(value1) << endl;
    cout << value2 << endl;

    doStuff(value2);

    cout << value2 << endl;

    return 0;
}

Output:
Code:
0x22ff74 0x22ff74 1 1234
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Linked Lists advice request promsan C Programming Language 74 23-May-2007 08:29
triangle (polygon), drawing, sizing, and rotation programme using linked lists... promsan C Programming Language 12 14-May-2007 14:03
need help with a console menu system BullBuchanan C++ Forum 6 20-Aug-2006 14:46
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.