-
July 27th, 2010, 02:51 PM
#16
Re: Simple GradeSystem
Originally Posted by GCDEF
You have a lot of problems. Starting at the beginning, at the time you're using it, the compiler hasn't seen findLetter, so it doesn't know what it is. Move it before main.
studentAverage is declared in main, yet you're trying to use it in findLetter. You'll need to pass it to findLetter as an argument. Same with letterGrade.
I explained the problem with A, B, C, etc earlier in the thread.
Not sure what you're trying to do in the first line of saveError.
In this for loop
Code:
for ( gradeNumber; canWe == true; gradeNumber++; )
{
cout << "Please submit the next grade." << endl << "Grade: ";
cin >> submitAmount;
submitAmount + totalAmount = totalAmount;
cout << "Thank you, the grade has been added." << endl;
cout << "Would you like to add another grade or would you like to quite?" << endl;
cout << "Type 1 to contine or type 2 to quite and press <enter>." << endl << "1 or 0: "; // may need to be switched to true or false
cin >> canWe >> endl;
return totalAmount, gradeNumber;
}
What are you trying to do with gradeNumber?
You have an unconditional return in the loop.
You can only return one value.
Your function calls after the loop won't be executed because of the return in the for loop.
I thought that it would first look at all the things above main() and then start the program. So long as it was above main then I would be fine. I will change that.
What I was trying to do was get the number and add all the numbers together and then average so I could give a grade.
-
July 27th, 2010, 02:55 PM
#17
Re: Simple GradeSystem
Originally Posted by Zeveso
I thought that it would first look at all the things above main() and then start the program. So long as it was above main then I would be fine. I will change that.
What I was trying to do was get the number and add all the numbers together and then average so I could give a grade.
I'm not really following you. It does look above main first. The problem you're having, at least with findLetter as I said is that you're using it before the compiler has seen it. You can either move it before you try to use it, or prototype it.
for(gradeNumer... doesn't make sense. The first part of a for loop lets you declare and initialize variables before the loop body. You're doing neither. You're not even using it in your loop.
Last edited by GCDEF; July 27th, 2010 at 02:57 PM.
-
July 27th, 2010, 03:07 PM
#18
Re: Simple GradeSystem
Originally Posted by GCDEF
I'm not really following you. It does look above main first. The problem you're having, at least with findLetter as I said is that you're using it before the compiler has seen it. You can either move it before you try to use it, or prototype it.
for(gradeNumer... doesn't make sense. The first part of a for loop lets you declare and initialize variables before the loop body. You're doing neither. You're not even using it in your loop.
i made comments, but ok... let me try again...
Code:
for ( gradeNumber; canWe == true; gradeNumber++; ){
cout << "Please submit the next grade." << endl << "Grade: ";
cin >> submitAmount;
submitAmount + totalAmount = totalAmount;
cout << "Thank you, the grade has been added." << endl;
cout << "Would you like to add another grade or would you like to quite?" << endl;
cout << "Type 1 to contine or type 2 to quite and press <enter>." << endl << "1 or 0: "; // may need to be switched to true or false
cin >> canWe >> endl;
return totalAmount, gradeNumber;
}
gradeNumber is the amount of averages, so lets say i have 100 90 and 80 that is 3 'gradeNumber's
canWe wants to know if we can continue, it it is true that we want to continue... then do the whole loop over again
gradeNumber++ lets us raise the amount of averages every time we run the loop
ex. loop run once gradeNumber = 1 loop run twice gradeNumber = 2 ect...
hope you understand now
-
July 27th, 2010, 03:23 PM
#19
Re: Simple GradeSystem
Originally Posted by Zeveso
i made comments, but ok... let me try again...
Code:
for ( gradeNumber; canWe == true; gradeNumber++; ){
cout << "Please submit the next grade." << endl << "Grade: ";
cin >> submitAmount;
submitAmount + totalAmount = totalAmount;
cout << "Thank you, the grade has been added." << endl;
cout << "Would you like to add another grade or would you like to quite?" << endl;
cout << "Type 1 to contine or type 2 to quite and press <enter>." << endl << "1 or 0: "; // may need to be switched to true or false
cin >> canWe >> endl;
return totalAmount, gradeNumber;
}
gradeNumber is the amount of averages, so lets say i have 100 90 and 80 that is 3 'gradeNumber's
canWe wants to know if we can continue, it it is true that we want to continue... then do the whole loop over again
gradeNumber++ lets us raise the amount of averages every time we run the loop
ex. loop run once gradeNumber = 1 loop run twice gradeNumber = 2 ect...
hope you understand now
But that's not how you do it. What you want is something like
Code:
for(int nCurrentGrade = 0; nCurrentGrade < gradeNumber; nCurrentGrade++)
That's if you're getting a specific number of grades. If you're going till the user says stop you want a while loop
-
July 27th, 2010, 06:07 PM
#20
Re: Simple GradeSystem
Ok, I think i got those changes right... but still the program is no where near runable
Current Errors...
Code:
gradeSystem.cpp: In function 'int findLetter(int, char)':
gradeSystem.cpp:17: error: 'A' was not declared in this scope
gradeSystem.cpp:20: error: 'B' was not declared in this scope
gradeSystem.cpp:23: error: 'C' was not declared in this scope
gradeSystem.cpp:26: error: 'D' was not declared in this scope
gradeSystem.cpp:29: error: 'F' was not declared in this scope
gradeSystem.cpp: In function 'void writeGrade(int, int)':
gradeSystem.cpp:13: error: too few arguments to function 'int findLetter(int, char)'
gradeSystem.cpp:38: error: at this point in file
gradeSystem.cpp: In function 'void saveError()':
gradeSystem.cpp:56: error: expected primary-expression before 'int'
gradeSystem.cpp:56: error: 'nowTime' was not declared in this scope
gradeSystem.cpp:59: error: 'theTime' was not declared in this scope
gradeSystem.cpp: In function 'int nowTime()':
gradeSystem.cpp:69: error: 'theTime' was not declared in this scope
gradeSystem.cpp: In function 'int main()':
gradeSystem.cpp:90: error: no match for 'operator>>' in 'std::operator>> [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> >&)(& std::cin)), ((char*)(& studentName))) >> std::endl'
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:748: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/istream.tcc:858: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
gradeSystem.cpp:98: error: lvalue required as left operand of assignment
gradeSystem.cpp:102: error: no match for 'operator>>' in 'std::cin.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((bool&)(& canWe))) >> std::endl'
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:748: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/istream.tcc:858: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
gradeSystem.cpp:46: error: too few arguments to function 'int theAverage(int, int, int)'
gradeSystem.cpp:107: error: at this point in file
gradeSystem.cpp:34: error: too few arguments to function 'void writeGrade(int, int)'
gradeSystem.cpp:108: error: at this point in file
make: *** [gradeSystem] Error 1
Code is now...
Code:
#include <iostream>
#include <fstream> // open, read, and write files
//for time
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int findLetter(int studentAverage, char letterGrade){
//take the number and find the letter it is == to
//then store that letter in letterGrade
if ( studentAverage > 95 ){
letterGrade = A;
if ( 95 > studentAverage && studentAverage > 85 ){
letterGrade = B;
}
if ( 85 > studentAverage && studentAverage > 80 ){
letterGrade = C;
}
if ( 80 > studentAverage && studentAverage > 69 ){
letterGrade = D;
}
if ( studentAverage < 69 ){
letterGrade = F;
}
return letterGrade;
}
void writeGrade(int studentAverage, int letterGrade){
/*
Use the variable gradeAverage to determin the the grade was a A, B, C, D, or F. Then open of the person's file, and write the grade.
*/
findLetter();
ofstream gradeFile;
gradeFile.open ("grades.txt");
gradeFile << "Student: " << studentAverage << endl;
gradeFile << "Grade: " << letterGrade << endl;
gradeFile << "Average: " << studentAverage << endl << endl;
}
int theAverage(int totalAmount, int gradeNumber, int studentAverage){
/*
Here we will average all the numbers together inside of a for loop
*/
studentAverage = totalAmount / gradeNumber;
return studentAverage;
}
void saveError(){
// save error in log.txt
nowTime(int theTime);
ofstream logFile;
logFile.open ("log.txt");
logFile << "Date: " << theTime << endl;
logFile << "Error: File does not exist or cannot be opened." << endl;
logFile << "Please create file, or create a new file." << endl << endl;
}
int nowTime(){
time_t now;
time(&now);
theTime = "%s", ctime(&now);
return theTime;
}
int main(){
//startup variables that we will use
char studentName[255];
int studentAverage;
int gradeNumber; // amount of student Averages
int totalAmount; //will hold all averages
int submitAmount; //amount submited during for loop session
char letterGrade; // will store the lettergrade before it is written to grades.txt
int theTime; // will hold the current time
bool canWe; // asks if we can continue
// find out who we will be grading
cout << "Hello user!" << endl;
cout << "This program lets you average the grade of your student and write there grade in a text file." << endl;
cout << "Who will be writting a grade for today?" << endl << "Name: ";
cin >> studentName >> endl;
cout << "Ok, we will be grading " << studentName << "today!" << endl;
// make a loop for finding out the average
while ( canWe = true ){
for ( int gradeNumber = 0; gradeNumber++; ){
cout << "Please submit the next grade." << endl << "Grade: ";
cin >> submitAmount;
submitAmount + totalAmount = totalAmount;
cout << "Thank you, the grade has been added." << endl;
cout << "Would you like to add another grade or would you like to quite?" << endl;
cout << "Type 1 to contine or type 2 to quite and press <enter>." << endl << "1 or 0: "; // may need to be switched to true or false
cin >> canWe >> endl;
return totalAmount, gradeNumber;
}
}
theAverage();
writeGrade();
return 0;
}
-
July 27th, 2010, 06:30 PM
#21
Re: Simple GradeSystem
Again, A is a variable name. If you really mean the character A, wrap it in single quotes 'A'.
The rest of your problems seem to be because you have problem batching your braces in findLetter.
FWIW, I find that style of brace matching pretty hard to read. Coded like this, your problem becomes obvious.
Code:
int findLetter(int studentAverage, char letterGrade)
{
if ( studentAverage > 95 )
{
letterGrade = A;
if ( 95 > studentAverage && studentAverage > 85 )
{
letterGrade = B;
}
if ( 85 > studentAverage && studentAverage > 80 )
{
letterGrade = C;
}
if ( 80 > studentAverage && studentAverage > 69 )
{
letterGrade = D;
}
if ( studentAverage < 69 )
{
letterGrade = F;
}
return letterGrade;
}
void writeGrade(int studentAverage, int letterGrade){
I believe I already mentioned separating your if statements with else. I also mentioned the return in your for statement and that you're trying to return two values. We're all happy to help, but you need to pay attention to the advice you're getting. You still have a lot of other problems, but work on them first.
Last edited by GCDEF; July 27th, 2010 at 06:41 PM.
-
July 27th, 2010, 07:49 PM
#22
Re: Simple GradeSystem
Originally Posted by GCDEF
Again, A is a variable name. If you really mean the character A, wrap it in single quotes 'A'.
The rest of your problems seem to be because you have problem batching your braces in findLetter.
FWIW, I find that style of brace matching pretty hard to read. Coded like this, your problem becomes obvious.
Code:
int findLetter(int studentAverage, char letterGrade)
{
if ( studentAverage > 95 )
{
letterGrade = A;
if ( 95 > studentAverage && studentAverage > 85 )
{
letterGrade = B;
}
if ( 85 > studentAverage && studentAverage > 80 )
{
letterGrade = C;
}
if ( 80 > studentAverage && studentAverage > 69 )
{
letterGrade = D;
}
if ( studentAverage < 69 )
{
letterGrade = F;
}
return letterGrade;
}
void writeGrade(int studentAverage, int letterGrade){
I believe I already mentioned separating your if statements with else. I also mentioned the return in your for statement and that you're trying to return two values. We're all happy to help, but you need to pay attention to the advice you're getting. You still have a lot of other problems, but work on them first.
Thanks, will do. I noticed that the opening of the file, writing of the variable, and closing of the file were messed up. I just figured out how to fix it and now it is fixed. Will do that now. Thank you!
-
July 27th, 2010, 10:00 PM
#23
Re: Simple GradeSystem
ok, now i have gotten the errors down by about 1/4 or so...
Errors:
Code:
root@bt:~/gradesystem# make gradeSystem
g++ gradeSystem.cpp -o gradeSystem
gradeSystem.cpp: In function 'void writeGrade(int, int)':
gradeSystem.cpp:13: error: too few arguments to function 'int findLetter(int, char)'
gradeSystem.cpp:38: error: at this point in file
gradeSystem.cpp: In function 'void saveError()':
gradeSystem.cpp:56: error: expected primary-expression before 'int'
gradeSystem.cpp:56: error: 'nowTime' was not declared in this scope
gradeSystem.cpp:59: error: 'theTime' was not declared in this scope
gradeSystem.cpp: In function 'int nowTime()':
gradeSystem.cpp:69: error: 'theTime' was not declared in this scope
gradeSystem.cpp: In function 'int main()':
gradeSystem.cpp:90: error: no match for 'operator>>' in 'std::operator>> [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> >&)(& std::cin)), ((char*)(& studentName))) >> std::endl'
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:748: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/istream.tcc:858: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
gradeSystem.cpp:102: error: no match for 'operator>>' in 'std::cin.std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((bool&)(& canWe))) >> std::endl'
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:748: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/bits/istream.tcc:858: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
gradeSystem.cpp:46: error: too few arguments to function 'int theAverage(int, int, int)'
gradeSystem.cpp:107: error: at this point in file
gradeSystem.cpp:34: error: too few arguments to function 'void writeGrade(int, int)'
gradeSystem.cpp:108: error: at this point in file
make: *** [gradeSystem] Error 1
New Source:
Code:
#include <iostream>
#include <fstream> // open, read, and write files
//for time
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int findLetter(int studentAverage, char letterGrade){
//take the number and find the letter it is == to
//then store that letter in letterGrade
if ( studentAverage > 94 ){
letterGrade = 'A';
}
if ( 94 >= studentAverage && studentAverage > 85 ){
letterGrade = 'B';
}
if ( 85 >= studentAverage && studentAverage > 78 ){
letterGrade = 'C';
}
if ( 78 >= studentAverage && studentAverage > 69 ){
letterGrade = 'D';
}
if ( studentAverage < 69 ){
letterGrade = 'F';
}
return letterGrade;
}
void writeGrade(int studentAverage, int letterGrade){
/*
Use the variable gradeAverage to determin the the grade was a A, B, C, D, or F. Then open of the person's file, and write the grade.
*/
findLetter(/*int studentAverage, char letterGrade*/);
ofstream gradeFile;
gradeFile.open ("grades.txt", ios::out | ios::app);
gradeFile << "Student: " << studentAverage << endl;
gradeFile << "Grade: " << letterGrade << endl;
gradeFile << "Average: " << studentAverage << endl << endl;
}
int theAverage(int totalAmount, int gradeNumber, int studentAverage){
/*
Here we will average all the numbers together inside of a for loop
*/
studentAverage = totalAmount / gradeNumber;
return studentAverage;
}
void saveError(){
// save error in log.txt
nowTime(int theTime);
ofstream logFile;
logFile.open ("log.txt", ios::out | ios::app);
logFile << "Date: " << theTime << endl;
logFile << "Error: File does not exist or cannot be opened." << endl;
logFile << "Please create file, or create a new file." << endl << endl;
}
int nowTime(){
time_t now;
time(&now);
theTime = "%s", ctime(&now);
return theTime;
}
int main(){
//startup variables that we will use
char studentName[255];
int studentAverage;
int gradeNumber; // amount of student Averages
int totalAmount; //will hold all averages
int submitAmount; //amount submited during for loop session
char letterGrade; // will store the lettergrade before it is written to grades.txt
int theTime; // will hold the current time
bool canWe; // asks if we can continue
// find out who we will be grading
cout << "Hello user!" << endl;
cout << "This program lets you average the grade of your student and write there grade in a text file." << endl;
cout << "Who will be writting a grade for today?" << endl << "Name: ";
cin >> studentName >> endl;
cout << "Ok, we will be grading " << studentName << "today!" << endl;
// make a loop for finding out the average
while ( canWe = true ){
for ( int gradeNumber = 0; gradeNumber++; ){
cout << "Please submit the next grade." << endl << "Grade: ";
cin >> submitAmount;
totalAmount = submitAmount + totalAmount;
cout << "Thank you, the grade has been added." << endl;
cout << "Would you like to add another grade or would you like to quite?" << endl;
cout << "Type 1 to contine or type 2 to quite and press <enter>." << endl << "1 or 0: "; // may need to be switched to true or false
cin >> canWe >> endl;
return totalAmount;
}
}
theAverage();
writeGrade();
return 0;
}
what should I do if I need more than one thing returned? or all variables still there and "saved"?
-
July 28th, 2010, 01:37 AM
#24
Re: Simple GradeSystem
Originally Posted by Zeveso
ok, now i have gotten the errors down by about 1/4 or so...
Errors:
Code:
root@bt:~/gradesystem# make gradeSystem
g++ gradeSystem.cpp -o gradeSystem
gradeSystem.cpp: In function 'void writeGrade(int, int)':
gradeSystem.cpp:13: error: too few arguments to function 'int findLetter(int, char)'
gradeSystem.cpp:38: error: at this point in file
New Source:
Code:
#include <iostream>
#include <fstream> // open, read, and write files
//for time
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int findLetter(int studentAverage, char letterGrade){
//take the number and find the letter it is == to
//then store that letter in letterGrade
if ( studentAverage > 94 ){
letterGrade = 'A';
}
if ( 94 >= studentAverage && studentAverage > 85 ){
letterGrade = 'B';
}
if ( 85 >= studentAverage && studentAverage > 78 ){
letterGrade = 'C';
}
if ( 78 >= studentAverage && studentAverage > 69 ){
letterGrade = 'D';
}
if ( studentAverage < 69 ){
letterGrade = 'F';
}
return letterGrade;
}
void writeGrade(int studentAverage, int letterGrade){
/*
Use the variable gradeAverage to determin the the grade was a A, B, C, D, or F. Then open of the person's file, and write the grade.
*/
findLetter(/*int studentAverage, char letterGrade*/);
ofstream gradeFile;
gradeFile.open ("grades.txt", ios::out | ios::app);
gradeFile << "Student: " << studentAverage << endl;
gradeFile << "Grade: " << letterGrade << endl;
gradeFile << "Average: " << studentAverage << endl << endl;
}
int theAverage(int totalAmount, int gradeNumber, int studentAverage){
/*
Here we will average all the numbers together inside of a for loop
*/
studentAverage = totalAmount / gradeNumber;
return studentAverage;
}
what should I do if I need more than one thing returned? or all variables still there and "saved"?
Do you even read what the compiler is telling you?
If you need to return several objects, you can either wrap them inside an object (struct/class), and return an instance of that object, or you can pass your variables as output references, like this:
Code:
void myFunction(int input, int& outut)
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 28th, 2010, 03:30 AM
#25
Re: Simple GradeSystem
Originally Posted by GCDEF
Not sure I really see the advantage to that. It doesn't save any lines of code, isn't any more efficient and is less readable.
The advantages of that compared to an if/else if cascade is that the relevant information is given in one single array. If you want to change the number of grades or the ranges for a single grade you have to change that array and nothing else.
The code has 8 statements (brackets not counted). The original code has 11 statements. So the assertment 'it doesn't save any lines of code' is wrong. Efficiency IMO is not the main goal of such a function but surely a loop is a far better coding technique than repeating similar statements multiple times. I also think that packing all relevant information to an array improves readability. Maybe the following code would point out that aspect more clearly:
Code:
char findLetter(int grade)
{
struct Grade
{
int low;
int high;
char grade;
};
enum { LOW = 0, HIGH = 120 };
static Grade grades[] =
{
{ LOW, 69, 'F', },
{ 70, 75, 'E', },
{ 76, 80, 'D', },
{ 81, 85, 'C', },
{ 86, 95, 'B', },
{ 96, HIGH, 'A', },
};
char letter = '?';
int nGrades = sizeof(grades)/sizeof(grades[0]);
for (int i = 0; i < nGrades; ++i)
{
if (grade >= grades[i].low && grade <= grades[i].high)
{
letter = grades[i].grade;
break;
}
}
return letter;
}
Though now it is more lines, less efficient, it is well readable, less error-prone and good quality (and tested).
Regards, Alex
-
July 28th, 2010, 03:53 AM
#26
Re: Simple GradeSystem
Originally Posted by itsmeandnobodyelse
The advantages of that compared to an if/else if cascade is that the relevant information is given in one single array. If you want to change the number of grades or the ranges for a single grade you have to change that array and nothing else.
The code has 8 statements (brackets not counted). The original code has 11 statements. So the assertment 'it doesn't save any lines of code' is wrong. Efficiency IMO is not the main goal of such a function but surely a loop is a far better coding technique than repeating similar statements multiple times. I also think that packing all relevant information to an array improves readability. Maybe the following code would point out that aspect more clearly:
Code:
char findLetter(int grade)
{
struct Grade
{
int low;
int high;
char grade;
};
enum { LOW = 0, HIGH = 120 };
static Grade grades[] =
{
{ LOW, 69, 'F', },
{ 70, 75, 'E', },
{ 76, 80, 'D', },
{ 81, 85, 'C', },
{ 86, 95, 'B', },
{ 96, HIGH, 'A', },
};
char letter = '?';
int nGrades = sizeof(grades)/sizeof(grades[0]);
for (int i = 0; i < nGrades; ++i)
{
if (grade >= grades[i].low && grade <= grades[i].high)
{
letter = grades[i].grade;
break;
}
}
return letter;
}
Though now it is more lines, less efficient, it is well readable, less error-prone and good quality (and tested).
Regards, Alex
While I see what you are getting at, It seems un-necessarily complicated. I'm fairly certain a standard array + binary_search would be simpler/easier to implement/safer/easier to maintain/easier to understand/faster.
Or heck, a (static const) map, and lower_bound. This solution is not only fast, it is very easy to implement, and very easy to understand.
But now I think we are staying from the subject
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 28th, 2010, 02:02 PM
#27
Re: Simple GradeSystem
Originally Posted by itsmeandnobodyelse
I hope, you already achieved to solve some of the errors above by applying the advice you got .. but if not I will give you samples of the correct statements to solve each single error:
Please delete your post. He derives no benefit from you doing it for him. Perhaps you noticed most of us here could easily correct his mistakes, but chose to give guidance instead of doing the work. That's how this board works. So what, he hands in his (your) assignment having learned nothing. How is that helping? Will you be here to do his next assignment too?
Last edited by GCDEF; July 28th, 2010 at 02:05 PM.
-
July 28th, 2010, 02:02 PM
#28
Re: Simple GradeSystem
Originally Posted by itsmeandnobodyelse
...The code has 8 statements (brackets not counted). The original code has 11 statements. So the assertment 'it doesn't save any lines of code' is wrong. Efficiency IMO is not the main goal of such a function but surely a loop is a far better coding technique than repeating similar statements multiple times. I also think that packing all relevant information to an array improves readability...
Consider this:
Code:
char findLetter(int grade)
{
static char LetterGrade[] = "F...FD...DC...CB...BA...A";
return LetterGrade[grade];
}
The ellipsis, of course, must be replaced with appropriate number of F’s, D’s, etc.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 28th, 2010, 02:06 PM
#29
Re: Simple GradeSystem
Originally Posted by VladimirF
Consider this:
Code:
char findLetter(int grade)
{
static char LetterGrade[] = "F...FD...DC...CB...BA...A";
return LetterGrade[grade];
}
The ellipsis, of course, must be replaced with appropriate number of F’s, D’s, etc.
Good idea. You could further improve it by, instead of requiring one to manually count letters when creating the char array, instead populating a vector<char> using Boost.Assign's repeat() function.
-
July 28th, 2010, 02:07 PM
#30
Re: Simple GradeSystem
Originally Posted by itsmeandnobodyelse
...All said leads to
Code:
if ( studentAverage > 94 ){
letterGrade = 'A';
}
else if ( 94 >= studentAverage && studentAverage > 85 ){
letterGrade = 'B';
}
...
The first compare in the “else if” statement is redundant: you already know that studentAverage is less than or equal to 94.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|