C++ Code gives wrong answers

Pages: 12
free
Last edited on
One problem is that you store 5 students in arrays of 4 elements. I don't know which problems that will solve, but it definitely isn't right.
Put your code in [code] tags as it is how hard to read.
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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

int height[4];
char gender[4];
int userNumber = 0;
int userSex = 0;
int maxHeight = 0;
int minHeight = 100;
int final = 0;
int count = 0;
int boys = 0;
int girls = 0;
int sum = 0;
int all = 0;
float totalHeight = 0;
float avg = 0;
float favg = 0;


cout << "Enter Student 1's height: ";
cin >> height[userNumber];
cout << "Enter Student 1's gender [M or F]: ";
cin >> gender[userSex];
gender[userSex] = toupper (gender[userSex]);
cout << "Enter Student 2's height: ";
cin >> height[userNumber];
++userNumber;
cout << "Enter Student 2's gender [M or F]: ";
cin >> gender[userSex];
++userSex;
cout << "Enter Student 3's height: ";
cin >> height[userNumber];
++userNumber;
cout << "Enter Student 3's gender [M or F]: ";
cin >> gender[userSex];
++userSex;
cout << "Enter Student 4's height: ";
cin >> height[userNumber];
++userNumber;
cout << "Enter Student 4's gender [M or F]: ";
cin >> gender[userSex];
++userSex;
cout << "Enter Student 5's height: ";
cin >> height[userNumber];
++userNumber;
cout << "Enter Student 5's gender [M or F]: ";
cin >> gender[userSex];
++userSex;

for(int count=0;count<5;count++)
{
totalHeight=totalHeight+height[count];

if (height[count]>=maxHeight)
maxHeight=height[count];

if (height[count]<=minHeight)
minHeight=height[count];

if(gender[count]=='F') girls++;

if(gender[count]=='M') boys++; 
}
avg = totalHeight/5;
favg = totalHeight/girls;
cout << "The tallest student is " << maxHeight << " and the shortest student is " << minHeight << endl;
cout << "The average height of all students is " << avg << endl;
cout << "The number of female students is " << girls << endl;
cout << "The number of male students is " << boys << endl;
cout << "The average height of female students is " << favg << endl;

cout << "The student heights and genders you entered are: "<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << height[x] << endl;
cout << gender[x] << endl;
}

ofstream studentHeights;
studentHeights.open("heights.txt", ios::out);

if (studentHeights.is_open())
{
for (int count = 0; count < 5; count = count + 1)
{
studentHeights << height[count] << endl;
}
studentHeights.close();
}
else
{
cout << "The file could not be opened." << endl;

}


system("pause");
return 0;
}

firstly let clean this up so that its legible. instead of using cout and cin for every student why not make a simple for loop that is more readable and accomplishes the same thing?
1
2
3
4
5
6
7
8
9
10
11
12
        int height[5];
	char gender[5];
	int userNumber = 0;
	for (int studentNum = 1; studentNum <= 5; studentNum++)
	{
		cout << "Enter Student " << studentNum << "'s height: ";
		cin >> height[userNumber];
		cout << "Enter Student " << studentNum << "'s gender: ";
		cin >> gender[userNumber];

		userNumber++;
	}
Last edited on
Should we report an user who deletes their posts?
We could, but there is not much point to do that. You can't ban someone for deleting his post and I bet twicker can't bring back the contents. Do you see any other possible reasons?
i only deleted the old question because the new code made things a bit confusing. sorry.

Here is the current code and problems I'm encountering:

The problems are:
1. It writes a 0 (to display and to heights.txt) for the first height entered no matter what value you put.
2. It does not give the minimum height.
3. It does not give the correct average height.
4. It does not give the number of boys entered and the number of girls.
5. It does not give the average girls height.

Here is the code:


#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

int height[5];
char gender[5];
int userNumber = 0;
int totalHeight = 0;
int totalGirlsHeight = 0;
int maxHeight = 0;
int minHeight = 0;
int girls = 0;
int boys = 0;
int avg = 0;
int favg = 0;


for (int studentNum = 1; studentNum <= 5; studentNum++)
{
cout << "Enter Student " << studentNum << "'s height: ";
cin >> height[userNumber];
cout << "Enter Student " << studentNum << "'s gender [M,m,F,f]: ";
cin >> gender[userNumber];
userNumber++;
}

for(int count=1;count<5;count++)
{

totalHeight=totalHeight+height[count];

height[0]=maxHeight=minHeight;
if (height[count]>=maxHeight)
maxHeight=height[count];

if (height[count]<=minHeight)
minHeight=height[count];

if (gender[count]=='F' || gender[count]=='f') girls++;
if (gender[count]=='M' || gender[count]=='m') boys++;

totalGirlsHeight=girls+height[count];
}
avg = totalHeight/5;
favg = totalGirlsHeight/girls;


cout << "The tallest student is " << maxHeight << " and the shortest student is " << minHeight << endl;
cout << "The average height of all students is " << avg << endl;
cout << "The number of female students is " << girls << endl;
cout << "The number of male students is " << boys << endl;
cout << "The average height of female students is " << favg << endl;

