You are here:

C++/Simple program i just cant get

Advertisement


Question
I have an online class and i just cant get the hang of how this is supposed to work, ive tried some other peoples suggestions but i still keep coming up with 6 errors and one warning.  Ill post the errors and warning messages below.  Any help is greatly appreciated

/*************************************
/* File: Project2 */
/* */
/* Created by: Andrew Myers */
/* Date: give 7Sept2009 */
/* */
/* Program to determine course grade */
/* */
/* Inputs: (keyboard) */
/* 1. Float - weighted total points (<= 100) */
/* */
/* Output: */
/* letter grade using pseudocode grading policy */
/* */
/* Algorithm: Comparisons using if-then-else */
/* */
/*************************************
#include <iostream>

using namespace std ;

int main()
{
float score;
char grade;


// read in total score

cout << endl;
cout << "Enter total score (float, must be <= 100) : " ;
cin >> score;

if (score >= 85) then
grade = A else
if (score <=85) and (score >= 75) then
grade = 'B' else
if (score <= 75) and (score >=65) then
grade = 'C' else
if (score <= 65) and (score >=55) then
grade = 'D' else
if (score < 55) then
grade = 'F'
endl;




// display the result

cout << endl ;
cout << "Your grade for CMIS 102 is: " << grade << endl ;

return (0); // terminate with success
}


errors and warnigns:
1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
1>Compiling...
1>Project2.cpp
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(35) : error C2065: 'then' : undeclared identifier
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(35) : error C2146: syntax error : missing ';' before identifier 'grade'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(35) : error C2065: 'A' : undeclared identifier
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(35) : error C2143: syntax error : missing ';' before 'else'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(35) : error C2181: illegal else without matching if
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(37) : error C2146: syntax error : missing ';' before identifier 'then'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(36) : error C3861: 'and': identifier not found
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(37) : error C2065: 'then' : undeclared identifier
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(37) : error C2146: syntax error : missing ';' before identifier 'grade'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(37) : error C2143: syntax error : missing ';' before 'else'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(37) : error C2181: illegal else without matching if
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(39) : error C2146: syntax error : missing ';' before identifier 'then'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(38) : error C3861: 'and': identifier not found
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(39) : error C2065: 'then' : undeclared identifier
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(39) : error C2146: syntax error : missing ';' before identifier 'grade'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(39) : error C2143: syntax error : missing ';' before 'else'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(39) : error C2181: illegal else without matching if
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(41) : error C2146: syntax error : missing ';' before identifier 'then'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(40) : error C3861: 'and': identifier not found
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(41) : error C2065: 'then' : undeclared identifier
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(41) : error C2146: syntax error : missing ';' before identifier 'grade'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(41) : error C2143: syntax error : missing ';' before 'else'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(41) : error C2181: illegal else without matching if
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(43) : error C2065: 'then' : undeclared identifier
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(43) : error C2146: syntax error : missing ';' before identifier 'grade'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(44) : error C2146: syntax error : missing ';' before identifier 'endl'
1>c:\users\andrew\documents\visual studio 2008\projects\project2\project2\Project2.h(44) : warning C4551: function call missing argument list
1>Build log was saved at "file://c:\Users\Andrew\Documents\Visual Studio 2008\Projects\Project2\Project2\Debug\BuildLog.htm"
1>Project2 - 26 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Answer
Visual Studio Help System
Visual Studio Help Sys  
Andrew, the problem is that C/C++ does not use the words "then" or "and" as part of the "if" construct. Since you are using Visual Studio 2008, have a look at the C language reference for examples of how to use the language. Start the help system using the F1 key and look at the sections in my screenshot. Make sure the filter in your help system is set to Visual C++.

The corrected program is below.
Make sure you match the starting of comments (/*) with the ending of comments (*/)

Best regards.


#include <iostream>

using namespace std ;

int main()
{
   float score;
   char grade;


   // read in total score

   cout << endl;
   cout << "Enter total score (float, must be <= 100) : " ;
   cin >> score;

   if (score >= 85)
       grade = 'A'; else
       if (score <=85  && score >= 75)
         grade = 'B'; else
         if (score <= 75 && score >=65)
         grade = 'C'; else
         if (score <= 65 && score >=55)
         grade = 'D'; else
         if (score < 55)
         grade = 'F';


   // display the result

   cout << endl ;
   cout << "Your grade for CMIS 102 is: " << grade << endl ;

   return (0); // terminate with success
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.