Detecting string chars??

Pages: 12
I made something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (a=="1")
    Attack();
    else if (a=="2")  
    Block();
    else if (a=="3")  
    Cast();
    
    else // <<<<<<<<<< EXIT >>>>>>>>>>
    {
         cout <<"Bye =)" << endl;
         system("PAUSE");
         return;
        }


a is a string variable. How can I make this program end if string contains MORE than just ONE char.

i.e.
if a == 1 all works fine
if a == p program ends because a isn't 1 nor 2 nor 3
if a == 1 2 it ends because there is more than just one char
Last edited on
closed account (z05DSL3A)
Use string::size to find the number of characters in the string.

http://www.cplusplus.com/reference/string/string/size.html
tnx =)
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
What is your code now?
1
2
3
4
5
else if (a.size() > 1 || !((a=="1" || a=="2" || a=="3" || a=="!" || a=="?")) )// <<<<<<<<<< EXIT >>>>>>>>>>
    {
         cout <<"Bye =)" << endl;
         return;
    }
Which was the condition for the if matching that else?
PLs ignore sentences cuz they are on Croatian and I don't feel like translating right now...
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
if (a=="1")
    Attack();
    else if (a=="2")  
    Block();
    else if (a=="3")  
    Cast();
    
    else if (a=="?")// <<<<<<<<<< VJEVERICIN INFO >>>>>>>>>>
    {
    cout << "Vjeverica ima:" << endl << "Attack = " << vjev_att << endl <<"Armor = " << vjev_armor << " kvalitete " << vjev_armor_quality << endl << "Life = "  << vjev_life <<  endl << "+Bonus " << vjev_spell_res << " % spell resistance" << endl << endl;
    system("PAUSE");
    system("CLS");
    biraj();
    }
    
    else if (a=="!")// <<<<<<<<<< TVOJ INFO >>>>>>>>>>
    {
    cout << "Tvoji statovi i ta sranja:" << endl;
    cout << "Life = " << life << endl;
    cout << "Mane = " << mana << endl;
    cout << "Attack = " << player_att << endl;
    cout << "Ako probas blockat smanjit ces dmg koji ovisi o tvom armoru." << endl;
    cout << "Armor =  " << armor << " koji je " << armor_quality << " kvalitete." << endl;
    cout << "Spell damage = " << cast_dmg << " al gubis jednu manu." << endl << endl;
    system("PAUSE");
    system("CLS");
    biraj();
}
    else if (a.size() > 1 || !((a=="1" || a=="2" || a=="3" || a=="!" || a=="?")) )// <<<<<<<<<< EXIT >>>>>>>>>>
    {
         cout <<"Bye =)" << endl;
         return;
    }
    
}
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
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
Double quote = pointer to character sequence ( "Some text" )
Single quote = single character ( 'a' )

http://www.cplusplus.com/doc/tutorial/ntcs.html
http://www.cplusplus.com/doc/tutorial/constants.html
Last edited on
but a is string...
closed account (z05DSL3A)
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
@ 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...
a is a string, a[i] is a char
@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
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
closed account (z05DSL3A)
Sorry for the confusion with the bool, I was on auto pilot with the while loop, then thought that I would match the same structure if..else if..else that you posted but forgot to delete the bool guard.

This is what I was originally going for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    bool finished = false;
    while(!finished)
    {
        
        std::getline(std::cin, a);
        // compare with
        // std::cin >> a;

        if (a == "1")
        ...
        else //everything else fails
        {
            finished = true;
        }

    }
    std::cout <<"Bye =)" << std::endl;
    system("PAUSE");
    return 1;



Do you understand the cause of the error and why changing cin >> a to getline(cin, a) fixed it?
I think so but I'm not positive so I don't won't to blab something to flame myself xD
Pages: 12