0

Please help!!

i don't know why this is wrong! Forgive me lack of semicolons etc. I am retyping on my phone. it compiles ok on Visual Studio so I am assuming I have typed it correctly, but crashes when running int x; int n; string player [4] int main() { cout << "Number of players?"; cin>>n; for (x=1; x<n;x++); { cout << "Name of player " << x; cin>> player [x]; } for (x=1; x<n;x++); { cout << "player " << x << " is "<< player [x]; } cout<< "Number of players is "<< n; }

21st Jul 2017, 3:32 AM
Richard Appleton
Richard Appleton - avatar
7 Answers
+ 1
You're missing a semicolon where you declare players. Also remember arrays are zero based. x should start at 0 not 1. Then in your cout you can output x+1 instead of just x. In the playground all of your inputs must be done before you submit. So if you wanted 5 players you would enter: 4 player1 player2 player3 player4 Then submit You also have some semicolons in the wrong place just after the for loops parentheses. Remove them. #include <iostream> using namespace std; int x; int n; string player[4]; int main() { cout << "Number of players? " << endl; cin>>n; for (x=0; x<n;x++) { cout << "Name of player " << x+1 << endl; cin>> player [x]; } for (x=0; x<n;x++) { cout << "player " << x+1 << " is "<< player [x] << endl; } cout<< "Number of players is "<< n; }
21st Jul 2017, 4:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
you forgot to close for loop and main function. return 0; is missing.
21st Jul 2017, 4:31 AM
‎ ‏‏‎Anonymous Guy
+ 1
sorry wrong post on wrong thread and why did he put semicolon after for loop
21st Jul 2017, 4:50 AM
‎ ‏‏‎Anonymous Guy
+ 1
Because I have been using C++ for about 2 days and I am still learning!
21st Jul 2017, 5:32 AM
Richard Appleton
Richard Appleton - avatar
0
So it crashes on the array data entry line. if I // that to make it a comment, it will only run through once, giving x a value from I don't know where and giving n a ridiculous value. (like 38 when I enter 4)! Again, I don't know where this has come from!! I took the basic data entry loop from a c++ tutorial site.
21st Jul 2017, 3:34 AM
Richard Appleton
Richard Appleton - avatar
0
@Sreejith Both for loops are closed, but he has a semicolon after the closing parantheses and before the opening curly brace which effectively makes both the for loop bodies code blocks. Also with some compilers the return 0 for the main function in c++ is implicitly applied, including the one in the playground.
21st Jul 2017, 4:37 AM
ChaoticDawg
ChaoticDawg - avatar
0
Thanks ChaoticDawg. Very helpful and informative answer. :-)
21st Jul 2017, 5:35 AM
Richard Appleton
Richard Appleton - avatar