Trouble with One Dimensional Parallel Arrays and Strings

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.
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.


Using the .length varible in the array:

1
2
3
4
string nameStudent[5] = {"", "", "", "", ""};
for (int i = 0; i < nameStudent.length; ++i) {
        cin >> nameStudent[i];
}


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.


and again, same idea:

1
2
3
for (int i = 0; i < nameStudent.length; ++i) {
        cout << nameStudent[i];
}


then again for the ages, of course.

Hope that helped.
Last edited on
Thanks for the quick reply! I have modified my code but I'm still getting errors now pertaining to the use of .length.

Here is the revised 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
//Parallel Arrays 11-2

#include <iostream>
using namespace std;

int main()
{
    //declare arrays
    string nameStudent[5] = {"", "", "" ,"", ""}; 
    int ageStudent[5] = {0};
    
    //declare variables
    string names = ""; //Check
    int age = 0;
    
    //Name entry
    for (int i = 0; i < nameStudent.length; ++i) 
    {   
        cout << "Enter a student name: ";
        cin >> nameStudent[i];   
        }
    //Age entry
    for (int age = 0; age < 5; age += 1)
    {
        cout << "Enter " << nameStudent << "'s Age: "; 
        cin >> ageStudent[age];
        }//end for loop
        
    //Display Data
    for (int i = 0; i < nameStudent.length; ++i)
    {
        cout << nameStudent[i];
        }
        
    for (int i = 0; i < ageStudent.length; ++i)
    {
        cout << ageStudent[i];
        }
    system("pause");
    return 0;
}
    


This is now displaying the following errors

17 and 30 request for member `length' in `nameStudent', which is of non-class type `std::string[5]'

35 request for member `length' in `ageStudent', which is of non-class type `int[5]'

From what I'm gathering, it seems like there is still something wrong with my arrays. On top of that, my variables now look...rather useless. Need to clean that up. At first, I thought it was something up with the Strings again but this is also applying to the age calls as well.



There's no .length for arrays (that's a Java thing).
Replace nameStudent.length and ageStudent.length with 5.
Better yet, make a new const variable for the size of the array and use that instead. (makes it easy if you want to change the size later)
Ah, that helps tremendously!...and now that I see it in the compiler, it looks so basic...I feel like crap for not thinking about that. Thanks to you both!

However, the battle is far from over. Now that the blasted thing compiled, it's not displaying things correctly...i.e. I screwed up somewhere.

The first section of the program works, the...

Enter student name:

That works, but when it is called for the...

Enter <variable>'s age:

It displays like this....

Enter *Actual memory address*'s age:

Then when it is all said and done...it displays all of the names like this:

TomJoeMikeBlahBlach 24156235412Press any key....

Not a literal description but just about the same thing. So close, almost got this.


*Edit* Alright, I got the display to work, now I just need to get it to display each name when asking for their ages. Blasted program is going down!
Last edited on
In line 25, change
cout << "Enter " << nameStudent << "'s Age: ";
to
cout << "Enter " << nameStudent[age] << "'s Age: ";.

You should also combine your last two for loops together, like this:
1
2
3
4
5
6
7
for (int i = 0; i < 5; ++i)
{
    cout << nameStudent[i];
    cout << ' ';
    cout << ageStudent[i];
    cout << '\n';
}

