Can't enter data in variable

This is the full program code. Its still unfinished but if I choose option 1 and enter the names. It goes to keepGoing function but it won't let me enter Y or N. It goes straight to the end of the program.
Why ?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

// Declare a structure for the record.
struct Students
{
       char name;
};

// FUNCTION PROTOTYPES

void addStudent(string[], int);
void stuAttendance();
void stuReport();
void showMenu();
void keepGoing();

int main ()
{

    //Open file 
    //fstream attendance("Student Attendance.txt", ios::out | ios::in);
    
    
    //VARIABLES AND CONSTANTS
    //variables
    int numStudents;
    int menuChoice;
    int size;
    string place;
    int num;
    //constants
    const char ADD_STUDENT = 1,
               ATTENDANCE = 2,
               REPORT = 3;
    //ARRAYS
    string stuInfo[numStudents];
    
    
    // Introduction
    cout << "Welcome to the attendance calculator: " << endl << endl;
    
    // Accept number of students
    cout << "\nPlease enter the number of students you have between 2 and 30 \n"
         << "or -1 to quit: ";
    cin >> numStudents;
    
    if (numStudents == -1)
    {
        cout << "!!Exiting program!!\n";
        system("pause");
        return 0;
    } else if (numStudents < 2 || numStudents > 30)
       {
           cout << "Please enter a number between 2 and 30\n";
           cin >> numStudents;
       }
    cout << numStudents << "\n";
    
    
    
    // GET OPTIONS FROM USER
    showMenu();
    cin >> menuChoice;
    
    
    //Switch for menuChoice
    if (menuChoice == 1)
       addStudent(stuInfo, numStudents);
    else if (menuChoice == 2)
       stuAttendance();
    else if (menuChoice == 3)
       stuReport();
       
    keepGoing();
    
    //cout << "Would you like to continue?";
    //cin >> place;
    //cout << "Enter a number please work: ";
    //cin >> num;
    
    /*if (proceed == 'Y' || proceed == 'y')    
       {
           showMenu();
       }
     else 
        cout << "Thank you for using Attendance Program.";*/
    
                   
    
    system("pause");
    return 0;
}
//*************************************************************
//showMenu function shows the choices for the program to run  *
// choices of S A or R.                                       *
//*************************************************************

void showMenu()
{
     cout << "Please make a choice from the menu.\n" << endl;
     cout << "1. Add a student\n"
          << "2. Enter attendance\n"
          << "3. See report\n"
          << ":: ";
          
}
//*******************************************************
// addStudent function allows user to enter a  students *
// information into the system.                         *
//*******************************************************

void addStudent(string nStudent[], int size)
{
     
     
     cout << "Welcome to addStudent" << endl;
     cout << "\nPlease enter the student information: " << endl;

     
     for (int index = 0; index < size; index++)
     {
         cout << "Enter the name of new student #" << (index+1)
              << ": ";
         getline(cin, nStudent[index]);
         cin.ignore();
     }
     
}

//******************************************************
// stuAttendance function alow a user to add students  *
// attendance record to the report.                    *
//******************************************************

void stuAttendance()
{
     cout << "Welcome to stuAttendance." << endl;
}

//*****************************************************
// stuReport function displays a students attendance  *
// record for the user to view                        *
//*****************************************************

void stuReport()
{
     cout << "Welcome to stuReport." << endl;
}
//******************************************************
// keepGoing function asks the user if they would like *
// to continue with the program                        *
//******************************************************

void keepGoing()
{
     string place;
     cout << "Would you like to continue? Please enter Y or N." << endl;
     cin >> place;
}
Last edited on
line 10: This allocates only a single character for name.

line 40: How many instances of stuInfo do you think are allocated here?
Hint: numStudents is uninitialized.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/

thanks for the code tags info. I was wondering about that. The user initializes the number of students. The programs not complete I just cant figure out why it will let me allow the Y or N before the menu loop, but if I place it after the loop it doesn't allow it. If I run the program as its written now select 2 students enter their name it asks to continue Y or N and goes straight to the end. I added the function to see if it would help. But that didn't work either.
The user initializes the number of students

No. In C++ arrays are allocated at compile time. The value of numStudents is not known at compile time.

You have some data left over in the cin buffer.
I added a cin.ignore (10,'\n'); after line 160 and the program ran as expected.
Last edited on
line 47 - 49 asks the user for the number of students.
Read what I said. arrays are allocated at compile time.
How does the compiler know how many students the user is going to enter?
Don't know but it works when I run it.
@AbstractionAnon
How does the compiler know how many students the user is going to enter?
@musicman74
Don't know but it works when I run it.

Then change your IDE to the one that does not allow such tricks, as they will work against your effort put in learning C++. Basically, your IDE by some additional plugin denies C++ standards and allows simplification, that would normally be an error, without you even noticing. (Code::Blocks?)
Last edited on
mannn! Now I gotta re-rewrite my program.

Thanks for the help guys.
Topic archived. No new replies allowed.