cout << "The student heights and genders you entered are: "<< endl;
for (int x = 0; x < 5; x = x + 1)
{
cout << height[x] << endl;
cout << gender[x] << endl;
}

ofstream studentHeights;
studentHeights.open("heights.txt", ios::out);

if (studentHeights.is_open())
{
for (int count = 0; count < 5; count = count + 1)
{
studentHeights << height[count] << endl;
}
studentHeights.close();
}
else
{
cout << "The file could not be opened." << endl;

}


system("pause");
return 0;
}
Last edited on
I don't think you understand.

CODE TAGS

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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

int height[5];
char gender[5];
int userNumber = 0;
int totalHeight = 0;
int totalGirlsHeight = 0;
int maxHeight = 0;
int minHeight = 0;
int girls = 0;
int boys = 0;
int avg = 0;
int favg = 0;


for (int studentNum = 1; studentNum <= 5; studentNum++)
{
cout << "Enter Student " << studentNum << "'s height: ";
cin >> height[userNumber];
cout << "Enter Student " << studentNum << "'s gender [M,m,F,f]: ";
cin >> gender[userNumber];
userNumber++;
}

for(int count=1;count<5;count++)
{

totalHeight=totalHeight+height[count];

height[0]=maxHeight=minHeight;
if (height[count]>=maxHeight)
maxHeight=height[count];

if (height[count]<=minHeight)
minHeight=height[count];

if (gender[count]=='F' || gender[count]=='f') girls++;
if (gender[count]=='M' || gender[count]=='m') boys++;

totalGirlsHeight=girls+height[count];
}
avg = totalHeight/5;
favg = totalGirlsHeight/girls;


cout << "The tallest student is " << maxHeight << " and the shortest student is " << minHeight << endl;
cout << "The average height of all students is " << avg << endl;
cout << "The number of female students is " << girls << endl;
cout << "The number of male students is " << boys << endl;
cout << "The average height of female students is " << favg << endl;

cout << "The student heights and genders you entered are: "<< endl;
for (int x = 0; x < 5; x = x + 1)
{
cout << height[x] << endl;
cout << gender[x] << endl;
}

ofstream studentHeights;
studentHeights.open("heights.txt", ios::out);

if (studentHeights.is_open())
{
for (int count = 0; count < 5; count = count + 1)
{
studentHeights << height[count] << endl;
}
studentHeights.close();
}
else
{
cout << "The file could not be opened." << endl;

}


system("pause");
return 0;
}
That code doesn't work. Here is the latest code. The only two issues left are:

1. Getting the code to output the minHeight (shortest student).
2. Getting the code to output the average height of the girls only.

Here is the code:

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

int height[5];
char gender[5];
int userNumber = 0;
int userSex = 0;
int maxHeight = 0;
int minHeight = 0;
int final = 0;
int boys = 0;
int girls = 0;
int sum = 0;
int all = 0;
float totalHeight = 0;
float totalGirlsHeight = 0;
float avg = 0;
float favg = 0;


for (int studentNum = 1; studentNum <= 5; studentNum++)
{
cout << "Enter Student " << studentNum << "'s height: ";
cin >> height[userNumber];
cout << "Enter Student " << studentNum << "'s gender [M,m,F,f]: ";
cin >> gender[userNumber];
userNumber++;
}

for(int count=0;count<5;count++)
{
totalHeight=totalHeight+height[count];
totalGirlsHeight=totalGirlsHeight+height[count];

if (height[count]>=maxHeight)
maxHeight=height[count];

if (height[count]<=minHeight)
minHeight=height[count];

if(gender[count]=='F') girls++;

if(gender[count]=='M') boys++;
}
avg = totalHeight/5;
favg = totalGirlsHeight/girls;
cout << "The tallest student is " << maxHeight << " and the shortest student is " << minHeight << endl;
cout << "The average height of all students is " << avg << endl;
cout << "The number of female students is " << girls << endl;
cout << "The number of male students is " << boys << endl;
cout << "The average height of female students is " << favg << endl;

cout << "The student heights and genders you entered are: "<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << height[x] << endl;
cout << gender[x] << endl;
}

ofstream studentHeights;
studentHeights.open("heights.txt", ios::out);

if (studentHeights.is_open())
{
for (int count = 0; count < 5; count = count + 1)
{
studentHeights << height[count] << endl;
}
studentHeights.close();
}
else
{
cout << "The file could not be opened." << endl;

}


system("pause");
return 0;
}
dear irishme please use code tags so we can actually read wtf your code supposed to be


That code doesn't work. Here is the latest code. The only two issues left are:

1. Getting the code to output the minHeight (shortest student).
2. Getting the code to output the average height of the girls only.

Here is the code:

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
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

