Home > Software engineering >  C for loop ends after input
C for loop ends after input

Time:12-05

#include <iostream>
#include <string>
using namespace std;

struct Student {
    string name;
    string hometown;
    int age;
    int games;
    int* hours = new int [games];
};

int main(){
    int players;
    Student* s = new Student [players];

    cout << "How many esports players are there at TTU who major in csc?\n";
    cin >> players;
    cout << "\nPlease enter in information about each player: \n\n";

    for(int i = 0; i < players; i  ){
        cout << "PLAYER " << (i   1) << ":\n";
        cout << "\tNAME:\t";
        getline(cin >> ws, s[i].name);
        cout << "\tHOMETOWN:\t";
        getline(cin >> ws, s[i].hometown);
        cout << "\tAGE:\t";
        cin >> s[i].age;
        cout << "\tHOW MANY GAMES DOES " << s[i].name << " PLAY?\t";
        cin >> s[i].games;
            for(int j = 0; j < s[i].games; j  ){
                cout << "NUMBER OF HOURS " << s[i].name << "PLAYED GAME"  << (j   1) << "\t";
                cin >> s[i].hours[j];
            }
    } 

    return 0;
}

For some reason, the for loop is ending after asking for age. I have tried with multiple inputs and it does the same thing. I am new to c . Does anyone know why? Thank you for your time

CodePudding user response:

You are not handling arrays correctly. You are allocating them using uninitialized variables for their sizes:

  • in int games; int* hours = new int [games];, games is uninitialized.
  • in int players; Student* s = new Student [players];, players is uninitialized.

And, even if you were allocating the arrays correctly, you would be leaking them, since you are not freeing them when you are done using them.

When you need to use an array whose size is not known until runtime, you are correct to use new[], but not until after you determine the size, eg:

#include <iostream>
#include <string>
using namespace std;

struct Student {
    string name;
    string hometown;
    int age = 0;
    int games = 0;
    int* hours = nullptr;
};

int main(){
    cout << "How many esports players are there at TTU who major in csc?\n";

    int players;
    cin >> players;

    Student *s = new Student[players];

    cout << "\nPlease enter in information about each player: \n\n";

    for(int i = 0; i < players;   i){
        cout << "PLAYER " << (i   1) << ":\n";
        cout << "\tNAME:\t";
        getline(cin >> ws, s[i].name);
        cout << "\tHOMETOWN:\t";
        getline(cin >> ws, s[i].hometown);
        cout << "\tAGE:\t";
        cin >> s[i].age;
        cout << "\tHOW MANY GAMES DOES " << s[i].name << " PLAY?\t";
        cin >> s[i].games;
        s[i].hours = new int[s[i].games];
        for(int j = 0; j < s[i].games;   j){
            cout << "NUMBER OF HOURS " << s[i].name << " PLAYED GAME " << (j   1) << "\t";
            cin >> s[i].hours[j];
        }
    }

   // use player info as neded...

    for(int i = 0; i < players;   i){
        delete[] s[i].hours;
    }
    delete[] s;

    return 0;
}

That being said, you should use std::vector instead of new[]/delete[] manually, eg:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Student {
    string name;
    string hometown;
    int age = 0;
    int games = 0;
    std::vector<int> hours;
};

int main(){
    cout << "How many esports players are there at TTU who major in csc?\n";

    int players;
    cin >> players;

    std::vector<Student> s(players);

    cout << "\nPlease enter in information about each player: \n\n";

    for(int i = 0; i < players;   i){
        cout << "PLAYER " << (i   1) << ":\n";
        cout << "\tNAME:\t";
        getline(cin >> ws, s[i].name);
        cout << "\tHOMETOWN:\t";
        getline(cin >> ws, s[i].hometown);
        cout << "\tAGE:\t";
        cin >> s[i].age;
        cout << "\tHOW MANY GAMES DOES " << s[i].name << " PLAY?\t";
        cin >> s[i].games;
        s[i].hours.resize(s[i].games);
        for(int j = 0; j < s[i].games;   j){
            cout << "NUMBER OF HOURS " << s[i].name << " PLAYED GAME " << (j   1) << "\t";
            cin >> s[i].hours[j];
        }
    } 

    // use player info as needed...

    return 0;
}
  • Related