Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with taking input... 1

Status
Not open for further replies.

thunderclees

Programmer
Jun 17, 2003
3
0
0
US
I'm trying to take in user input to get a number for each member of an array. I'm trying to loop the question and take input each time the loop runs, but I'm losing myself... heres what i have...

const int numAliens = 7;
int casName[numAliens];
const char *species[numAliens] =
{"alien1","alien2","alien3","alien4","alien5","alien6",
"alien7"};

for(int i = 0; i < numAliens; i++)
cout << &quot;Please enter the casualty amount for <<
species << &quot;.&quot;;
cin >> species;


Thats as far as i got, it wont compile, i dont know if im going about it in the wrong way or what. Thanks for the help.
 
You need a struct to encapsulate the alien name and casualty. As it is, you're trying to write the casualty into the alien name.

struct alien {
char *name;
int casualty;
} species[] = {
{ &quot;alien1&quot;, 0 },
{ &quot;alien2&quot;, 0 },
{ &quot;alien3&quot;, 0 },
{ &quot;alien4&quot;, 0 },
{ &quot;alien5&quot;, 0 }
};

// ...

for(int i = 0; i < sizeof species / sizeof *species; i++)
cout << &quot;Please enter the casualty amount for <<
species.name << &quot;.&quot;;
cin >> species.casualty;






 
Cool gotcha. I had to look up a cpl things you were doing.. not there yet. but i got it now =) Thanks!
 
Another thing which is missing : curly braces :


for(int i = 0; i < sizeof species / sizeof *species; i++)
{
cout << &quot;Please enter the casualty amount for <<
species.name << &quot;.&quot;;
cin >> species.casualty;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top