First program: Averaging 6 grades

I'm creating a grade averaging program for class that needs user input of 6 grades which are to be entered as strings and divide them by the total (6) for the average.

The instructions confuse me on what to do vs what I did to accomplish the task. My teacher wants me to use atoi or atof to convert the grades that were input as strings which I kinda get but I don't understand his example code.

"Calculation and Output Screen - should display the following: Student Number, Last Name, First Name, and six Grades, and the calculation for Grade Average. The string value for each grade will be need to be converted into an integer or double prior to be used in an arithmetic operation. This should be completed using the atoi() or atof() function immediately preceding the use in calculations. For an example, refer to the following program segment --"

1
2
3
4
5
6
7
8
9
10
11
12
13
#include < cstdlib > // included for the atoi()
    #include < string > // included for the string class
    
    int iGrade = 0;// integer variable
    string gradeStr = "XXX";// string variable
    cout<<"Enter a date: "; // prompt
    cin>>gradeStr;// data entry
    iGrade=atoi(gradeStr.data());// IMPORTANT use of atoi()
    
    // Note that gradeStr.data() creates a character array which is used by atoi() (or atof() )
    //Newer versions of C++ incorporat stoi() and stod() which are not recognized by all compilers.
    //We will use the technique above for our code.
    // The rest of the pgm." 


And here's what I think of but I guess they are not input as strings and it doesn't use the atoi thing.

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
#include <iostream>
    
    using namespace std;
    
    int main()
    {
        double grade1 = 0, grade2 = 0, grade3 = 0, grade4 = 0, grade5 = 0, grade6 = 0;
    
        cout << "Please Enter Grade #1" << endl;
        cin >> grade1;
    
        cout << "Please Enter Grade #2" << endl;
        cin >> grade2;
    
        cout << "Please Enter Grade #3" << endl;
        cin >> grade3;
    
        cout << "Please Enter Grade #4" << endl;
        cin >> grade4;
    
        cout << "Please Enter Grade #5" << endl;
        cin >> grade5;
    
        cout << "Please Enter Grade #6" << endl;
        cin >> grade6;
    
        double Average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6) / 6;
    
        cout << "Your average is: " << Average << endl;
    
        return 0;


So I could just do something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
    
    using namespace std;
    
    int main( )
    {
    string grade1 = "XXXX";
    string grade2 = "XXXX";
    string grade3 = "XXXX";
    string grade4 = "XXXX";
    string grade5 = "XXXX";
    string grade6 = "XXXX";
    }


And then what is it I do with these according to the instruction?
Last edited on
You can do the same thing for input.
Do cin >> grade1; cin >> grade2; etc.

The instructions seem ambiguous on whether to support doubles or not, but let's just assume they are supported (e.g. a grade of 85.7).

So, you take the grade1, and convert it to a double using atoi() or atof().
e.g.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string grade1;
    cin >> grade1;
    double grade1_num = atof(grade1.c_str());

    cout << "Grade as double: " << grade1_num << '\n';
}


Now simply use grade1_num instead of grade1 for your numeric calculations.
That actually helps a lot but unfortunately I am just starting so I'm confused where I am going wrong with this although I'm sure the mistakes are obvious.

This is my whole program (it is supposed to be comprised of 3 screens, splash, input, output):

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//#include "stdafx.h"
#include <iostream> // I/O operations
#include <iomanip> // Formatting functions
#include <cstdlib> // General use library functions
#include <string> // Defines dynamic string stuff

using namespace std;