int height[5];
char gender[5];
int userNumber = 0;
int userSex = 0;
int maxHeight = 0;
int minHeight = 0;
int final = 0;
int boys = 0;
int girls = 0;
int sum = 0;
int all = 0;
float totalHeight = 0;
float totalGirlsHeight = 0;
float avg = 0;
float favg = 0;


for (int studentNum = 1; studentNum <= 5; studentNum++)
{
cout << "Enter Student " << studentNum << "'s height: ";
cin >> height[userNumber];
cout << "Enter Student " << studentNum << "'s gender [M,m,F,f]: ";
cin >> gender[userNumber];
userNumber++;
}

for(int count=0;count<5;count++)
{
totalHeight=totalHeight+height[count];
totalGirlsHeight=totalGirlsHeight+height[count];

if (height[count]>=maxHeight)
maxHeight=height[count];

if (height[count]<=minHeight)
minHeight=height[count];

if(gender[count]=='F') girls++;

if(gender[count]=='M') boys++; 
}
avg = totalHeight/5;
favg = totalGirlsHeight/girls;
cout << "The tallest student is " << maxHeight << " and the shortest student is " << minHeight << endl;
cout << "The average height of all students is " << avg << endl;
cout << "The number of female students is " << girls << endl;
cout << "The number of male students is " << boys << endl;
cout << "The average height of female students is " << favg << endl;

cout << "The student heights and genders you entered are: "<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << height[x] << endl;
cout << gender[x] << endl;
}

ofstream studentHeights;
studentHeights.open("heights.txt", ios::out);

if (studentHeights.is_open())
{
for (int count = 0; count < 5; count = count + 1)
{
studentHeights << height[count] << endl;
}
studentHeights.close();
}
else
{
cout << "The file could not be opened." << endl;

}


system("pause");
return 0;
}
I don't know what you mean by using code tags. I'm totally new at this, which is why it's in the beginners forum.
[code]
Your code goes here
[/code]
This all works as C++ code EXCEPT it will not give me the average girls height.

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

#include <iostream>
 #include <fstream>
 using namespace std;
 
int main ()
 {
 
int height[5];
 char gender[5];
 int userNumber = 0;
 int userSex = 0;
 int maxHeight = 0;
 int minHeight = 999999;
 int boys = 0;
 int girls = 0;
 float totalHeight = 0;
 float totalGirlsHeight = 0;
 float avg = 0;
 float favg = 0;
 

for (int studentNum = 1; studentNum <= 5; studentNum++)
 {
 cout << "Enter Student " << studentNum << "'s height: ";
 cin >> height[userNumber];
 cout << "Enter Student " << studentNum << "'s gender [M,m,F,f]: ";
 cin >> gender[userNumber];
 userNumber++;
 }
 
for(int count=0;count<5;count++)
 {
 totalHeight=totalHeight+height[count];
 totalGirlsHeight=girls+height[count];
 
if (height[count]>=maxHeight)
 maxHeight=height[count];
 
if (height[count]<=minHeight)
 minHeight=height[count];
 
if(gender[count]=='F' || gender[count]=='f') girls++;
 
if(gender[count]=='M' || gender[count]=='m') boys++; 
}
 avg = totalHeight/5;
 favg = totalGirlsHeight/girls;
 cout << "The tallest student is " << maxHeight << " and the shortest student is " << minHeight << endl;
 cout << "The average height of all students is " << avg << endl;
 cout << "The number of female students is " << girls << endl;
 cout << "The number of male students is " << boys << endl;
 cout << "The average height of female students is " << favg << endl;
 
cout << "The student heights and genders you entered are: "<< endl;
 for (int x = 0; x < 10; x = x + 1)
 {
 cout << height[x] << endl;
 cout << gender[x] << endl;
 }
 
ofstream studentHeights;
 studentHeights.open("heights.txt", ios::out);
 
if (studentHeights.is_open())
 {
 for (int count = 0; count < 5; count = count + 1)
 {
 studentHeights << height[count] << endl;
 }
 studentHeights.close();
 }
 else
 {
 cout << "The file could not be opened." << endl;
 
}
 

system("pause");
 return 0;
 }
line 34
Thanks, but what about line 34? That looks right to me. So does Line 35. What should it be?
Last edited on
Well, you're adding the number of girls to the height of some person and assigning the result to totalGirlsHeight. How is that right?
In fact that line shouldn't even be there.
You need to do what you did with totalHeight, but you only need to do it if the current student is a girl.
I'm not trying to be obtuse, but your answer is very vague to me. What line should not be where?

As for the other, are you saying I should use this for Line 35:

totalGirlsHeight=totalGirlsHeight+girls[count];

I tried that but it did not work. I really don't understand this and I've read everything I could find on the Net about it.
while were bitching about how you format your posts PLEASE INDENT YOUR CODE TO
You could do that but then totalGirlsHeight would be equal to totalHeight (as the code that calculates them would be the same)
Wait. girls[cout] ? a typo?..

Anyway, this is not something you are going to read somewhere. This is a matter of logic. Just answer a question. When do you need to add height[count] to totalGirlsHeight ?
Pages: 12