I am programming a number of students and quiz questions and correct answers program for my school's programming II class. I have an ok knowledge of C++, but I cannot understand the reason why my code does not work. I have created an integer of num of students, I asked the question of how many students do you have, then I made a const int Num Of Students equaling the user's input and then I set student names with Number Of Students like so: const string student_name[Number_Of_Students];

What am I doing wrong???

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
    int number_of_students;

    cout << "how many students do you have? ";
    cin >> number_of_students;
    cout << endl;

    const int Number_Of_Students = number_of_students;

    const string student_name[Number_Of_Students];
    system("pause");
    return 0;
}

Let's start with the I'm a C guy, but this C++ stuff sounds really neat approach:

#include <iostream>
#include <string>
#include <cmath>
//#include <math.h>  //Don't need this if you have cmath.
#include <algorithm>

using namespace std;

int main()
{
    int number_of_students, i;
    string dummy, *student_name; //Declare student_name as a pointer
    cout << "how many students do you have? ";
    cin >> number_of_students;
    getline(cin,dummy); //Flushes dangling new-line. Needed later for getline.
    cout << endl;

    //string *student_name =new string[number_of_students];
    student_name =new string[number_of_students]; //Allocate student_name with new[]
    for(i=0;i<number_of_students;i++){
        cout<<"Enter name of student["<<(i+1)<<"]: "; //Using i+1 because no one wants to be student[0]
        getline(cin,student_name[i]);
    }

    cout<<"These are the students.\n";
    for(i=0;i<number_of_students;i++){
        cout<<"Student["<<(i+1)<<"]: "<<student_name[i]<<endl;
    }

    delete student_name; //Free memory allocated by student_name.

    //Some DaniWeb veterans get very vehement when you use system("pause");
    cout<<"Please press Enter to exit...";
    getline(cin,temp);  //Flushes dangling new-line.

    return 0;
}

And, as far as C++ is concerned, this is the wrong way even though its correct.
Let's get to actual Standard Template Library stuff.

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int number_of_students, i;
    string temp; //Actually a very cool STL data structure, but doesn't get the respect it deserves.
    vector<string>student_name; //Now we're cooking with STL!
    cout << "how many students do you have? ";
    cin >> number_of_students;
    cout << endl;

    getline(cin,temp);  //Flushes dangling new-line.
    for(i=0;i<number_of_students;i++){
        cout<<"Enter name of student["<<(i+1)<<"]: ";
        getline(cin,temp);
        student_name.push_back(temp); //Adds the student's name to the list
    }

    cout<<"These are the students.\n";
    for(i=0;i<number_of_students;i++){
        cout<<"Student["<<(i+1)<<"]: "<<student_name[i]<<endl;
    }

    cout<<"Please press Enter to exit...";
    getline(cin,temp);  //Flushes dangling new-line.

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.