|
From: DrNoose on 20 Feb 2006 21:48 I'm writing a program that will read a student's name together with his with his or her test scores. The program should then compute the average test score for each student and assign the appropriate grade. The grade scale is as follows: 90-100-, A; 80-89, B; 70-79, C; 60-69, D; 0-59, F. I'm just trying to test to see if my in and out files are working properly. The program compiles, but when I look at the output file there is nothing there. Here is the program so far and a copy of the text files and what the output should look like. I'm thinking that since I haven't done the functions, that could be the reason I'm not seeming anything in the output file. Any help is appreciated! //This Program will read a student's name together with his or her test scores. //The program should then compute the average test score for each student and //assign the appropriate grade. The grade scale is as follows: 90-100-, A; 80-89, //B; 70-79, C; 60-69, D; 0-59, F. #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { ifstream inFile; ofstream outFile; int test1, test2, test3, test4, test5; double average; char studentName; inFile.open ("c:testscores.txt"); if(!inFile) { cout << "Cannot open input file. " << endl; cout << "The program terminates. " << endl; return 1; } outFile.open ("c:testscoresoutput.out"); outFile << fixed << showpoint; outFile << setprecision(2); cout << "Processing data" << endl; inFile >> studentName; outFile << "studentName: " << studentName << endl; inFile >> test1 >> test2 >> test3 >> test4 >> test5 >> average; outFile << " Student " << setw(4) << " Test1 " << test1 << setw(4) << " Test2 " << test2 << setw(4) << " Test3 " << test3 << setw(4) << "Test4 " << test4 << setw(4) << " Test5 " << test5 << " Average " << average << endl; average = static_cast<double> (test1 + test2 + test3 + test4 + test5) / 5.0; outFile <<" Class Average = " << setw(6) << average << endl; inFile.close(); outFile.close(); return 0; } ******************************************************************************************** text file - testscores.txt Balto 85 83 77 91 76 Mickey 80 90 95 93 48 Minnie 78 81 11 90 73 Doc 92 83 30 69 87 Goofy 23 45 96 38 59 Duckey 60 85 45 39 67 Grumpy 27 31 52 74 83 Sunny 93 94 89 77 97 Piggy 79 85 28 93 82 Pluto 85 72 49 75 63 It should have an output like this: Student Test1 Test2 Test3 Test4 Test 5 Average Grade Balto 85 83 77 91 76 Mickey 80 90 95 93 48 Minnie 78 81 11 90 73 Doc 92 83 30 69 87 Goofy 23 45 96 38 59 Duckey 60 85 45 39 67 Grumpy 27 31 52 74 83 Sunny 93 94 89 77 97 Piggy 79 85 28 93 82 Pluto 85 72 49 75 63 Class Average = Thanks!!!!
From: I V on 20 Feb 2006 22:38 DrNoose wrote: > I'm just trying to test to see if my in and out files are working > properly. The program compiles, but when I look at the output file there > is nothing there. First of all, add some error checking: > outFile.open ("c:testscoresoutput.out"); if( !outFile ) { cerr << "Error opening output file" << end; return 1; } > inFile >> studentName; if( !inFile ) { cerr << "Error reading student name" << endl; return 1; } > > outFile << "studentName: " << studentName << endl; if( !outFile ) { cerr << "Error writing student name" << endl; return 1; } > inFile >> test1 >> test2 >> test3 >> test4 >> test5 >> average; if( !inFile ) { cerr << "Error reading student's grades" << endl; return 1; } > outFile << " Student " << setw(4) << " Test1 " << test1 << setw(4) << > " Test2 " << test2 << setw(4) << " Test3 " << test3 > << setw(4) << "Test4 " << test4 << setw(4) << " Test5 " << test5 << > " Average " << average << endl; if( !outFile ) { cerr << "Error writing student's grades" << endl; return 1; } > > average = static_cast<double> (test1 + test2 + test3 + test4 + test5) / > 5.0; > > outFile <<" Class Average = " << setw(6) > << average << endl; if( !outFile ) { cerr << "Error writing class average" << endl; return 1; } > inFile.close(); > outFile.close(); > return 0; > > } This should let you see where the program is going wrong, which is the first stage to fixing the problem. Once you've narrowed it down, if that doesn't help, then you can ask further questions here. (Incidentally, why are you calling the files "c:testscores.txt" and "c:testscoresoutput.out" ? That opening "c:" is unusual, as it means "the current directory on the C drive (note in particular that it _doesn't_ necessarily mean the root directory on the C drive). Why not just access the files from the current directory, with "testscores.txt" etc? Apart from anything else, that will mean your program will work for those of us who don't use windows).
From: BobR on 21 Feb 2006 17:06 DrNoose wrote in message ... >I'm writing a program that will read a student's name together with his >with his or her test scores. The program should then compute the average >test score for each student and assign the appropriate grade. The grade >scale is as follows: 90-100-, A; 80-89, B; 70-79, C; 60-69, D; 0-59, F. > >I'm just trying to test to see if my in and out files are working >properly. The program compiles, but when I look at the output file there >is nothing there. > >Here is the program so far and a copy of the text files and what the >output should look like. I'm thinking that since I haven't done the >functions, that could be the reason I'm not seeming anything in the >output file. Any help is appreciated! > >//This Program will read a student's name together with his or her test >scores. >//The program should then compute the average test score for each >student and >//assign the appropriate grade. The grade scale is as follows: 90-100-, >A; 80-89, >//B; 70-79, C; 60-69, D; 0-59, F. > >#include <iostream> >#include <fstream> >#include <iomanip> > >int main(){ > using namespace std; // move this here, if you must use it. > ifstream inFile; > ofstream outFile; > > int test1, test2, test3, test4, test5; > double average; > char studentName; > inFile.open ("c:testscores.txt"); > if(!inFile){ > cout << "Cannot open input file. \n" > << "The program terminates. " << endl; > return 1; > } > > outFile.open ("c:testscoresoutput.out"); if(!outFile){ cout << "OOOooohh NOOOooooooo. "<<endl; return 1; } > outFile << fixed << showpoint; > outFile << setprecision(2); > > cout << "Processing data" << endl; > inFile >> studentName; > outFile << "studentName: " << studentName << endl; > if( InFile ){ // you read 'studentName' above, stream may be in failstate! > inFile >> test1 >> test2 >> test3 >> test4 >> test5 >> average; } > > outFile << " Student " << setw(4) << " Test1 " << test1 << setw(4) << >" Test2 " << test2 << setw(4) << " Test3 " << test3 > << setw(4) << "Test4 " << test4 << setw(4) << " Test5 " << test5 << >" Average " << average << endl; > average = static_cast<double> (test1 + test2 + test3 + test4 + test5) / >5.0; > outFile <<" Class Average = " << setw(6) > << average << endl; > inFile.close(); > outFile.close(); > return 0; >} >**************************************************************************** **************** >text file - testscores.txt >Balto 85 83 77 91 76 >Mickey 80 90 95 93 48 >Minnie 78 81 11 90 73 [snip] >It should have an output like this: >Student Test1 Test2 Test3 Test4 Test 5 Average Grade >Balto 85 83 77 91 76 >Mickey 80 90 95 93 48 >Minnie 78 81 11 90 73 [snip] >Class Average = >Thanks!!!! If you are NOT in a C++ class (pencil-n-paper, before calculator), you need to learn to use struct and class to group your data. A 'loose' example follows: #include<iostream> #include<ostream> // std::endl #include<string> #include<vector> class Student{ std::string studentName; // #include<string> std::vector<int> tests; // #include<vector> // double average; public: Student(char const *name) : studentName(name){} void AddScore(int const score){ tests.push_back(score); } double CalcAverage() const { double temp(0); for(size_t i(0); i < tests.size(); ++i){ temp += double( tests.at(i) ); } // for(i) // average = temp / tests.size(); // you'll have to remove 'const' to do this // return average; return temp / tests.size(); } // CalcAverage() void Print(std::ostream & out){ out<<studentName<<" scores= "; for(size_t i(0); i < tests.size(); ++i){ out<<tests.at(i)<<" "; } // for(i) out<<" average= "<<CalcAverage()<<std::endl; } }; // class Student // ----------------------------------- int main(){ // quick example to show usage. // Balto 85 83 77 91 76 Student Balto("Balto"); Balto.AddScore(85); Balto.AddScore(83); Balto.AddScore(77); Balto.AddScore(91); Balto.AddScore(76); Balto.Print( std::cout ); std::ofstream DrNooseTest("DrNooseTest.txt"); DrNooseTest<<"Student Test1 Test2 Test3 Test4 Test 5 Average Grade"<<std::endl; Balto.Print( DrNooseTest ); system("pause") // windows only return 0; } // main() end
From: DrNoose on 22 Feb 2006 16:23 Hi! Thanks for the help! I'll let you know what happens! BobR wrote: > DrNoose wrote in message ... >> I'm writing a program that will read a student's name together with his >> with his or her test scores. The program should then compute the average >> test score for each student and assign the appropriate grade. The grade >> scale is as follows: 90-100-, A; 80-89, B; 70-79, C; 60-69, D; 0-59, F. >> >> I'm just trying to test to see if my in and out files are working >> properly. The program compiles, but when I look at the output file there >> is nothing there. >> >> Here is the program so far and a copy of the text files and what the >> output should look like. I'm thinking that since I haven't done the >> functions, that could be the reason I'm not seeming anything in the >> output file. Any help is appreciated! >> >> //This Program will read a student's name together with his or her test >> scores. >> //The program should then compute the average test score for each >> student and >> //assign the appropriate grade. The grade scale is as follows: 90-100-, >> A; 80-89, >> //B; 70-79, C; 60-69, D; 0-59, F. >> >> #include <iostream> >> #include <fstream> >> #include <iomanip> >> >> int main(){ >> using namespace std; // move this here, if you must use it. >> ifstream inFile; >> ofstream outFile; >> >> int test1, test2, test3, test4, test5; >> double average; >> char studentName; >> inFile.open ("c:testscores.txt"); >> if(!inFile){ >> cout << "Cannot open input file. \n" >> << "The program terminates. " << endl; >> return 1; >> } >> >> outFile.open ("c:testscoresoutput.out"); > if(!outFile){ > cout << "OOOooohh NOOOooooooo. "<<endl; > return 1; > } >> outFile << fixed << showpoint; >> outFile << setprecision(2); >> >> cout << "Processing data" << endl; >> inFile >> studentName; >> outFile << "studentName: " << studentName << endl; >> > if( InFile ){ // you read 'studentName' above, stream may be in failstate! >> inFile >> test1 >> test2 >> test3 >> test4 >> test5 >> average; > } >> outFile << " Student " << setw(4) << " Test1 " << test1 << setw(4) << >> " Test2 " << test2 << setw(4) << " Test3 " << test3 >> << setw(4) << "Test4 " << test4 << setw(4) << " Test5 " << test5 << >> " Average " << average << endl; >> average = static_cast<double> (test1 + test2 + test3 + test4 + test5) / >> 5.0; >> outFile <<" Class Average = " << setw(6) >> << average << endl; >> inFile.close(); >> outFile.close(); >> return 0; >> } >> **************************************************************************** > **************** > >> text file - testscores.txt >> Balto 85 83 77 91 76 >> Mickey 80 90 95 93 48 >> Minnie 78 81 11 90 73 > [snip] >> It should have an output like this: >> Student Test1 Test2 Test3 Test4 Test 5 Average Grade >> Balto 85 83 77 91 76 >> Mickey 80 90 95 93 48 >> Minnie 78 81 11 90 73 > [snip] >> Class Average = >> Thanks!!!! > > If you are NOT in a C++ class (pencil-n-paper, before calculator), you need > to learn to use struct and class to group your data. A 'loose' example > follows: > > #include<iostream> > #include<ostream> // std::endl > #include<string> > #include<vector> > > class Student{ > std::string studentName; // #include<string> > std::vector<int> tests; // #include<vector> > // double average; > public: > Student(char const *name) : studentName(name){} > void AddScore(int const score){ tests.push_back(score); } > double CalcAverage() const { > double temp(0); > for(size_t i(0); i < tests.size(); ++i){ > temp += double( tests.at(i) ); > } // for(i) > // average = temp / tests.size(); // you'll have to remove 'const' to > do this > // return average; > return temp / tests.size(); > } // CalcAverage() > void Print(std::ostream & out){ > out<<studentName<<" scores= "; > for(size_t i(0); i < tests.size(); ++i){ > out<<tests.at(i)<<" "; > } // for(i) > out<<" average= "<<CalcAverage()<<std::endl; > } > }; // class Student > // ----------------------------------- > int main(){ // quick example to show usage. > // Balto 85 83 77 91 76 > Student Balto("Balto"); > Balto.AddScore(85); > Balto.AddScore(83); > Balto.AddScore(77); > Balto.AddScore(91); > Balto.AddScore(76); > Balto.Print( std::cout ); > std::ofstream DrNooseTest("DrNooseTest.txt"); > DrNooseTest<<"Student Test1 Test2 Test3 Test4 Test 5 Average > Grade"<<std::endl; > Balto.Print( DrNooseTest ); > system("pause") // windows only > return 0; > } // main() end >
|
Pages: 1 Prev: Newbie Dummie in a Stew with First C++ Program Next: Passing reference through void pointer |