Need help With case structure

So im writing code for a game, everything is working fine except when i press a letter instead of a number e.g. "w" instead of "1" it showing the message i want to appear but after that i can't put in any more input it just shuts down, does anyone know howto fix this?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <windows.h>

using namespace std;
int health = 100;

void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}




int main()
{
    int gold;
    int streghth;
    int agility = 5;
    int intelegence = 5;
    int attack = 0;
    srand(time(0));
    int enemy_health[3] ={20,30,40};
    int enemy_attack[3] ;
    string help;



    string characters[4] = {"Ork", "Ork", "Ork", "Ork"};
        int x = rand() % 4;

if(characters[x] == "Ork"){
cout << "\nYou have encountered an ";
setcolor (15);
cout << "Unstopable force named Moash";
setcolor (7);
cout << "\nWhat do you want to do?\n\n[1] Attack\n[2] Run\n";
cin >> attack;

while (attack <3){

switch(attack)
{
    case 1:
        srand(time(0));
        streghth = rand() % 12 + 8 ;
        enemy_attack[0] = rand() % 5 + 1 ;

if (enemy_health[0] >= 1 and health >=1 ){
enemy_health[0] = enemy_health[0] - streghth;
health = health - enemy_attack[0];
cout << "\nyour health is " << health << "\n";
cout << "\nthe enemys health is " << enemy_health[0] << "\n";
attack <= 0;
cin >> attack;

}
else if (enemy_health[0] <1){
cout << "YOU WIN";
gold = gold + 5;
cout << gold;
return 0;
}
else if (health <1){
cout << "YOU LOST";
return 0;
}
   break;
    case 2:
    cout << " got away safly!";
    return 0;
    break;

default:
        cout << "what did you want to do again?";
        return 0;
        cin>> attack;
}
}
}
}

Ints only take numbers, so entering a char is an error. There are ways to handle it. Have a read here:

http://www.cplusplus.com/forum/beginner/89680/#msg481792
closed account (3qX21hU5)
1
2
3
4
default:
        cout << "what did you want to do again?";
        return 0;
        cin>> attack;


The return statement is telling the program to exit. Though once you remove that it will enter a infinite loop as Codeez mentioned because you can't assign a char to a integer. Here is one way to handle it.

1
2
3
4
cout << "what did you want to do again?";
cin.clear();
cin.ignore();
cin>> attack;


First you clear the error state of the stream, then ignore any extra things in the buffer and then you are ready to grab new input.


Anyways you have more problems to worry about. I am not sure how this program even compiles for you... For one on line 54 there is no such thing as "and" as a operator. What you are looking for is &&.

Also on line 65 you use the variable "gold" when it is uninitialized which will cause undefined behavior. Make sure you define a value to a variable before using it.

I would also recommend looking at how you indent your code. Using a different level of indention for each scope helps to keep the code much more readable. For example here is your program with the usual indention.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <windows.h>

using namespace std;
int health = 100;

void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}




int main()
{
    int gold = 0;
    int streghth;
    int agility = 5;
    int intelegence = 5;
    int attack = 0;
    srand(time(0));
    int enemy_health[3] ={20,30,40};
    int enemy_attack[3] ;
    string help;



    string characters[4] = {"Ork", "Ork", "Ork", "Ork"};
    int x = rand() % 4;

    if(characters[x] == "Ork"){
        cout << "\nYou have encountered an ";
        setcolor (15);
        cout << "Unstopable force named Moash";
        setcolor (7);
        cout << "\nWhat do you want to do?\n\n[1] Attack\n[2] Run\n";
        cin >> attack;

        while (attack <3){

            switch(attack)
            {
                case 1:
                    srand(time(0));
                    streghth = rand() % 12 + 8 ;
                    enemy_attack[0] = rand() % 5 + 1 ;

                    if (enemy_health[0] >= 1 && health >=1 ){
                        enemy_health[0] = enemy_health[0] - streghth;
                        health = health - enemy_attack[0];
                        cout << "\nyour health is " << health << "\n";
                        cout << "\nthe enemys health is " << enemy_health[0] << "\n";
                        attack <= 0;
                        cin >> attack;

                    }
                    else if (enemy_health[0] <1){
                        cout << "YOU WIN";
                        gold = gold + 5;
                        cout << gold;
                        return 0;
                    }
                    else if (health <1){
                        cout << "YOU LOST";
                        return 0;
                    }
                    break;

                case 2:
                    cout << " got away safly!";
                    return 0;
                    break;

                default:
                    cout << "what did you want to do again?";
                    return 0;
                    cin>> attack;
            }
        }
    }
}


Just adding consistent indentation increased the readability a lot. I would recommend figuring out a style you like personally and use it consistently with everything you do. It doesn't matter what your style is (As long as it isn't way out there) as long as you do it consistently.
Last edited on

[quote=Zereo (1882)]For one on line 54 there is no such thing and "and" as a operator. What you are looking for is &&.[/quote]

Sorry to pull you up on this minor point buddy, but and is a valid operator in c++, it is just that everyone is much more used to the traditional &&

There are others too:

http://stackoverflow.com/questions/2393673/c-and-or-not-xor-keywords


Hope all is well at your end.

EDIT:

As you said, the OP has bigger problems, which you have gone a long way to address in your last edit.
Last edited on
thanks for the help, i got it working and yeah i will start to make indentations i can understand where i need to work form now haha heres the finished 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <windows.h>
#include <limits>

using namespace std;
int health = 100;

void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}




int main()
{
    int gold = 0;
    int streghth;
    int agility = 5;
    int intelegence = 5;
    int attack = 0;
    srand(time(0));
    int enemy_health[3] ={20,30,40};
    int enemy_attack[3] ;
    string help;



    string characters[4] = {"Ork", "Ork", "Ork", "Ork"};
    int x = rand() % 4;

    if(characters[x] == "Ork"){
        cout << "\nYou have encountered an ";
        setcolor (15);
        cout << "Unstopable force named Moash";
        setcolor (7);
        cout << "\nWhat do you want to do?\n\n[1] Attack\n[2] Run\n";
        cin >> attack;

        while (attack < 3){

            switch(attack)
            {
                case 1:
                    srand(time(0));
                    streghth = rand() % 12 + 8 ;
                    enemy_attack[0] = rand() % 5 + 1 ;

                    if (enemy_health[0] >= 1 and health >=1 ){
                        enemy_health[0] = enemy_health[0] - streghth;
                        health = health - enemy_attack[0];
                        cout << "\nyour health is " << health << "\n";
                        cout << "\nthe enemys health is " << enemy_health[0] << "\n";
                        attack <= 0;
                        cin >> attack;

                    }
                    else if (enemy_health[0] <1){
                        cout << "YOU WIN \n";
                        gold = gold + 5;
                        cout << "Gold:" << gold;
                        return 0;

                    }
                    else if (health <1){
                        cout << "YOU LOST";
                        return 0;
                    }
                    break;

                case 2:
                    cout << " got away safly!";
                    return 0;
                    break;

                default:
                    while( !(cin >> attack) ){
                        cout << "You need to enter a number\n";
                        cin.clear();
                        cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        cin >> attack;



                    }

            }

        }
    }
}


it works well enogught for what i need it to do, thank you :)
Topic archived. No new replies allowed.