int main( )
{
//--------  Start of variable and object creation ------

	string csid = "XXXX";
	string first = "XXXX";
	string last = "XXXX";

	string grade1 = "XXXX";
	string grade2 = "XXXX";
	string grade3 = "XXXX";
	string grade4 = "XXXX";
	string grade5 = "XXXX";
	string grade6 = "XXXX";
	// A variable to throw away the screen transition input

	string resp = "XXXX";

//--------  End of variable declarations -----------------------

//--------  Beginning of the Splash Screen   -------------------
system("cls");  //Calls to the OS using cls  function  MS specific
	cout << endl << endl << endl << endl; //4 blank lines
	cout << "                               " << endl;
	cout << endl << endl;
	cout << "                       " << endl;
	cout << endl << endl << endl << endl << endl << endl; // 6 blank lines
	cout << "                             " << endl;
	cout << endl << endl << endl << endl << endl << endl << endl << endl;

//--------  End of the Splash Screen  --------------------------

//Transition to next screen
	cout << "                          Press <ENTER> to Continue";
	getline(cin, resp);
	system("cls");
	//End of transition

//--------  Start of Input Screen  -----------------------------

	cout << endl << endl;
	cout << "                               COLLEGE NAME" << endl;
	cout << endl;
	cout << "                                STUDENT GRADE TRACKING SYSTEM" << endl;
	cout << endl;
	cout << "                                Input Screen" << endl;
	cout << endl;
	cout << "    Enter data as requested and press <ENTER> after each.";
	cout << endl << endl;
	cout << "     Last Name:  "; getline(cin, last);
	cout << "     First Name:  "; getline(cin, first);
	cout << endl;
	cout << "     Student ID Number:  "; getline(cin, csid);
	
	cout << "     Grade 1:  "; getline(cin, grade1);
	cout << "     Grade 2:  "; getline(cin, grade2);
	cout << "     Grade 3:  "; getline(cin, grade3);
	cout << "     Grade 4:  "; getline(cin, grade4);
	cout << "     Grade 5:  "; getline(cin, grade5);
	cout << "     Grade 6:  "; getline(cin, grade6);

//--------  End of the Input Screen  --------------------------

	//Transition to next screen
	cout << "                           Press <ENTER> to Continue";
	getline(cin, resp);
	system("cls");
	//End of transition

	//--------  Start of Output Screen  -----------------------------

cout << endl << endl;
	cout << "                               COLLEGE NAME" << endl;
	cout << endl;
	cout << "                                STUDENT GRADE TRACKING SYSTEM" << endl;
	cout << endl;
	cout << "                                Output Screen" << endl;
	cout << endl << endl << endl;
	cout << "     Student Name (First Last):  " << first;
	cout << " " << last << endl;
	cout << "     CSID:  " << csid << endl;
	
	cout << "     Grade 1:  " << grade1 << endl;
	cout << "     Grade 2:  " << grade2 << endl;
	cout << "     Grade 3:  " << grade3 << endl;
	cout << "     Grade 4:  " << grade4 << endl;
	cout << "     Grade 5:  " << grade5 << endl;
	cout << "     Grade 6:  " << grade6 << endl;
	cout << "     Total Grade Average:  " << Average << endl;
	double Average = (grade1_num + grade2_num + grade3_num + grade4_num + grade5_num + grade6_num) / 6;
	
	
	// Conversions, Decisions and Calculations
	double grade1_num = atof(grade1.c_str());
	double grade2_num = atof(grade2.c_str());
	double grade3_num = atof(grade3.c_str());
	double grade4_num = atof(grade4.c_str());
	double grade5_num = atof(grade5.c_str());
	double grade6_num = atof(grade6.c_str());
	
	//--------  End of the Output Screen  --------------------------

	//Transition to next screen
	cout << "                           Press <ENTER> to Continue";
	getline(cin, resp);
	system("cls");
	//End of transition

	return 0;
}// int main( ) 

You need to create and calculate the Average after you've created the grade1_num to grade6_num variables.
Yeah I think I understand that part but I really do not know the order in which I place what I have. I have a calculation for the Average and I have the strings at the top which are what people will input their scores into and I'm pretty sure the atof just converts them to double?

I'm confused by the logic/order of where things go.

I think it's because my teacher showed an example where his atof/atoi conversions were after his output screen.

