displaying numbers properly/comparing character input

Hello. I have two problems. One is that I cannot get my numbers to display properly. If you run the program with certain values that should clearly average and show 90.00, it is instead displaying "89". I don't know why I can't get it to display correctly. I don't know how to use setprecision(2) properly, or something.

My other question is: how do you compare words?

I must have misunderstood something in lecture. I tried to declare

char choicemore;

and then say if (choicemore=="y")

but it didn't work. How would I do that properly? Here's my code if you wanna see it for specific errors.


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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
float choiceprogram;
float choicequiz;
float choicetest;
float programavg = 0;
float programscores = 0;
float programmax;

float quizavg = 0;
float quizscores = 0;
float quizmax;

float testavg = 0;
float testscores = 0;
float testmax;

int choicemore;
float examavg;
float overallavg;

// This is the main loop of the program, that executes
// one time guaranteed, and continues when the user
// tells it 'y' after going through the loop once completely

do
{
cout << "Enter the maximum possible program points so far: ";
cin >> programmax;
cout << endl;

// This is the loop that accepts program scores

       while (choiceprogram>-1)
       {
               cout << "Enter a program score (-1 to quit): ";
               cin >> choiceprogram;


               programscores+=choiceprogram;

       }

cout << endl << "Enter the maximum possible quiz points so far: ";
cin >> quizmax;
cout << endl;

// This is the loop that accepts quiz scores

       while (choicequiz>-1)
       {
               cout << "Enter a quiz score (-1 to quit): ";
               cin >> choicequiz;


               quizscores+=choicequiz;

       }

cout << endl << "Enter the maximum possible test points so far: ";
cin >> testmax;
cout << endl;

// This loop is to verify that there are more than 0 test points possible

       if (testmax!=0)
       {

       // This loop is to get test scores

               while (choicetest>-1)
               {

                       cout << "Enter a test score (-1 to quit): ";
                       cin >> choicetest;

                       testscores+=choicetest;

               }

               cout << endl;
       }

programavg = programscores / programmax;
quizavg = quizscores / quizmax;

programavg*=100;
quizavg*=100;


cout << "Program average is: " << programavg;
cout << endl << "Quiz average is " << quizavg;

// This loop is to display the proper message based on tests entered or not
       if (testmax==0)
       {
               cout << endl << "No tests entered so no test average calculated";
       }
       else
       {
               testavg = testscores / testmax;
               testavg*=100;
               cout << endl << "Test average is: " << testavg;
       }
//  The following loop is to calculate the exam average
       if (testmax==0)
       {
               examavg = quizavg;
               cout << endl << "Exam average is: " << examavg;
       }
       else
       {
               examavg = ((((testavg * 3) + quizavg)) / 4);
               cout << endl << "Exam average is: " << examavg;
       }

// This loop is for displaying the Overall average

       if (testmax==0)
       {
               overallavg = ((.6*quizavg) + (.4*programavg));
               cout << endl << "Overall average is: " << overallavg;
       }
       else
       {
               overallavg = ((.4*programavg) + (.15*quizavg) + (.45*testavg));
               cout << endl << "Overall average is: " << overallavg;
       }




// This next loop is the warning message for low test scores
       if (examavg<55)
       {
               cout << endl << endl << "*** Your exam average is below 55%! ***" << endl;
       }
       else
       {
       cout << endl;
       }





cout << endl << "Another (1/0)? ";
cin >> choicemore;
cout << endl;

choiceprogram = 0;
choicequiz = 0;
choicetest = 0;

overallavg = 0;
examavg = 0;
testavg = 0;
programscores = 0;
quizscores = 0;
testscores = 0;
programavg = 0;
quizavg = 0 ;
testavg = 0;
}
while (choicemore>0);

return 0;

}
Your first problem is floating point roundoff error because values like 0.4 and 0.6 do not have exact binary representations. The wrong way to solve the problem (but will work) is to use doubles instead of floats. The right way to solve the problem is to use integer calculations throughout and convert to floating point only when about to display the result (although even then it isn't strictly necessary to convert to floating point).

Your second problem is that

"Y" (double quotes) is a string whose type in C/C++ is char*.
'Y' (single quotes) is a character whose type in C/C++ is char.

To compare characters you can use ==, ie if( answer == 'y' )
To compare strings you must use strcmp.
right now choicemore is an int, not a char. If you meant it to be a char, change it.
jpeg I know it's wrong right now. I changed it to a number because I understand those and wanted to test how it works when running again.

jsmith thanks a lot I'm going to try and implement that right now
Hmmm.. tried switching everything to a double but that didn't work for my decimal problem.

I get why my char problem got an error though, thanks!

How do I change it to a float at the last minute?

like

cout << float quizavg;

?
Topic archived. No new replies allowed.