-1
#include <iostream>
#include "Stud.h"

using namespace std;

int main()
{
    short x;

    cout << "Hello and Welcome to the minigame!" << endl;

    cout << "Please state how many students you would wish to add to the mini-game (Your choice cannot be changed later so be careful!)" << endl << "~> ";
    cin >> x;
    cout << endl << endl;

    // Get this fixed!!!
    Stud st[x];
    cout << "Now please input the information of your student('s)" << endl << endl;
    for (int i = 0; i <= x; i++) {
        cout << "Student Number " << i + 1 << ", What is your Name? " << endl << "~> ";
        st[x].getName();
        cout << endl;
    }


    cout << "Check!";
    return 0;
}

Line 24: Stud st[x];

I want to make it so that the user can choose how many students get to participate but can't seem to understand how not to get the "expression must be a constant value error...

I've managed to temporarily fix it by changing st[x] to st[50] and st[x].getName to st[i].getName so that I can attempt other functions while I'm learning what I can do about it...

  • Using `i <= x` for `Stud st[x];` is [off-by-one error](https://en.wikipedia.org/wiki/Off-by-one_error). – MikeCAT May 12 '21 at 14:45
  • 1
    Consider using [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead of the non-standard VLA (Variable-Length Array). – MikeCAT May 12 '21 at 14:45
  • `std::vector st(x);` and fix `for (int i = 0; i <= x; i++) {` to `for (int i = 0; i < x; i++) {` – drescherjm May 12 '21 at 14:47
  • 1
    Ah, `st[x].getName();` is an out-of-range access even if the condition were `i < x`. – MikeCAT May 12 '21 at 14:47
  • And as @MikeCAT pointed out `st[i].getName();` instead of `st[x].getName();` – drescherjm May 12 '21 at 14:48
  • @drescherjm i changed the for loop but am confused by "std::vector st(x) as in where do i put it. – ANONYMOUSEJR May 12 '21 at 14:51
  • Also, @MikeCat could you please tell me where I should put std::vector, rather what I should replace it with? – ANONYMOUSEJR May 12 '21 at 14:58
  • If you look in your C++ textbook's chapter that explains what `std::vector` is, and how to use it, it will answer all of your questions. Is there anything in your C++ textbook's explanation or `std::vector` tutorial that's unclear to you? – Sam Varshavchik May 12 '21 at 14:59
  • You should replace `Stud st[x];` with `std::vector st(x);`. – MikeCAT May 12 '21 at 14:59
  • @MikeCAT thank you very much the code works now (though there is a syntax bug that I'll need to chew on for a bit though I think it's workable). – ANONYMOUSEJR May 12 '21 at 15:07
  • @SamVarshavchik oh it's not like that, I don't really *Have* a textbook it's more like our teacher explaining a topic and me taking notes... in fact, I hadn't even known something like ```std::vector``` existed until @MikeCAT had suggested it... Our curriculum didn't call for it. – ANONYMOUSEJR May 12 '21 at 15:08
  • @MikeCAT umm sorry to disturb you but I have one more problem... in the for loop; the ```st[i].getName;``` is skipped for the first student and only asks for the other two, do you have any advice for that? – ANONYMOUSEJR May 12 '21 at 15:35
  • C++ is the most complicated general purpose programming language in use today. You cannot learn C++ just by asking one question at a time. The only way to effectively learn it is with a good textbook. – Sam Varshavchik May 12 '21 at 16:00
  • You put `std::vector st(x);` where you have this: `Stud st[x];` which is invalid in standard `c++` because of this: [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm May 12 '21 at 16:00
  • Not knowing the definition of `getName`, sounds like this: [c++ - Why does std::getline() skip input after a formatted extraction? - Stack Overflow](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – MikeCAT May 12 '21 at 17:02

0 Answers0