(You don't have to use four separate cout statements like I did; you can smash it all into one. I just made them separate so you could see what I meant by "combining your last two for loops together)".)
Awesome, thanks for the assistance. I'm going to have to look up this stuff again. While putting [age] next to nameStudent worked, I need to know why, otherwise, this course is for nothing.

Either way, the program is done. Thanks for the suggestion on putting the loops together but in the end, I kept them separate for my own sanity. I'm not good at this stuff and need to see each and every process as it goes in.

Again, thank you.
nameStudent is an array, so to access the elements, you need the index to be there (i.e. nameStudent[0] will give you the name of the first student, nameStudent[1] will give you the second student's name, and so on).
Perhaps the use of age as your index for the for loop might have been confusing you a little bit. (In fact, your names and age variables are otherwise completely unused.)

If you just type cout << nameStudent;, then the compiler will treat nameStudent as a pointer and print out the value of that pointer (which is just the memory address of the first element of the array).

The reason I combined the for loops was because I needed to display the name and age together on the same line, so I couldn't have kept the loops separate. If I had instead written this:
1
2
3
4
5
for (int i = 0; i < 5; ++i)
    cout << nameStudent[i] << '\n';

for (int i = 0; i < 5; ++i)
    cout << ageStudent[i] << '\n';

I would have gotten this output:
Joe
Mark
Mary
Michelle
Aaron
25
24
35
19
65
.
I see, now I see. I did kind of what you did already when I edited my post above. Thanks for the explanation.
Alright, I think I'm about to call it a night but I've run into another snag in another program. Instead of making a new topic, I'll just post it here since it is within the same topic, just a whole new ball game here.

This problem requests that I create two arrays, both strings, one with input CD titles and the other Artist names of said CDs. Then, I need to ask the user for the number of CDs to be inputted before starting the input. This time, the loops are not separate and I need to input CD name and then CD artist one after the other.

Then some stuff on void function but I think I got all of that. However, I'm running into an odd problem. Here's my code. It's pretty much done and compiled, but I'm getting some odd output...

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
//Parallel Arrays 11-3
#include <iostream>
using namespace std;

//function prototype
void displayCDTitle();
void displayCDArt();

int main()
{
    //declare arrays
    string nameCD[20] = {""};
    string artistCD[20] = ("");
    
    //declare variables
    int numCD = 0;
    
    //Enter number of CDs
    cout << "Enter number of CD's: ";
    cin >> numCD;
    
    //Do while or For Loop here
    for (numCD = 0; numCD < 20; numCD += 1)
        {
               cout << "Enter name of CD"; 
               cout << numCD + 1 << ": ";
               cin >> nameCD[numCD];
               cout << "Enter the artist of " << nameCD[numCD] << ": ";
               cin >> artistCD[numCD];
               }
               
    //Output
    displayCDTitle();
    for (numCD = 0; numCD < 20; numCD += 1)
        {
               cout << nameCD[numCD] << endl;
               }
               
    displayCDArt();
    for (numCD = 0; numCD < 20; numCD += 1)
        {
               cout << artistCD[numCD] << endl;
               }
    system("pause");
    return 0;
}    
    //Function Definition
    void displayCDTitle()
    {
         cout << "CD Names" << endl;
         cout << "--------" << endl;
         }
    
    void displayCDArt()
    {
         cout << "Artists" << endl;
         cout << "--------" << endl;
         }



So, first off, I know my counter is off. Instead of limiting it to what number the user inputs, the program just asks for 20 right off the bat. That's the least of my worries.

My main problem is getting the program to accept multiple words as strings. Before, all I had to work with were simple numbers and a single word. Those work. I tried the program with just single words and it worked, sans the broken counter.

The second I put in a full title, i.e. "The Battle of Los Angeles". All heck breaks loose and the text gets really funky. It ends up only typing the first word, then a space, then goes into the next question, without allowing input, then goes to the CD after that.

That sounds like there isn't enough room for the title, or am I mistaken? Single words work just fine, but multiple words break the program without crashing it.

I'm assuming it has to do with the use of my variable and the array itself but I don't know what exactly is causing the problem. Everything else works, surprisingly. The Output display and the functions, it's just this last bit that's holding me back.

Any advice on where my problem lies?
cin stops reading in at the first whitespace reached. Use cin.getline() to read in multiple words.

I don't remeber exatly how getline work, it has arguments, but you can do a quick goole on it to figure it out.
Alright, made some changes but some more issues popped up... Here's a bit of the revision since the majority hasn't changed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//declare arrays
    string nameCD[20] = {""};
    string artistCD[20] = ("");
    
    //declare variables
    int numCD = 0;
    
    //Enter number of CDs
    cout << "Enter number of CD's: ";
    cin >> numCD;
    
    //Do while or For Loop here
    for (numCD = 0; numCD < 20; numCD += 1)
        {
               cout << "Enter name of CD"; 
               cout << numCD + 1 << ": ";
               getline(cin,nameCD[numCD]);
               cout << "Enter the artist of " << nameCD[numCD] << ": ";
               getline(cin,artistCD[numCD]);
               }


I tested the getlines and it worked in the end. However, using the getline screwed up the first line. Both couts appear on the same line. The first calling for the CD name gets negated and the input only counts for the artist.

I'm thinking it has something do with the counter pushing numCD up one before getting the nameCD input since after the first messed up line, everything evens out and the output works.
Alright, I'm at the home stretch. I ran the the program several times, worked out a problem with the display loops and now all that is left, is the first line problem and a bit on the counter. Here is the revised 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
//Parallel Arrays 11-3
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

//function prototype
void displayCDTitle();
void displayCDArt();

int main()
{
    //declare arrays
    string nameCD[20] = {""};
    string artistCD[20] = ("");
    
    //declare variables
    int numCD = 0;
    
    //Enter number of CDs
    cout << "Enter number of CD's: ";
    cin >> numCD;
    
    
    //Do while or For Loop here
    for (int x = 0; x <= numCD; x += 1)
        {
               cout << "Enter name of CD"; 
               cout << x + 1 << ": ";
               getline(cin,nameCD[x]);
               cout << "Enter the artist of " << nameCD[x] << ": ";
               getline(cin,artistCD[x]);
               }
               
    //Output
    displayCDTitle();
    for (int x = 0; x <= numCD; x += 1)
        {
               cout << nameCD[x] << endl;
               }
               
    displayCDArt();
    for (int x = 0; x <= numCD; x += 1)
        {
               cout << artistCD[x] << endl;
               }
    system("pause");
    return 0;
}    
    //Function Definition
    void displayCDTitle()
    {
         cout << "CD Names" << endl;
         cout << "--------" << endl;
         }
    
    void displayCDArt()
    {
         cout << "Artists" << endl;
         cout << "--------" << endl;
         }


My professor reported that the use of the getch() line after the cin CDnum would fix the getline issue. Odd, since it did nothing but require two enter presses after the initial prompt...nothing more.

So, the problem still lies within the beginning of the For loop. The program combines both couts and combines them, appearing like this:

"Enter name of CD:Enter the artist of:"

The entry for this line skips the first CD name and assigns whatever inputted to the first artist listed. After this, the loop works as intended, with a minor hitch, the numbering is off.

Number of CD's: 3
CD1: Then the messed up first line displays, taking an artist name and not CD name.

CD2-4: Works as normal. However, it always seems to add one to whatever is inputted. As in, I input 5, it asks for info on 6 CDs. I've got a burst of confidence after figuring out the display issue, just a bit more and this problem is solved...
As it would seem, another user, Shadow Fiend, posted some hep to another topic. This was spot on with what I was having trouble with and has fixed my getline woes.

Thanks to all who came to my aid!

The topic in question is:

http://www.cplusplus.com/forum/beginner/116368/
Last edited on
Topic archived. No new replies allowed.