Question:
Need help with C++ problem?
anonymous
2011-11-30 16:22:21 UTC
1. A 5th grade teacher has measured the height (in inches) of 10 students in her class. Write a program that (Your program will have two arrays) :

a. Uses one loop to read the heights from the keyboard and store the heights in an array (use int as variable type) AND in addition read the student gender (‘M’ ‘m’ ‘F’ ‘f’ ) into a second array to store the gender (use char as variable type).
Suggested screen output :
Enter student 1 height:
Enter student 1 gender:
Enter student 2 height:
Enter student 2 gender:
Enter student 3 height:
Enter student 3 gender:
Etc..

The program should also:

b. Display the current height and the gender of each student.

c. Calculate and display the average height of the Female and the Male students.

d. Display the height of the shortest and tallest student in the whole class.

e. Display the count of Female and Male students.

f. Display the heights of the Female students only

g. Save the heights in the text file named heights.txt



Here is what I have so far but I keep getting an error


#include

using namespace std;

int main()
{
double height[10];
double gender = 0.0;
double sumHeight = 0.0;
double avgHeight = 0.0;

for (int x = 0; x < 10; x = x + 1)
{
height[x] = 0.0;

}

cout << "You will be asked to enter the height in inches and gender of 10 students."<< endl;

for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter students height " << x+1 << ": ";
cin >> height[x];
}
cout << "Enter students gender" << gender << ": ";
cin >> gender;
cout << "The height in inches and gender of 10 students: " << endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << "Height " << x+1 << ": high " << height[x] << " Gender: "<< gender << endl;
}
{
sumHeight = sumHeight + height[x];
}

avgHeight = sumHeight / 10;
cout << "The average student height: " << avgHeight << endl;

system("pause");
return 0;
}
Three answers:
steve
2011-11-30 16:28:43 UTC
Give me a min to look over it, don't log off yet;)



You have tons of logic errors, and I can only find 1 syntax error. Its explained below.



you can replace this:

for (int x = 0; x < 10; x = x + 1)

{

   height[x] = 0.0;

}



by initializing the variable like this

double height[10] = {0.0};



that will make the default value for all 10 elements 0.0.







gender shouldn't be a double, it should be a char. A double is a decimal, are you going to insert 1.0 for males, and 2.0 for females? Using 'M' 'F' 'm' 'f' as your professor instructed makes it more intuitive.



cout << "Enter students gender" << gender << ": ";

not sure why you cout gender here





cout << "Height " << x+1 << ": high " << height[x] << " Gender: "<< gender << endl;

here, the variable gender will always hold the 10th persons gender, since you aren't saving all of the genders. This is where your second array should come into play. Do the same thing you did with height, except make them char and not double







{

sumHeight = sumHeight + height[x];

}



this isn't doing anything special, its outside of all of the loops. x doesn't exist in this scope (this is actually where your compilation error is coming from).

If I were you, I would put this in the following loop.

for (int x = 0; x < 10; x = x + 1)

{

cout << "Enter students height " << x+1 << ": ";

cin >> height[x];

sumHeight = sumHeight + height[x];

}





There is a lot wrong with the logic, you'll need to work a little more on it, try improving on what I suggested
?
2016-10-24 08:38:27 UTC
Its possibly you're writing a normal c utility (no longer a domicile windows app). this suggests that as quickly as you run an utility, all output would be displayed in a 'DOS' window, yet whilst the execution of your app is comprehensive (whilst the 'significant' function returns) then that window would be closed as that's no longer needed. To get around this undertaking, you could placed a normal assertion to request a character from the keyboard, and this equipment won't proceed till it recieves that character. it is comprehensive by using including right here... getchar(); // Returns an int protecting the main code of which ever keyboard key replaced into pressed. desires 'enter' from keyboard besides. you will would desire to #comprise to apply this in case you arn't already. instead, you need to call a equipment command (which takes any command which you would be able to enter under DOS, as lyinx2001 shows, by using including... equipment("PAUSE"); you will would desire to #comprise while you're no longer already doing so. A thrid way, and is principally while you're arising in domicile windows, is to pause your software for x seconds. you're able to try this by using calling... Sleep(one thousand); // take into consideration the capital S and the selection is the time you % this equipment to sleep in milliseconds. one thousand == a million 2nd. you will would desire to #comprise if utilising this. My selection has continually being the 1st physique of techniques, and im useful there is many different techniques of conserving the window open (there in many situations are many techniques of winding up a single activity whilst programming is worried - its all a count of selection). in case you have been utilising a Microsoft progression environment, then in many situations the window is basically no longer closed and a 'Press any key' would be displayed by using default.
?
2011-11-30 16:34:29 UTC
you shold date C++ to know her more batter


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...