Feb 12, 2009 at 3:19pm UTC
Last edited on Feb 12, 2009 at 3:24pm UTC
Feb 12, 2009 at 3:28pm UTC
Use string::size to find the number of characters in the string.
http://www.cplusplus.com/reference/string/string/size.html
Feb 12, 2009 at 3:48pm UTC
Oh if I write 1 2 it still runs before it shuts down...
First Attack() runs and then Block() and program closes afterward...
EDIT
Excuse me my mistake program doesn't shut down afterward it just runs Attack() and Block()
Last edited on Feb 12, 2009 at 3:52pm UTC
Feb 12, 2009 at 4:09pm UTC
Which was the condition for the if
matching that else
?
Feb 12, 2009 at 4:21pm UTC
You are checking the same way as before...
Try with a loop
1 2 3 4 5 6 7 8 9 10
for (unsigned i=0; i<a.size(); i++)
if ( a[i] /*(i-1)th catacter in 'a'*/ == '1' )
//...
//else if...
// if you need skip spaces
else
{
cout << "Bye =)" << endl;
return ;
}
Last edited on Feb 12, 2009 at 4:21pm UTC
Feb 12, 2009 at 4:23pm UTC
lol, I tried to avoid different checking because I don't understand your way of checking =P
I mean, I do but i don't understand stuff like
1 2
for (unsigned i=0; i<a.size(); i++)
if ( a[i] /*(i-1)th catacter in 'a'*/ == '1' )
I AM NOOB as you can see =P
EDIT
if i write
1 2
for (unsigned i=0; i<a.size(); i++)
if ( a[i] == "1" )
i get an error about comparing pointer with an integer
Last edited on Feb 12, 2009 at 4:31pm UTC
Feb 12, 2009 at 4:35pm UTC
Last edited on Feb 12, 2009 at 4:36pm UTC
Feb 12, 2009 at 4:38pm UTC
How are you getting 'a', If you are just doing
cin >> a;
, that may be the cause of your problem.
Have a look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include<iostream>
#include<string>
int main()
{
std::string a;
bool finished = false ;
while (1)
{
std::getline(std::cin, a);
// compare with
// std::cin >> a;
if (a == "1" )
{
std::cout << "Attack();" << std::endl;
}
else if (a =="2" )
{
std::cout << "Block();" << std::endl;
}
else if (a == "3" )
{
std::cout << "Cast();" << std::endl;
}
else //everything else fails
{
std::cout <<"Bye =)" << std::endl;
system("PAUSE" );
return 1;
}
}
return 0;
}
Last edited on Feb 12, 2009 at 4:41pm UTC
Feb 12, 2009 at 4:41pm UTC
@ Grey wolf
yes I am using cin...
I'll change later I must go now,I'll return in about 30 mins and try it...
Feb 12, 2009 at 4:42pm UTC
a is a string , a[i] is a char
Feb 12, 2009 at 4:59pm UTC
@Grey Wolf
i don't understand why you are using bool when it's always true since you don't change it in the code...
@Bazzy
tnx for the explanation of a and a[1]
EDIT sry grey now i saw it xP
Last edited on Feb 12, 2009 at 5:06pm UTC
Feb 12, 2009 at 5:25pm UTC
I think I'm going with Grey Wolfs way just because it's much simpler to me and thus I understand better what I'm doing =P
Just one more problem...
How can I return 1 in void?
EDIT
Never mind I got it and btw, you don't need bool finished = false ;
Last edited on Feb 12, 2009 at 5:32pm UTC
Feb 12, 2009 at 6:20pm UTC
I think so but I'm not positive so I don't won't to blab something to flame myself xD