I don't know where to start


Ok I'm really stuck, I'm really struggling to understand this C++ stuff, and I'm doing stuff that's VERY basic. What I need help with is creating a parallel array that allows the user to input a number and then depending on that number I want the array to give me specific data. for example....

initialize the array with 5 students names, Bob, Mary, Mike, Joe, Susan. In the second array, initialize the array with these test scores averages, 95, 80, 90, 65, 92. When running the program, the user will prompted to enter a number between 1 and 5. If the number entered is 1, the program should print "Student 1 is Bob, who has a test score average of 95".

make sense? Can someone help me get started? even if you can give me something that works with completely different data, just as long as I can get the jist of it.
You can create two arrays, for example -

1
2
std::string Students[5];
int Average[5];


Fill it with the appropriate information you need then prompt the user for a number:

1
2
3
std::cin >> number;
std::cout << "Student 1 is " << Students[number - 1] << "who has a test score average of " << Average[number - 1] << std::endl;
// You access number - 1 of the array because if you enter 1, you are trying to access the first member which is in fact 0 


You also might want to add some checking to make sure the number they enter is between 1 - 5.
Last edited on
Awesome, here's what I ended with... please feel free to give me direction on streamlining.

Thanks so much!!
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
#include <iostream>

using namespace std;

int main()
{
	string students[5] = {"Bob", "Mary", "Mike", "Joe", "Susan"};
	int average[5] = {95, 80, 90, 65, 92};
	int number;
	
	cout << "Please input a number between 1 and 5" << endl;
	std::cin >> number;
	
	if (number < 1)
	{
		cout << "Illegal choice" << endl;
		return 0;
	}
	else if (number > 5)
	{
		cout << "Illegal choice" << endl;
		return 0;
	}

	else 
	{
		std::cout << "Student " << number << " is " << students[number - 1] << " who has a test score average of " << average[number - 1] << std::endl;
	}

	return 0;
	
}
Just some tips -

The program probably shouldn't close when the user enters an invalid number - but instead, maybe it should just ask the user again?

1
2
3
4
5
while (number < 1 || number > 5) //smaller than 1 OR bigger than 5
{
    std::cout << "Illegal choice." << std::endl;
    std::cin >> number;
}


As you can see, you can also combine the the two functions together by using the || operator.
Topic archived. No new replies allowed.