Desperately need help with this for finals! Instructor says my code is correct, but I get Syntax Errors. Please help! Also, does anyone know of a "syntax error scrubber program" anywhere like the w3c has for HTML and CSS?
PROGRAM:
//*********************************************************
// K. Tavane Griffith
// COP1000.052
// 04/21/2009
// This is the grade calculator program. The highest of the first two grades
// will be added to the third grade to determine the numeric grade average
// for the course.
//*********************************************************
#include<iostream>//required to perform C++ stream I/O functions
#include<iomanip>//required for paramaterized stream manipulators
#include<string>//required to access string functions
using namespace std;//for accessing C++ Standard Library Members
int main()
{
//define variables
int score1;//stores the value of the first test score
int score2;//stores the value of the second test score
int score3;//stores the value of the third test score
int average = 0;//the numeric grade average for the course
string studentName;//user enters the student's name
cout<<"Welcome to the grade calculator. You will input three test scores.\nThe highest of the first 2 grades and the\nthird grade will be added together\nto determine the numeric grade average for the course\nEach test score has a maximum of 50 points."<<endl;//welcome statement to the user
cout<<"Enter the student's name:";//prompts the user inputs the student name
getline( cin, studentName );
if ( ( studentName.size ()==0 ))
{
cout << "\nError:Enter a name:\n "<<endl;//prompts the user to enter a name if they didn't on the first prompt
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
getline ( cin,studentName );//inputs studentName
}
cout<<"Please enter test score 1:";//prompts the user to input the first test score
cin>>score1;//inputs score1
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
else if(score1<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number, please enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score1;//inputs score1
}
cout<<"\nPlease enter test score 2:";//prompts the user to enter the second test score
cin>>score2;//get second test score
if (!cin || score2<0||score2 >50)//logical operation/calculation
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//error message to user, prompts user to try again
}
else if (score2<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50,\nplease enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score2;//inputs score2
}
cout<<"\nPlease enter test score 3:";//prompts the user to enter the third test score
cin>>score3;//get third score
if (!cin || score3<0||score3 >50)//logical operation/calculation
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
else if (score3<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score3;//inputs score3
}
if (score1>score2)//begin logical operation/calculation
average = score1+score3;//gets calculation
else //part 2 of logical calculation/operation
average=score2+score3;//gets calculation
cout<<"\nThe average for the course is = ";<<average;//displays message
cout<<studentName;<<"has earned an"
if (average>=90)//begin logical calculation
cout<<"an A for the course.";//displays result of logical calculation to user
else if (average>=80)//logical calculation
cout<<"a B for the course.";//displays result of logical calculation to user
else if (average>=70)//logical calculation
cout<<" a C for the course.";//displays result of logical calculation to user
else if (average>=60)//logical calculation
cout<<" a D for the course.";<<endl;//displays result of logical calculation to user
else //logical calculation
cout<<" an F for the course.";<<endl;//displays result of logical calculation to user
}
cout<<"\nThank you for using this program.";<<endl;//displays end message to user
return 0; // indicate that program ended successfully
} // end function main
Syntax Errors:
k:\intro2programming\project4\p4ktg\p4ktg.cpp(119) : error C2143: syntax error : missing ';' before '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(120) : error C2143: syntax error : missing ';' before '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(125) : error C2181: illegal else without matching if
k:\intro2programming\project4\p4ktg\p4ktg.cpp(135) : error C2143: syntax error : missing ';' before '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(137) : error C2181: illegal else without matching if
k:\intro2programming\project4\p4ktg\p4ktg.cpp(139) : error C2143: syntax error : missing ';' before '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(142) : error C2143: syntax error : missing ';' before '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(142) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
k:\intro2programming\project4\p4ktg\p4ktg.cpp(142) : error C2059: syntax error : '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(144) : error C2059: syntax error : 'return'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(146) : error C2059: syntax error : '}'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(146) : error C2143: syntax error : missing ';' before '}'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(146) : error C2059: syntax error : '}'
Wow - that helped a lot! Thanks! I now just have one syntax error to get rid of:
New Code:
//*********************************************************
// K. Tavane Griffith
// COP1000.052
// 04/22/2009
// This is the grade calculator program. The highest of the first two grades
// will be added to the third grade to determine the numeric grade average
// for the course.
//*********************************************************
#include<iostream>//required to perform C++ stream I/O functions
#include<iomanip>//required for paramaterized stream manipulators
#include<string>//required to access string functions
using namespace std;//for accessing C++ Standard Library Members
int main()
{
//define variables
int score1;//stores the value of the first test score
int score2;//stores the value of the second test score
int score3;//stores the value of the third test score
int average = 0;//the numeric grade average for the course
string studentName;//user enters the student's name
cout<<"Welcome to the grade calculator. You will input three test scores.\nThe highest of the first 2 grades and the\nthird grade will be added together\nto determine the numeric grade average for the course\nEach test score has a maximum of 50 points."<<endl;//welcome statement to the user
cout<<"Enter the student's name:";//prompts the user inputs the student name
getline( cin, studentName );
if ( ( studentName.size ()==0 ))
{
cout << "\nError:Enter a name:\n "<<endl;//prompts the user to enter a name if they didn't on the first prompt
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
getline ( cin,studentName );//inputs studentName
}
cout<<"Please enter test score 1:";//prompts the user to input the first test score
cin>>score1;//inputs score1
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
else if(score1<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number, please enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score1;//inputs score1
}
cout<<"\nPlease enter test score 2:";//prompts the user to enter the second test score
cin>>score2;//get second test score
if (!cin || score2<0||score2 >50)//logical operation/calculation
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//error message to user, prompts user to try again
}
else if (score2<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50,\nplease enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score2;//inputs score2
}
cout<<"\nPlease enter test score 3:";//prompts the user to enter the third test score
cin>>score3;//get third score
if (!cin || score3<0||score3 >50)//logical operation/calculation
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
else if (score3<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score3;//inputs score3
}
if (score1>score2)//begin logical operation/calculation
average = score1+score3;//gets calculation
else //part 2 of logical calculation/operation
average=score2+score3;//gets calculation
cout<<"\nThe average for the course is = "<<average;//displays message
cout<<studentName<<"has earned";
<<if (average>=90)//begin logical calculation
<<"an A for the course."//displays result of logical calculation to user
<<else if (average>=80)//logical calculation
<<"a B for the course."//displays result of logical calculation to user
<<else if (average>=70)//logical calculation
<<" a C for the course."//displays result of logical calculation to user
<<else if (average>=60)//logical calculation
<<" a D for the course."//displays result of logical calculation to user
<<else //logical calculation
<<" an F for the course.";//displays result of logical calculation to user
cout<<"\nThank you for using this program."<<endl;//displays end message to user
return 0; // indicate that program ended successfully
} // end function main
P4ktg.cpp
k:\intro2programming\project4\p4ktg\p4ktg.cpp(121) : error C2143: syntax error : missing ';' before '<<'
Build log was saved at "file://k:\Intro2Programming\Project4\P4ktg\Debug\BuildLog.htm"
P4ktg - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
//*********************************************************
// K. Tavane Griffith
// COP1000.052
// 04/22/2009
// This is the grade calculator program. The highest of the first two grades
// will be added to the third grade to determine the numeric grade average
// for the course.
//*********************************************************
#include<iostream>//required to perform C++ stream I/O functions
#include<iomanip>//required for paramaterized stream manipulators
#include<string>//required to access string functions
using namespace std;//for accessing C++ Standard Library Members
int main()
{
//define variables
int score1;//stores the value of the first test score
int score2;//stores the value of the second test score
int score3;//stores the value of the third test score
int average = 0;//the numeric grade average for the course
string studentName;//user enters the student's name
cout<<"Welcome to the grade calculator. You will input three test scores.\nThe highest of the first 2 grades and the\nthird grade will be added together\nto determine the numeric grade average for the course\nEach test score has a maximum of 50 points."<<endl;//welcome statement to the user
cout<<"Enter the student's name:";//prompts the user inputs the student name
getline( cin, studentName );
if ( ( studentName.size ()==0 ))
{
cout << "\nError:Enter a name:\n "<<endl;//prompts the user to enter a name if they didn't on the first prompt
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
getline ( cin,studentName );//inputs studentName
}
cout<<"Please enter test score 1:";//prompts the user to input the first test score
cin>>score1;//inputs score1
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
else if(score1<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number, please enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score1;//inputs score1
}
cout<<"\nPlease enter test score 2:";//prompts the user to enter the second test score
cin>>score2;//get second test score
if (!cin || score2<0||score2 >50)//logical operation/calculation
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//error message to user, prompts user to try again
}
else if (score2<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50,\nplease enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score2;//inputs score2
}
cout<<"\nPlease enter test score 3:";//prompts the user to enter the third test score
cin>>score3;//get third score
if (!cin || score3<0||score3 >50)//logical operation/calculation
{
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
else if (score3<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score3;//inputs score3
}
if (score1>score2)//begin logical operation/calculation
average = score1+score3;//gets calculation
else //part 2 of logical calculation/operation
average=score2+score3;//gets calculation
cout<<"\nThe average for the course is = "<<average;//displays message
cout<<studentName<<"has earned";
if (average>=90)//begin logical calculation
<<"an A for the course."//displays result of logical calculation to user
else if (average>=80)//logical calculation
<<"a B for the course."//displays result of logical calculation to user
else if (average>=70)//logical calculation
<<" a C for the course."//displays result of logical calculation to user
else if (average>=60)//logical calculation
<<" a D for the course."//displays result of logical calculation to user
else //logical calculation
<<" an F for the course.";//displays result of logical calculation to user
cout<<"\nThank you for using this program."<<endl;//displays end message to user
return 0; // indicate that program ended successfully
} // end function main
k:\intro2programming\project4\p4ktg\p4ktg.cpp(123) : error C2143: syntax error : missing ';' before '<<'
k:\intro2programming\project4\p4ktg\p4ktg.cpp(123) : warning C4390: ';' : empty controlled statement found; is this the intent?
Build log was saved at "file://k:\Intro2Programming\Project4\P4ktg\Debug\BuildLog.htm"
P4ktg - 1 error(s), 1 warning(s)
This tells you that line 123 is the problem. Rather than paste your whole source without any formatting and expect us to sift through all of it to find the problem for you, why not just paste line 123 along with the one or two lines above and below it (or better yet, look specifically at line 123 to see if you can spot the problem for yourself).
Sorry guys it's my first time posting in this forum. I still can't identify the error no matter how hard I try to manipulate the code:
cout<<"\nThe average for the course is = "<<average;//displays message
cout<<studentName<<"has earned";
if (average>=90)//begin logical calculation
LINE 123-----------> <<"an A for the course."//displays result of logical calculation to user
Error 1 error C2143: syntax error : missing ';' before '<<' e:\intro2programming\project4\p4ktg\p4ktg.cpp 123
Warning 2 warning C4390: ';' : empty controlled statement found; is this the intent? e:\intro2programming\project4\p4ktg\p4ktg.cpp 123
This is my best effort yet. The lack of colors I would imagine is just a copy/paste issue with the forum. I need to turn this in before noon today.....I realize for you guys it's probably an easy fix but I'm a beginner and just can't spot it.
Thanks for the command. cout<<"An A for the course.";
means I would have to add the semi colon to B,C,D & F and I am not getting error messages on those lines. I tried that attempt and it gave me more error messages. Also I did clean up my brackets....
You need a cout every time you want to output something. Simply using the insertion operator will not work. You
also need a semicolon after every line. In short, you do need to add these items to B, C, D, and F.
//*********************************************************
// K. Tavane Griffith
// COP1000.052
// 04/22/2009
// This is the grade calculator program. The highest of the first two grades
// will be added to the third grade to determine the numeric grade average
// for the course.
//*********************************************************
#include<iostream>//required to perform C++ stream I/O functions
#include<iomanip>//required for paramaterized stream manipulators
#include<string>//required to access string functions
usingnamespace std;//for accessing C++ Standard Library Members
int main()
{
//define variables
int score1;//stores the value of the first test score
int score2;//stores the value of the second test score
int score3;//stores the value of the third test score
int average = 0;//the numeric grade average for the course
string studentName;//user enters the student's name
cout<<"Welcome to the grade calculator. You will input three test scores.\nThe highest of the first 2 grades and the\nthird grade will be added together\nto determine the numeric grade average for the course\nEach test score has a maximum of 50 points."<<endl;//welcome statement to the user
cout<<"Enter the student's name:";//prompts the user inputs the student name
getline( cin, studentName );
if ( ( studentName.size ()==0 ))
{
cout << "\nError:Enter a name:\n "<<endl;//prompts the user to enter a name if they didn't on the first prompt
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
getline ( cin,studentName );//inputs studentName
}
cout<<"Please enter test score 1:";//prompts the user to input the first test score
cin>>score1;//inputs score1
if(!cin || score1<0||score1 >50)//logical operation/calculation
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
elseif(score1<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number, please enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score1;//inputs score1
cout<<"\nPlease enter test score 2:";//prompts the user to enter the second test score
cin>>score2;//get second test score
if (!cin || score2<0||score2 >50)//logical operation/calculation
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//error message to user, prompts user to try again
}
elseif (score2<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50,\nplease enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score2;//inputs score2
cout<<"\nPlease enter test score 3:";//prompts the user to enter the third test score
cin>>score3;//get third score
if (!cin || score3<0||score3 >50)//logical operation/calculation
if (!cin)
{
cout<<"\nTest scores must be a numeric value between 0 and 50, try again.";//prompts the user to try again
}
elseif (score3<0)
{
cout<<"\nTest scores cannot be less than zero or a negative number,\nplease enter your score again.";//prompts the user to try again
}
else
{
cout<<"\nTest scores cannot be greater than 50, please enter your score again.";//prompts the user to try again
}
cin.clear();//reset the read to good
cin.sync();//clear out the contents of the input buffer
cin>>score3;//inputs score3
if (score1>score2)//begin logical operation/calculation
average = score1+score3;//gets calculation
else //part 2 of logical calculation/operation
average=score2+score3;//gets calculation
cout<<"\nThe average for the course is"<<average;//displays message
cout<<studentName<<"has earned";
if (average>=90)//begin logical calculation
cout<<"an A for the course.";//displays result of logical calculation to user
elseif (average>=80)//logical calculation
cout<<"a B for the course.";//displays result of logical calculation to user
elseif (average>=70)//logical calculation
cout<<" a C for the course.";//displays result of logical calculation to user
elseif (average>=60)//logical calculation
cout<<" a D for the course.";//displays result of logical calculation to user
else //logical calculation
cout<<" an F for the course.";//displays result of logical calculation to user
cout<<"\nThank you for using this program."<<endl;//displays end message to user
return 0; // indicate that program ended successfully
} // end function main
If it stops near the point where you input score1, take a look at the code in that location. You're missing brackets for the if conditional on line 41 (and probably other locations). Without the brackets, you input score1 on line 39 and again on line 60. It's probably stopping on line 60 and waiting for input, and you weren't expecting that.