I'm new here, and haven't posted anywhere for any help since I've been all about learning for myself. However, I've gotten to the point where I don't want to fail this class so I'm seeking as much help as I can! I don't want it done for me, just a little guidance in the right direction since I'm just at a loss as to where to continue.

The assignment asks for the input of 10 students names that are going to be stored into an array. Then I need a parallel array that will hold the final test scores of each of the students. The output should be similar to:

Joe Smith [tab] 93
... ... ...
and so forth for each of the ten students.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()

    // declare the variables
    int x = 0; // names
    int y = 0; // scores

    // declare arrays
    string names[10];
    int scores[10];

    // input data into the names array
    for (x = 0; x < 10; x++)
        cout << "Enter a student's name: ";
        cin >> names[x];

    // input test data
    for ( y = 0; y < y; y++)
        cout << "Enter test score for " << names[x] << ": ";
        cin >> scores[y];

    // display name and score
    // i know what all goes here, i just need to get the test data loop to work

    return 0;

It asks me for the student's names but it won't ask me for the test scores. I'm uncertain as to what I'm doing wrong?

Recommended Answers

multi-statement for loops and if statements require { and } around them

for(i = 0; i < 10; i++)
   // blabla

line 23: y < y will never ever be true. Probably should be y < 10

Jump to Post

All 5 Replies

multi-statement for loops and if statements require { and } around them

for(i = 0; i < 10; i++)
   // blabla

line 23: y < y will never ever be true. Probably should be y < 10

commented: I can't believe I overlooked the curly Qs. Changing line 23 helped also. Now the only problem I'm having is that when it asks for input for the test scores, it's only asking for the last person's name entered. +0

Also, if you intend to get firstname lastname in one string input, you'll need getline()

cin >> a_string;will store just one word's worth of content, stopping at the space between name parts.

getline( cin, a_string ); will read everything till the newline.

In line 24, if you want to display a name for the student, you need to use the same index variable as the loop's counter.

Ancient Dragon: I can't believe I forgot the curly Qs. I blame it on being sleepy. I fixed everything else and only have one one error [see below].

vmanes: It's actually just last name, but that's good to keep in mind for future reference, :) I think I fixed line 24.

My issue now is that I can get the input for the test scores, but it just says "enter test score for: " and it doesn't give the student's name that is stored in the array... only for the first name in the array.

[As an aside, I'm trying to figure out how to comment on here, so if I repeat myself somewhere along the lines, I apologise.]

Ha. Nevermind. I forgot to change one lousy index variable and it was throwing it off. Got that all squared away.

Just a quick question, though. I have the display as

cout << names[0] << scores[0] << endl;
cout << names[1] << scores[1] << endl;

and so on for each student. Is there an easier way of doing the output instead of manually?

use a loop like you already have, and use the same loop counter variable as the index for names and for scores.

Don't forget to put some space between the two in your output statement.