I need help coding the Input Function..any takers??


the code looks like this:

// prototype
int enterGrade(); // prompts user to enter grade 0-100

const int NUMBER_GRADES = 5;

int grade;
int totalPoints;
double averagePoints;

// Enter the grades
for (int index = 0; index < NUMBER_GRADES; index++)
{
// function call
grade = enterGrade();

// accumulate total points
totalPoints += grade;
}



Now my job is to find out the Input Function - code the enterGrade() function?
So you want them to enter one grade here - and validate that it's between 0 and 100 before

1
2
3
4
5
ask user to enter grade (0-100)
//validate input
while the grade is not a valid input
--ask user to reenter grade
return grade


You'll want to initialize variables you're going to be using to accumulate sums, otherwise they'll start off with some unknown garbage value.

Your totalPoints NUMBER_GRADES and totalPoints variables are both integers - just something I'm wondering about since you have your averagePoints variable defined as a double. If you divide two integers, you'll get an integer result.
Last edited on
@rico4444

This project shouldn't be too hard, even for a beginner. The only thing I would point out to you, is you should initialize TotalPoints in the beginning, as 0. Otherwise, who knows what values you'll have when you take the value of TotalPoints and add grade to it.

In the enterGrade function, just verify that whatever the user inputs is greater-than-equals, zero, and less-than-equals, 100. Then return the value of that input.
Last edited on
so would like the following be correct??


int enterGrade()
{

int grade;
cout << "Enter grade 0-100:";
cin >> grade;

return grade;
}

can someone let me know if im at least in the right direction..??
Cuz to be completely honest I dont know shit about programming, /: I'm a network major and there making me take this course for my degree and I cant stand it..!!! /:
Passing is a must for me though so whoever can kind of school me through this I'd gladly appreciate it, I warn you guys I can be a slow learner at times...bare with me..
or would it maybe look more like this in the beginning ?

int enterGrade( >=0 == <=100 )


ugh....!!! feel soo damn lost..!!!
In your first example, yes, you are returning the grade that was inputted, but you never made sure that it was ONLY a grade from 0 to 100. If someone type 243 for a score, that would be what was returned.

Try something more like this..

1
2
3
4
5
6
7
8
9
10
11
// pseudo code -- Will not compile
declare grade to be int and less than than 0
do
print out request to user to input between 0 and 100
cin grade
//AN OPTION if you want
if grade is NOT between specified amounts, inform user.
 
while (grade less than 0 or grade over 100); 
 
return grade
Last edited on
int enterGrade()
{

int grade;

grade = >0 == <100;

cout << "Enter grade 0-100:";
cin >> grade;

return grade;
}

Is this a little better..? If im still off please just show me an actual Input Function so I can get the idea better rather than the pseudo-code thanks WhiteNite1 your help is gladly appreciated..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <limits>

int enterGrade()
{
    using std::cin;
    using std::cout;

    int grade;

    cout << "Enter a grade.\n> ";

    //  while:
    //     input extraction is not successful OR
    //           |            grade is less than 0 OR
    //           |                |         grade is greater than 100.
    //      _____|_______      ___|___      ____|____
    //     |             |    |       |    |         |
    while (!(cin >> grade) || grade < 0 || grade > 100)
    {
        cout << "Invalid input.  Try again.\n> ";

        // reset the error status of and clear any erroneous input from 
        // the stream:
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    return grade;
}
Last edited on
Topic archived. No new replies allowed.