0

I'm creating a console timetable application in which you can create a timetable, change it, and delete it. I'm on the stage of taking the input for the calculator. However, when I run the code, as soon as I finish taking the input, the window just closes. Here is the code:

int input()
{
    int numberOfElements;
    cout << "How many items do you want in your timetable? ";
    cin >> numberOfElements;
    char* itemArray[numberOfElements] = {};
    for (int i = 1; i <= numberOfElements; i++)
    {
        cout << "Please enter a session: ";
        cin >> itemArray[i];
    }
    for (int i = 0; i < numberOfElements; i++)
    {
        cout << itemArray[i] << "\n";
    }
    return 0;
}

There is some code in the main function as well, but it's irrelevant (only to find out for what day it is). I want you to have a look at the first for loop in the code, where I take in input. When running this code (in a separate window altogether), it closes as soon as I give in the input. Even if I say that I want 3 sessions (or any number), it closes right after I input the first session. In case you were wondering, I already tried replacing

char* itemArray[numberOfElements] = {};

with

char* itemArray[numberOfElements];

Just in case it's useful to anyone, I'm using the MinGW compiler.

Thanks.

5
  • 1
    Please just use std::vector<std::string> instead of messing with char pointers and arrays like this. char* itemArray[numberOfElements] is not allowed in standard C++ since the size of arrays needs to be known at compile-time. It is allowed only as a compiler-specific extension. You would also need to allocate memory of sufficient size for each pointer char* in the array manually, but please don't do that. cin >> itemArray[i]; does not allocate memory to store the string and using cin >> with a char* pointer is no longer allowed since C++20. Commented Apr 1, 2022 at 16:11
  • 1
    for (int i = 1; i <= numberOfElements; i++) did you mean to start at 0 and go to < numberOfElements? Commented Apr 1, 2022 at 16:11
  • 1
    cin >> numberOfElements; char* itemArray[numberOfElements] = {}; is not standard C++. Commented Apr 1, 2022 at 16:12
  • Welcome to the Stack. Note that it doesn't always look like this. A quick usage note: When asking a "why doesn't my code work?" type question, you should provide a runnable example that people can copy and paste into their tools, compile, run and see (within the bounds of what you can expect from Undefined Behaviour) what you see. Use minimal reproducible example for inspiration when making this example. In this case all you need is a small main that calls input and exhibits the same behaviour you see in your program and a few include directives. Commented Apr 1, 2022 at 16:30
  • Sometimes making this minimal example is all you need to do to reduce the noise around a bug to make the bug obvious and fix it yourself. Commented Apr 1, 2022 at 16:30

1 Answer 1

1

In Standard C++ the size of an array must be a compile time constant. So take for example the following statements in your program:

int numberOfElements;
cout << "How many items do you want in your timetable? ";
cin >> numberOfElements;
char* itemArray[numberOfElements] = {};//not standard C++

The statement char* itemArray[numberOfElements] = {}; is not standard C++ because numberOfElements is not a constant expression.

Additionally, you're going out of bounds of the array because of the <= in the for loop instead of <. This leads to undefined behavior.

Better would be to use std::vector<std::string> as shown below:

#include <iostream>
#include<vector>
#include <string>
int main()
{
   
    int numberOfElements;
    std::cout << "How many items do you want in your timetable? ";
    std::cin >> numberOfElements;
    std::vector<std::string> arr(numberOfElements); //create vector of size numberOfElements
    for (std::string &element: arr)
    {
        std::cout << "Please enter the element: ";
        std::cin >> element;
    }
    for (const std::string& element: arr)
    {
        std::cout << element << "\n";
    }

}

Demo.

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

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.