Codes

Nov 25, 2013 at 12:40pm
I need to get an input which takes a bunch of numbers you insert so like 294745
makes 2 footmen 9 archers 4 knights 7 ghosts 4 zombies 5 skeletons
is there any way to do this in one line of input

1
2
3
4
5
6
7
8
9
10
11
//.....
int footmen;
int archers;
int knights;
int ghosts;
int zombies;
int skeletons;
int code;
cout << "Code Please";
cin >> code;
//........... 
Nov 25, 2013 at 12:49pm
That'd be easy:
1
2
cout << "Code Please";
cin >> footmen >> archers >> knights >> ghosts >> zombies >> skeletons;
I wouldn't recommend that though
Nov 25, 2013 at 12:51pm
thanks mate and why wouldn't yourecommend it?
Nov 25, 2013 at 1:06pm
I forgot to say that you need to enter space in order to separate the numbers.

why wouldn't yourecommend it?
It's way to obscure.

if you want it that obscure and no space you could to this (it does not allow numbers > 9):
1
2
3
4
5
6
7
8
9
10
11
...
string code;
cout << "Code Please";
getline(cin, code);
if(code.size() >= ...)
{
  footmen = code[0] - '0';
  archers = code[1] - '0';
  knights = code[2] - '0';
  ...
}
Nov 25, 2013 at 1:09pm
And if i would want to use letters in the code to times the numbers like b2 would be 4 (b=2 x 2) or k9 = 99
is there a way to do that
Nov 25, 2013 at 8:52pm
Sure, a string can contain everything. you just need to identify it
Nov 26, 2013 at 7:23am
Thanks im still a bit new don't really know how to use strings
Nov 26, 2013 at 12:49pm
you can use a string like an array. The function size() (as shown above) tells how many char the string contains. See:

http://www.cplusplus.com/reference/string/string/?kw=string

just try
Topic archived. No new replies allowed.