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 05-Mar-2007, 21:52
mimi78 mimi78 is offline
New Member
 
Join Date: Mar 2007
Posts: 3
mimi78 is on a distinguished road

Need help with if else statements and functions


I am very new to C++. I need someone to look at my code and tell me if I am anywhere close to getting this assignment right and if not, point me the right direction. Thanks!

Here is the assignment:

Your program shall read three integer test scores (between 0 and 100) from the keyboard. The program shall call a function (computeGrade) that calculates and returns a student’s grade based on the following requirements:

a. If the average score is 90% or more, the grade is ‘A’.
b. If the average score is 70% or more and less than 90%, examine the third score. If the third score is more than 90%, the grade is ‘A’, otherwise the grade is ‘B’.
c. If the average score is 50% or more and less than 70%, examine the average of the second and third scores. If the average of the two is greater than 70%, the grade is ‘C’, otherwise, it is ‘D’.
d. If the average score is less than 50% then the grade is ‘F’.

The program’s main shall contain only call statements. At least three subfunctions (with the following names) are required:

1. readScore – Reads three test scores
2. determineGrade – Determine the student’s grade
3. printResults - Outputs the results to the computer’s monitor (screen)

5. Your program shall display the following text with answers in this format (Bold numbers are typical responses.):

Student’s Test Scores: 75, 80, 95


Here's what I've written so far

CPP / C++ / C Code:
//Preprocessing directives 

#include <iostream>

using namespace std; 


void readScore (int num1, int num2, int num3)
{ return (int num1, int num2, int num3, ;
}
char letterGrade

int average (( num1 + num2 + num3)/3)
 void determineGrade (char x);
 
 void printResults (num1, num2, num3 );
 
 int computeGrade 
 
 (if (finalNumericGrade >= 90) {
                finalLetterGrade = 'A';
            }                   //End if

            else if ((finalNumericGrade >= 70) && (finalNumericGrade < 90) && 
            (num3 < 90)){
                finalLetterGrade = 'B';
            }                   //End if 

            else if ((finalNumericGrade >= 50) && (finalNumericGrade < 70)) &&
            (((num2 + num3)/2) >= 70) 
            {   finalLetterGrade = 'C';
            }                   //End if

            else if ((finalNumericGrade >= 50) && (num2 + num3)/2 < 70)) {
                finalLetterGrade = 'D';
            }                   //End if 

            else if (finalNumericGrade < 50) {
                finalLetterGrade = 'F';
            }                   //End if 


//Main()

int main() 
{ 
int num1
int num2
int num3

cout << "Enter three numeric grades" ;
cin >> num1, num2, num3;

Please help me!
  #2  
Old 06-Mar-2007, 09:16
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: Need help with if else statements and functions


Yikes. There's quite a bit wrong with this program:

1. There's no return statement or ending bracket for main.
2. finalNumericGrade and finalLetterGrade are never declared. letterGrade is never initialized or used.
3. Semicolons, semicolons, semicolons. Each declaration (int num1, etc.) needs to have a semicolon after it.
4. Your formatting is a mess. What's with the opening parentheses for the readScore return statement? Why is there a comma, then a semicolon at the end of that line? Why are you trying to return more than one value, especially from a void function? Is letterGrade a function? A variable declaration?
5. When you're initializing average, you need an equals sign. It should be "int average = ((num1 + num2 + num3) / 3);"
6. Is printResults supposed to be a function? Why do you have the prototype inside another function?
7. Your if statements look good in general, but they could be improved. In your elseif statements, you don't need to check that finalNumericGrade is < 90, 70, etc. In order for the program to get to that statement, (finalNumericGrade >= 90) must be false. In other words, checking if finalNumericGrade is less than 90 afterwords is redundant.

Anyway, your functions are very unclear. It's hard to tell where one begins or ends, and what it's supposed to accomplish. Why don't you tell us the functions you plan to use, what they take as arguments, and what they're supposed to do. That will help us point you in the right direction.
__________________
Gamer_2k4
  #3  
Old 07-Mar-2007, 17:55
mimi78 mimi78 is offline
New Member
 
Join Date: Mar 2007
Posts: 3
mimi78 is on a distinguished road

Re: Need help with if else statements and functions


The functions are
readscore - which should get three integer test scores from the keyboard.
CPP / C++ / C Code:
void readScore (int num1, int num2, int num3)
{cout << "Enter three numeric grades" ;
cin >> num1, num2, num3;
return;
}

determineGrade - should calculate and return the students grade.
CPP / C++ / C Code:
void determineGrade ()
 {
     if (finalNumericGrade >= 90) {
                finalLetterGrade = 'A';
            }                   //End if

            else if ((finalNumericGrade >= 70) && (finalNumericGrade < 90) && 
                (num3 < 90))
                {
                finalLetterGrade = 'B';
                 }                   //End if 

            else if ((finalNumericGrade >= 50) && (finalNumericGrade < 70)) &&
            (((num2 + num3)/2) >= 70) ;
            {   
                finalLetterGrade = 'C';
            }                   //End if

            else if ((finalNumericGrade >= 50) && (num2 + num3)/2 < 70)) 
            {
                finalLetterGrade = 'D';
            }                   //End if 

            else if (finalNumericGrade < 50)
             {
                finalLetterGrade = 'F';
            }                   //End if 

return;
}

printResults - which should output to the monitor

CPP / C++ / C Code:
void printResults (int num1, int num2, int num3, int finalLetterGrade );
  #4  
Old 07-Mar-2007, 18:44
mimi78 mimi78 is offline
New Member
 
Join Date: Mar 2007
Posts: 3
mimi78 is on a distinguished road

Re: Need help with if else statements and functions


I am also getting an error message "expected primary - expression before "else". I am not sure what that means.
  #5  
Old 07-Mar-2007, 19:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with if else statements and functions


Quote:
Originally Posted by mimi78
I am also getting an error message "expected primary - expression before "else". I am not sure what that means.

Kind of hard to spot, and the message isn't particularly enlightening (unless, like me, you have seen it about a million times) but:

CPP / C++ / C Code:
            else if ((finalNumericGrade >= 50) && (finalNumericGrade < 70)) &&
            (((num2 + num3)/2) >= 70) /* ; <=== Lose the semicolon */

The semicolon really screws things up here.

(I think you could simplify things at least a little: When you get to the final "else if", is there any other possibility? I mean is it possible that the grade is not less than 50 at this point? If so, then you need some other assignment. If not, then why not eliminate the last if? (In other words change it to
CPP / C++ / C Code:
    else {
        finalLetterGrade = 'F';
    }

Or something like that.

Regards,

Dave
 
 

Recent GIDBlogPython ebook 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
Programs failing to execute...? netnut C++ Forum 5 12-Jan-2006 10:39
First prog, using functions that pass values... boousaf C Programming Language 2 27-Sep-2005 16:10
Pointers to Functions (was Saving some if statements) TekiFreek C++ Forum 17 17-Jun-2004 11:25
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 17:59
JavaScript Tutorial Part 1 pcxgamer Web Design Forum 2 01-Dec-2003 09:16

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

All times are GMT -6. The time now is 18:12.


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