Pretty sure this ordering is better:

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    //#include "stdafx.h"
    #include <iostream> // I/O operations
    #include <iomanip> // Formatting functions
    #include <cstdlib> // General use library functions
    #include <string> // Defines dynamic string stuff
    
    using namespace std;
    
    int main()
    {
    	//--------  Start of variable and object creation ------
    
    	string csid = "XXXX";
    	string first = "XXXX";
    	string last = "XXXX";
    
    	string grade1 = "XXXX";
    	string grade2 = "XXXX";
    	string grade3 = "XXXX";
    	string grade4 = "XXXX";
    	string grade5 = "XXXX";
    	string grade6 = "XXXX";
    
    	double grade1_num = atof(grade1.c_str());
    	double grade2_num = atof(grade2.c_str());
    	double grade3_num = atof(grade3.c_str());
    	double grade4_num = atof(grade4.c_str());
    	double grade5_num = atof(grade5.c_str());
    	double grade6_num = atof(grade6.c_str());
    
    	double Average = (grade1_num + grade2_num + grade3_num + grade4_num + grade5_num + grade6_num) / 6;
    
    	// A variable to throw away the screen transition input
    
    	string resp = "XXXX";
    
    	//--------  End of variable declarations -----------------------
    
    	//--------  Beginning of the Splash Screen   -------------------
    	system("cls");  //Calls to the OS using cls  function  MS specific
    	cout << endl << endl << endl << endl; //4 blank lines
    	cout << "                               " << endl;
    	cout << endl << endl;
    	cout << "                       " << endl;
    	cout << endl << endl << endl << endl << endl << endl; // 6 blank lines
    	cout << "                             " << endl;
    	cout << endl << endl << endl << endl << endl << endl << endl << endl;
    
    	//--------  End of the Splash Screen  --------------------------
    
    	//Transition to next screen
    	cout << "                          Press <ENTER> to Continue";
    	getline(cin, resp);
    	system("cls");
    	//End of transition
    
    //--------  Start of Input Screen  -----------------------------
    
    	cout << endl << endl;
    	cout << "                               COLLEGE NAME" << endl;
    	cout << endl;
    	cout << "                                STUDENT GRADE TRACKING SYSTEM" << endl;
    	cout << endl;
    	cout << "                                Input Screen" << endl;
    	cout << endl;
    	cout << "    Enter data as requested and press <ENTER> after each.";
    	cout << endl << endl;
    	cout << "     Last Name:  "; getline(cin, last);
    	cout << "     First Name:  "; getline(cin, first);
    	cout << endl;
    	cout << "     Student ID Number:  "; getline(cin, csid);
    
    	cout << "     Grade 1:  "; getline(cin, grade1);
    	cout << "     Grade 2:  "; getline(cin, grade2);
    	cout << "     Grade 3:  "; getline(cin, grade3);
    	cout << "     Grade 4:  "; getline(cin, grade4);
    	cout << "     Grade 5:  "; getline(cin, grade5);
    	cout << "     Grade 6:  "; getline(cin, grade6);
    
    	//--------  End of the Input Screen  --------------------------
    
    		//Transition to next screen
    	cout << "                           Press <ENTER> to Continue";
    	getline(cin, resp);
    	system("cls");
    	//End of transition
    
    	//--------  Start of Output Screen  -----------------------------
    
    	cout << endl << endl;
    	cout << "                               COLLEGE NAME" << endl;
    	cout << endl;
    	cout << "                                STUDENT GRADE TRACKING SYSTEM" << endl;
    	cout << endl;
    	cout << "                                Output Screen" << endl;
    	cout << endl << endl << endl;
    	cout << "     Student Name (First Last):  " << first;
    	cout << " " << last << endl;
    	cout << "     CSID:  " << csid << endl;
    
    	cout << "     Grade 1:  " << grade1 << endl;
    	cout << "     Grade 2:  " << grade2 << endl;
    	cout << "     Grade 3:  " << grade3 << endl;
    	cout << "     Grade 4:  " << grade4 << endl;
    	cout << "     Grade 5:  " << grade5 << endl;
    	cout << "     Grade 6:  " << grade6 << endl;
    	cout << "     Total Grade Average:  " << Average << endl;
    
    	// Conversions, Decisions and Calculations
    
    
    
    	//--------  End of the Output Screen  --------------------------
    
    	//Transition to next screen
    	cout << "                           Press <ENTER> to Continue";
    	getline(cin, resp);
    	system("cls");
    	//End of transition
    
    	return 0;
    }// int main( ) 


But now the calculation for the average just gives me 0 each time.
Last edited on
Yes, atof parses and converts a given string to a double. Line 98 is where you're attempting to calculate Average. See how it uses grade1_num, grade2_num etc.?

But you don't declare grade1_num, grade2_num, etc. until line 102. So on line 98, grade1_num does not exist, nor does it have a valid value.

