0

This is what I have to do:

A teacher has asked all her students to line up single file according to their first name. For example, in one class Amy will be at the front of the line and Yolanda will be at the end. Write a program that prompts the user to enter the number of students in the class, then loops to read in that many names. Once all the names have been read in it reports which student wourld be at the front of the line and which one would be at the end of the line. You may assume that no two students have the same name. Input Validation: Do not accept a number less than 1 or greater than 25 for the number of students.

This is what I have so far:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    int StudentNum;

    cout << "How many student are in the class?\n";
    cin >> StudentNum;

    char sname[StudentNum + 1][25];
    if (StudentNum < 1 || StudentNum > 25)
    {
    cout << "Please enter a number between 1-25 and try again\n"; 
    return 0;
    }

    for (int i = 1; i <= StudentNum; i++); 
    {
        cout << "Please enter the name of student #" << i << endl;
        cin >> sname[i];        
    }   
    for (int output = 0; output <=StudentNum; output++);
    {
    cout << endl << sname[output] << endl;
    } 
    system ("pause");
    return 0;
}

Am I missing something about arrays??

4 Answers 4

3

You cannot create such an array because its length has to be known at compile time (i.e., it cannot be the result of an expression such as StudentNum + 1).

You can solve this issue because by the problem definition you know an upper bound for the array size, so you can use that as a compile time constant.

However, this problem can be solved without using an array at all. Read the wording carefully.

Hint for the solution without arrays: Think of the array as a single piece of paper (variable) with all the names written one after another. Not using an array then means that you have to be able to solve the problem without looking at all the names at once. How would you come to the answer if I only allowed you to see the names one by one?

Another hint: The problem is still solvable if there were several trillion students in the class (with unique names no less), i.e. more than could possibly fit in the computer's memory at any one time.

Sign up to request clarification or add additional context in comments.

11 Comments

Then whats the best way to store something in a variable without knowing the actual number of objects you'll be storing until user input?
You gotta give me a hint man. This one problem has been making me mad haha...I know I can just make variables for all 25 names but I need a easy and practical way
@JoshLake: Why do you want to put all the names in an array?
I don't want to make separate variables and then do a bunch of else-if statements. Nor do I want to use switch.
@JoshLake: That's not an answer to my question. I know why you think you need an array. I want to make you think about it. The answer I wanted is "to store all the student names inside". Why do you want to store all the student names? Also, see the update I 'm about to make.
|
1

C++ array dimensions must be known at compile time (ie not dependent on user-entered variables at run-time). Use strings instead:

string sname[25];

If you were using something besides char arrays, you could also use a vector.

3 Comments

I do that but then I start getting an error in the for loop that says "22 name lookup of i' changed for new ISO for' scoping "
Why would I have to delete that?
@Josh I wasn't telling you to delete something, I was asking if you had deleted anything (such as comments) or if this was a total copy and paste of all the code. Anyways, the error is because of the semicolon at the end of line 20 (for (int i = 1; i <= StudentNum; i++);). There's also one at the end of line 25 that needs removed: for (int output = 0; output <=StudentNum; output++);.
0

Think about what the problem statement is actually asking for. Your program only needs to output the first and last names alphabetically. Do you actually need to store all the names to do that?

4 Comments

How else would you output the data without storing it?
You obviously need to store some names, but you only need to output two of them. Think about this, how long do you need to keep each name that was entered? I know I'm being cryptic, but I'm trying to help you think through the problem.
I know I could store it as 'first' and 'last' and then just compare the two but I was trying to look past that.
I'd go with that approach. There's no need to store the names that won't get output. Just keep a track of the current first and last names in your input loop.
0

Just for fun, here's how I would do it. Don't turn this in unless are ready to explain to your teacher how it works.

struct MinMax {
    std::string min;
    std::string max;
    MinMax& operator+(const std::string& kid) {
        if( min.empty() || kid < min) min = kid;
        if( max.empty() || kid > max) max = kid;
        return *this;
    }
};

int main() {
    int nKids;
    std::cout << "How many students? " << std::flush;
    std::cin >> nKids;

    std::cout << "Enter students' names, followed by EOF\n";
    MinMax mm(std::accumulate(
        std::istream_iterator<std::string>(std::cin),
        std::istream_iterator<std::string>(),
        MinMax()));
    std::cout << mm.min << ", " << mm.max << "\n";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.