Multiple User Input

Jun 4, 2010 at 6:36pm
I am fairly new to C++ and mostly used VB up to this point.

I am trying to create a program that allows me to input multiple names into an array. Here is my code so far.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{

int friends[3];
int x=0;

for (x=0;x<3;x++)

{
cout << "Please enter the name of person " << x+1 << endl;
cin >> friends[x];

}


cout << friends[0];


system("pause");
return 0;
}

My problem is the first loop cycle takes the cin statement, but as soon as I hit return it shows lines 2 and 3 without taking any more input. I am probably missing something very obvious like run the loop x amount of times or take user input however many times the upper limit to the For loop.
Jun 4, 2010 at 6:43pm
The problem is that the friends array is an array of ints, not strings. Here is my suggestion:

1
2
3
4
5
string friends[3];
for (int x = 0; x < 3; x++) {
	cout << "Please enter the name of person " << x << endl;
	cin >> friends[x];
}

Last edited on Jun 4, 2010 at 6:43pm
Jun 4, 2010 at 6:45pm
Thank you fafner! I new it was something that simple.
Topic archived. No new replies allowed.