Switch the order:
1
2
3
4
5
6
7
8
9
	double grade1_num = atof(grade1.c_str());
	double grade2_num = atof(grade2.c_str());
	double grade3_num = atof(grade3.c_str());
	double grade4_num = atof(grade4.c_str());
	double grade5_num = atof(grade5.c_str());
	double grade6_num = atof(grade6.c_str());
	// now, grade1_num ... grade6_num all have values.
	double Average = (grade1_num + grade2_num + grade3_num + grade4_num + grade5_num + grade6_num) / 6;
	cout << "     Total Grade Average:  " << Average << endl;


You can't use a variable before it's declared and is set with a proper value.
First you calculate the result. THEN you print it, or use it for further calculation.
Last edited on
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream> // I/O operations
#include <iomanip> // Formatting functions
#include <cstdlib> // General use library functions
#include <string> // Defines dynamic string stuff

using namespace std;

int main( )
{
    //--------  Start of variable and object creation ------
    
    string csid;
    string first;
    string last;
    
    string temp;
    
    double grade1 = 0;
    double grade2 = 0;
    double grade3 = 0;
    double grade4 = 0;
    double grade5 = 0;
    double grade6 = 0;
    
    string resp = "XXXX";
    
    // system("cls");
    
    cout << endl << endl << endl << endl; //4 blank lines
    cout << "                               " << endl;
    cout << endl << endl;
    cout << "                       " << endl;
    cout << endl << endl << endl << endl << endl << endl; // 6 blank lines
    cout << "                             " << endl;
    cout << endl << endl << endl << endl << endl << endl << endl << endl;
    
    //--------  End of the Splash Screen  --------------------------
    
    //Transition to next screen
    cout << "                          Press <ENTER> to Continue";
    getline(cin, resp);
    // system("cls");
    //End of transition
    
    //--------  Start of Input Screen  -----------------------------
    
    cout << endl << endl;
    cout << "                               COLLEGE NAME" << endl;
    cout << endl;
    cout << "                                STUDENT GRADE TRACKING SYSTEM" << endl;
    cout << endl;
    cout << "                                Input Screen" << endl;
    cout << endl;
    cout << "    Enter data as requested and press <ENTER> after each.";
    cout << endl << endl;
    cout << "     Last Name:  "; getline(cin, last);
    cout << "     First Name:  "; getline(cin, first);
    cout << endl;
    cout << "     Student ID Number:  "; getline(cin, csid);
    
    cout << "     Grade 1:  "; getline(cin, temp);
    grade1 = atof(temp.c_str());
    
    cout << "     Grade 2:  "; getline(cin, temp);
    grade2 = atof(temp.c_str());
    
    cout << "     Grade 3:  "; getline(cin, temp);
    grade3 = atof(temp.c_str());
    
    cout << "     Grade 4:  "; getline(cin, temp);
    grade4 = atof(temp.c_str());
    
    cout << "     Grade 5:  "; getline(cin, temp);
    grade5 = atof(temp.c_str());
    
    cout << "     Grade 6:  "; getline(cin, temp);
    grade6 = atof(temp.c_str());
    
    cout << "                           Press <ENTER> to Continue";
    getline(cin, resp);
    system("cls");
    
    cout << endl << endl;
    cout << "                               COLLEGE NAME\n";
    cout << endl;
    cout << "                                STUDENT GRADE TRACKING SYSTEM\n";
    cout << endl;
    cout << "                                Output Screen\n";
    cout << endl << endl << endl;
    cout << "     Student Name (First Last):  " << first;
    cout << " " << last << endl;
    cout << "     CSID:  " << csid << endl;
    
    cout << "     Grade 1:  " << grade1 << endl;
    cout << "     Grade 2:  " << grade2 << endl;
    cout << "     Grade 3:  " << grade3 << endl;
    cout << "     Grade 4:  " << grade4 << endl;
    cout << "     Grade 5:  " << grade5 << endl;
    cout << "     Grade 6:  " << grade6 << endl;
    
    double Average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6) / 6;
    
    
    cout << "     Total Grade Average:  " << Average << endl;
    
    cout << "                           Press <ENTER> to Continue";
    getline(cin, resp);
    //system("cls");
    
    return 0;
}
Topic archived. No new replies allowed.