Only accepting 4 inputs from user

Hello guys. I am a beginner in c++. I am trying a coding where user can only enter 4 characters for time1, time2, time3 but its ending up in crash. I couldnt paste the whole coding here because its too long but I guess this module is enough for you guys to help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  void add_movie()
{
    char name [25];
    char time1[4];
    char time2[4];
    char time3[4];

    fstream movie;
    movie.open("Movies.txt",ios::app);

    cin.ignore();
    cout<<" Enter the movie name: ";
    cin.getline(name,25);
    cout<<" Enter the time for First show  (HH:MM): ";
    cin.getline(time1,4);
    cout<<" Enter the time for Second show (HH:MM): ";
    cin.getline(time2,4);
    cout<<" Enter the time for Third show  (HH:MM): ";
    cin.getline(time3,4);
    movie << name<<'|'<<time1<<'|'<<time2<<'|' <<time3<<'\n';
    cout << endl << endl;
    cout<< "You are done, Manager!";
    movie.close();
}
Hello Mathavan,

If
1
2
3
char time1[4];
char time2[4];
char time3[4];

are to hold 4 characters where is there any room for the terminating null, (\0), to mark the end of the string?

Those arrays should have a size of 5 and it would be better to do something like constexpr int MAXSIZE{5}; then char time1[MAXSIZE];. This way it would be easy to change is you need something different also you would not be using magic numbers that could be missed if a change is needed.

Also a few blank lines would make the code easier to read unless you do not want anyone to read it because you are writing it for the compiler.

Lastly line 23 is not required as the dtor will close the file stream when the function looses scope, but OK if you leave it.

Andy
If the format is hh:mm, don't you want the : entered as well? - which makes a max of 5 chars plus the terminating null - 6 in total?

Hello Mathavan,

Consider this for you function:
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
int add_movie()  // <--- Changed.
{
    constexpr int MAXTIME{ 5 }, MAXNAME{ 26 };

    char name[MAXNAME];
    char time1[MAXTIME];
    char time2[MAXTIME];
    char time3[MAXTIME];

    ofstream movie("Movies.txt", ios::app);  // <--- Changed. An "fstream" is not needed because you do not read anything from the file.
    //movie.open("Movies.txt", ios::app);

    if (!movie)  // <--- Added.
    {
        //std::cerr << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
        std::cerr << "\n File \"" << "Movies.txt" << "\" did not open." << std::endl;

        return 1;
    }

    cin.ignore();  // <--- This may not be needed here. And may not be needed with the "cin.getline".

    cout << " Enter the movie name: ";
    cin.getline(name, MAXNAME);

    cout << " Enter the time for First show  (HH:MM): ";
    cin.getline(time1, MAXTIME);

    cout << " Enter the time for Second show (HH:MM): ";
    cin.getline(time2, MAXTIME);

    cout << " Enter the time for Third show  (HH:MM): ";
    cin.getline(time3, MAXTIME);

    movie << name << '|' << time1 << '|' << time2 << '|' << time3 << '\n';

    cout << endl << endl;

    cout << "You are done, Manager!";

    return 0;  // <--- Added. Meaning that all is good.
    //movie.close();  // <--- Not required as the dtor will close the file when the function looses scope.
}

Always good to check that the file stream opened even for an output file.

I also noticed that your prompts say: cout << " Enter the time for Third show (HH:MM): ";. That is 5 characters to start with, so your arrays would need to have a size of 6 for the "\0" at the end.

Andy
Topic archived. No new replies allowed.