Looking for input

Just looking on some input for this code that I wrote, I'm looking to improve upon it. I compiled it on visual studio. From testing, I believe that the code is doing everything that I wanted it to do. Prompts the user for their 3 test scores and then gives them their average. It also checks for user error for a grade that isn't obtainable such as 110.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
 
double total = 0;
double final = 0;
 
//**Display message**
void welcome_message() {
   
    std::cout << "Hello user, we are going to compute your grades today for you\n";
    //User prompt
    std::cout << "What should I call you?\n";
 
    std::string name;
    //Grabs the users name with white space
    std::getline(std::cin, name);
    //Displays users name
    std::cout << "Hello, " << name << " let's get started.\n";
}
//Function prompts user for 3 grades, and calculates the final average, then returns the value
int grades() {
 
    //Declarations for three grades
    double g1, g2, g3 = 0;
    std::cout << "Please enter in your first grade 0-100\n";
    std::cin >> g1;
    //Checking the values of user input to ensure that they are within range
    while (g1 > 100 || g1 < 0) {
 
        std::cout << "Please enter in your first grade 0-100, whole numbers only please.\n";
        std::cin >> g1;
 
        if (g1 < 100 && 0 > g1) {
           
            return g1;
            break;
        }
    }
 
    std::cout << "Please enter in your second grade.\n";
    std::cin >> g2;
    //Checking the values of user input to ensure that they are within range
    while (g2 > 100 || g2 < 0) {
 
        std::cout << "Please enter in your second grade 0-100, whole numbers only please.\n";
        std::cin >> g2;
 
        if (g2 < 100 && g2 > 0) {
           
            return g2;
            break;
        }
    }
 
    std::cout << "Please enter in your third grade.\n";
    std::cin >> g3;
    //Checking the values of user input to ensure that they are within range
    while (g3 > 100 || g3 < 0) {
 
        std::cout << "Please enter in your third grade 0-100, whole numbers only please.\n";
        std::cin >> g3;
 
        if (g3 < 100 && g3 > 0) {
           
            return g3;
            break;
        }
    }
 
   
    //Grade calculations
    total = g1 + g2 + g3;
    final = total / 3;
 
    return final;
 
}
 
int main() {
    //Function calls
    welcome_message();
    grades();
    //Setting grade to show 2 decimal places
    std::cout << std::setprecision(2) << std::fixed << std::showpoint;
    std::cout << "Your final grade is: " << final;
    std::cout << "%" << std::endl;
 
    _getch();
 
    return 0;
}.
Last edited on
Looking for input

What kind of input are you looking for?
Last edited on
Do you see any blatant beginner mistakes in what I wrote? I'm retaking a class for programming, and I was iffy on functions and the use of while statements. As far as my while and if statements, I need to return the variables before it breaks out of the while statement because then it won't return my variables correct?

You are learning C++, you should not use global variables (unless specified by the assignment)

1
2
double total = 0;
double final = 0;


Instead of using global variables, you can use reference parameters to pass variables from a function to another.
Is that because every function that I have then has access to the variables and it can be confusing to see which function is using it when first starting out?
You have three pieces of code like these :
1
2
3
4
5
6
7
8
9
10
11
while (g1 > 100 || g1 < 0) {
 
        std::cout << "Please enter in your first grade 0-100, whole numbers only please.\n";
        std::cin >> g1;
 
        if (g1 < 100 && 0 > g1) {
           
            return g1;
            break;
        }
    }


Sorry, but I have to say they are awfully hard to read. Also, try not to use return statement in a while loop. Please just end the while loop normally (using break or without this keyword it would be better).

0 > g1
I strongly advise against this code style. When comparing two values (one is a variable (g1) and one is a value (0)), always put the variable on the left side, and put the value on the right side. So the code would become :
g1 > 0

Last but not least, make use of these comparison operators : >=, <=
Last edited on
I had tried to use those comparison operators but I believe that it had tossed me out an error. Thank you though for your input.
I had the variables on the left side but I forgot to change them back when I was troubleshooting the comparison operator issue, having the variable on the left side though is just proper coding etiquette though I take it?
Do what you have to do. After you come back with better code, I will be willing to do more code review.
closed account (LA48b7Xj)
Is that because every function that I have then has access to the variables and it can be confusing to see which function is using it when first starting out?

Pretty much, if you can limit the code that has access to a variable it's easier to know which code is modifying it. Even if you are an expert, you can only focus on so many things at one time.

double g1, g2, g3 = 0;

This might not be an error but I don't think it does what you intended. g3 is initialized to 0, g1 and g2 are left undefined.

You could write ...
double g1 = 0, g2 = 0, g3 = 0;

Or maybe even better, write 3 separate lines
1
2
3
double g1 = 0;
double g2 = 0;
double g3 = 0;

It's a good idea to define variables just before they are used, that way you always have a meaningful value to put in them straight away. And it limits some possible mistakes you could make (like if you wrote g3 where you meant to write g1).

Do you notice in your grades function how a lot of the code looks like a copy/paste job. The only thing that is different is the text message and the variable, so you could write a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double input_grade(const std::string& grade_str)
{
    std::cout << "Please enter in your " << grade_str << " grade 0-100\n";

    double grade_num;
    std::cin >> grade_num;

    while(grade_num > 100 || grade_num < 0)
    {
        std::cout << "Please enter in your " << grade_str << " grade 0-100, whole numbers only please.\n";
        std::cin >> grade_num;
    }

    return grade_num;
}

Now you only have to check one piece of code for errors, instead of three parts.

You could use that function like this...
1
2
3
4
5
6
7
8
double grades()
{
    const double g1 = input_grade("first");
    const double g2 = input_grade("second");
    const double g3 = input_grade("third");

    return (g1 + g2 + g3) / 3;
}
Last edited on
Thank you this helped a lot, and as far as when I declared the doubles g1, g2 and g3 I had meant to set each equal to 0. It was explained to me by my professor that this is best to do when you first declare a variable that will be later used to hold user input.
Topic archived. No new replies allowed.