Stuck on a real tough problem

free
Last edited on
Your arrays aren't decalred, all you're doing is overwriting your previous entry in the section where you prompt the user for their input.

It would be helpful to know what error you are getting when you try to compile your project. Off the top of my head you didn't declare "cstdlib" so the "system(...)" function at the end of your code remains undefined. Not that you should be using "system(...)" but it gets old telling everyone that comes through here why.

EDIT: You're trying to address "height" as an array in your for loop toward the end (please use code brackets), it is not declared as such. This would be another reason your code would not compile.

EDIT2: You did not declare "fstream.h" but you are trying to use and "ofstream" object. Yet another reason this would not compile. Also, unless you are denied write permissions to the directory you are working in which would cause so many other issues, there is really no reason that an ofstream would fail.
Last edited on
CODE TAGS PLEASE
First of all, please use [ CODE ] tags... second, instead of this ridiculous snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int userNum[100];
int height[5];
char userMF[1];

cout << "Enter Student 1's height: ";
cin >> height[userNum++];
cout << "Enter Student 1's gender [M or F]: ";
cin >> gender[userMF++];
cout << "Enter Student 2's height: ";
cin >> height[userNum++];
cout << "Enter Student 2's gender [M or F]: ";
cin >> gender[userMF++];
cout << "Enter Student 3's height: ";
cin >> height[userNum++];
cout << "Enter Student 3's gender [M or F]: ";
cin >> gender[userMF++];
cout << "Enter Student 4's height: ";
cin >> height[userNum++];
cout << "Enter Student 4's gender [M or F]: ";
cin >> gender[userMF++];
cout << "Enter Student 5's height: ";
cin >> height[userNum++];
cout << "Enter Student 5's gender [M or F]: ";
cin >> gender[userMF++];


Do this:

1
2
3
4
5
6
7
8
9
10
int height[5];
char userMF[5];

for(int i = 0;i < 5;i++)
{
    cout << "Enter Student " << (i + 1) << "\'s Height: ";
    cin >> height[i];
    cout << "Enter Student " << (i + 1) << "\'s Gender: ";
    cin >> userMF[i];
}


The reason your code keeps breaking is because of this:

1
2
3
4
5
int userNum[100];
.
.
.
cin >> height[userNum++];


Not only is this not valid syntax, it doesn't make any sense. First of all, userNum is an array... you can't increment the identifier like that... second, height is an array of 5 places, not 100... so this goes out of bounds of the array.
Last edited on
Topic archived. No new replies allowed.