Hello, first time poster here so please bear with me. I've been up and down the internet, trying to figure out how to figure out this problem. Yes, it is a homework problem and I understand the approach, no worries there. I'm just asking for some guidance.
So, here is my issue. I'm trying to figure out One Dimensional Parallel Arrays, on top of that, one of the arrays must consist of several Strings. I've never dealt with either of these topics so, it is all rather confusing. Long story short, I need to create a program that asks for student's names, five in total. Then, asks for each student's age. Finally it displays the student and their age together on the same line.
Ultimately, the program needs to look like this.
Enter a student name: Joe
Enter a student name: Mark
Enter a student name: Mary
Enter a student name: Michelle
Enter a student name: Aaron
Enter Joe’s Age: 25
Enter Mark’s Age: 24
Enter Mary’s Age: 35
Enter Michelle’s Age: 19
Enter Aaron’s Age: 65
Joe 25
Mark 24
Mary 35
Michelle 19
Aaron 65
Here's the incomplete code I have so far.
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
|
//Parallel Arrays 11-2
#include <iostream>
using namespace std;
int main()
{
//declare arrays
string nameStudent[5] = {""};
int ageStudent[5] = {0};
//declare variables
string names = "";
int age = 0;
//Name entry
for (string names = ""; names += 1; names < 5;)
{
cout << "Enter a student name: " <<
cin >> nameStudent[names];
}
//Age entry
for (int age = 0; age < 5; age += 1)
{
cout << "Enter " << "" << "'s Age: ";
cin >> ageStudent[age];
}//end for loop
//Display Data
cout << nameStudent[names] << ageStudent[age];
}
|
Here are my problems...
Declaring variables, let alone arrays, has not been covered at all in this course. So, when it comes to the For loop calling for names, I'm getting several errors, listed below.
17 could not convert `(&names)->std::basic_string<_CharT, _Traits, _Alloc>::operator+= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](1)' to `bool'
17 no match for 'operator<' in 'names < 5'
30 no match for 'operator[]' in 'nameStudent[names]'
How do I go about creating a For loop that displays up to the amount of variables in the nameStudent array, while using Strings? I did the same thing with a One Dimensional Array and it worked out just fine. Granted, it was a simple enter and display program and it worked through integers, not strings.
Then comes the matter of displaying the contents of both Arrays, which seems simple, but I'm at a loss as the nameStudent array doesn't seem to be worded correctly.
I apologize for asking such a question but I am really at a loss when it comes to Arrays and Strings. I've been able to figure out every other type of problem I've faced but this has brought all of my progress to a screeching halt.
Thank you for your time and efforts.