c++ - How do I use cin in a while loop? -
i'm trying user input name(s) using while loop array , cin, after last person's name input, program crashes instead of moving on. there way fix this, or need change code? i'm new c++, can answers given possible?
#include <iostream> #include <string> using namespace std; int main() { unsigned int numberofplayers; number://loop here if more 4 players cout << "number of players: "; cin >> numberofplayers; if(numberofplayers > 4 || numberofplayers < 1){ cout << "invalid number, please enter number 1 4." << endl; goto number; } string name[numberofplayers]; cout << "enter name" << endl; int = 1; while(a < numberofplayers + 1){ cout << "player " << << ": "; cin >> name[a]; cout << "hello, " << name[a] << "." << endl; a++; } }
you facing array index out of bound, change while loop , set a=0
fill 0th index.
while(a < numberofplayers){ }
Comments
Post a Comment