Help please if loop

Hi i am new at c++ and i am working on a game(text based) i have just started and found a problem i have searched and couldn't find a proper answer
so all i want to do is if the answer isn't y,Y,n,N it must re-ask the question
(go back up)
and if anything else is wrong may you please inform me

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    cout << " -------------------------------------------------------------------------------"
         << " |                                                                            |\n"
         << " |\t\t       ZZZZZ  ZZZ  Z     Z ZZZZ  ZZZZZ ZZZZZ                  |\n"
         << " |\t\t          Z  Z   Z ZZ   ZZ Z   Z   Z   Z                      |\n"
         << " |\t\t         Z   Z   Z Z Z Z Z ZZZZ    Z   ZZZ                    |\n"
         << " |\t\t        Z    Z   Z Z  Z  Z Z   Z   Z   Z                      |\n"
         << " |\t\t       ZZZZZ  ZZZ  Z     Z ZZZZ  ZZZZZ ZZZZZ                  |\n"
         << " |                                                                            |\n"
         << " |         Z     ZZZZ   ZZZ   ZZZZ     Z     Z     Z   Z ZZZZ  ZZZZZ ZZZZZ    |\n"
         << " |        Z Z    Z   Z Z   Z Z        Z Z    Z      Z Z  Z   Z Z     Z        |\n"
         << " |       ZZZZZ   ZZZZ  Z   Z Z       ZZZZZ   Z       Z   ZZZZ  ZZZZZ ZZZ      |\n"
         << " |      Z     Z  Z     Z   Z Z      Z     Z  Z       Z   Z         Z Z        |\n"
         << " |     Z       Z Z      ZZZ   ZZZZ Z       Z ZZZZZ   Z   Z     ZZZZZ ZZZZZ    |\n"
         << " |                                                                            |\n"
         << " -------------------------------------------------------------------------------\n"
         << "\t     ****************************************************\n"
         << "\t           Created and Developed by Tristan Thompson\n"
         << "\t     ****************************************************\n\n";
         
             
    /***********************************     
                 ENEMY STATS
    ***********************************/
    //Z1 (zombie easy)
    int z1attack = 5 ;
    int z1healthbegin = 10 ;
    int z1health = 10;
    int z1bowdefense = 1 ;
    int z1defense = 1 ;
    int z1magdefense = 1;
        
    //N1 (necromancer easy)
    int n1magattack = 6;
    int n1health = 25; 
    int n1defense = 3;
    int n1bowdefense = 3;
    int n1magdefense = 4;

    /***********************************
                PLAYER STATS
    ***********************************/ 
    int pattack = 6;
    int pbowattack = 7;
    int phealth = 100;
    int pdefense = 3;
    int pbowdefense = 2;
    int pmagattack = 4;
    int pmagdefense = 3;
    // INVENTORY
    int arrows = 0;
    
    // HELPING HANDS
    int fmnumber = 0;
    int anumber = 0;
    
    
    /***********************************
              FOOTMEN STATS 
    ***********************************/
    int fmattack = 3;
    int fmhealth = 5;
    int fmdefense = 2;
    int fmbowdeffense = 0;
    int fmmagdefense = 1;
    
    /***********************************
             ARCHER STATS
    ***********************************/
    int abowattack = 3;
    int ahealth = 6;
    int adefense = 1;
    int abowdefense = 2;       
    int amagdefense = 2;
    
    /**********************************
                  NAMES
    **********************************/
    
    system("PAUSE");
    system("CLS");
    cout << "\t\t\t  -----------------------------\n"
         << "\t\t\t     Chapter 1: THE RISING!!\n"
         << "\t\t\t  -----------------------------\n";
    cout << "Welcome what is your name:";
    char name[20];
    cin >> name;
    cout << name << endl;
    cout << "Thats a nice name can you fight at all?\n";
    cout << "Y/N:";
    char a1;
    cin >> a1;
    cout << endl;
    if( a1 == 'Y' )
    {
           cout << "Good we need a leader for a pack of footmen going to the graveyard\n" 
                << " for inspection, We have heard strange noises from there\n";
           fmnumber = fmnumber + 5;
           arrows = arrows + 10;
    } 
    else if ( a1 == 'y' )
    {     
          cout << "Good we need a leader for a pack of footmen going to the graveyard\n" 
                << " for inspection, We have heard strange noises from there\n";
           fmnumber = fmnumber + 5;
           arrows = arrows +10;
}
    else if ( a1 == 'n' )
    {
         cout << "well thats to bad we need you to lead our footmen to the graveyard\n"
              << " for inspection but since you can't fight we will send 5 extra footmen\n"
              << " to help you";
              fmnumber = fmnumber + 10;
              arrows = arrows +10;
    }
    else if ( a1 == 'N' )
    { 
         cout << "well thats to bad we need you to lead our footmen to the graveyard\n"
              << " for inspection but since you can't fight we will send 5 extra footmen\n"
              << " to help you";
              fmnumber = fmnumber + 10;
              arrows = arrows +10;
    }
    else 
    {
         cout << "You have selected a unrecognized answer\n\n";
//here is where i want it to go back up and re-ask the question
         }
    system("PAUSE");
    return EXIT_SUCCESS;
}
First, a few things:

1) Try using functions
This means that you can reuse your code, say for battles, and makes the whole program easier to read, and takes less code.

2) Use classes for your objects
This is especially useful, as it allows you to easily add in multiple footmen and enemies and things, without having to hard code variables for each one (of course, don't bother if you haven't learnt about classes yet, though I highly recommend that you do).

3) Try to avoid duplication
Basically, this is to allow you to do everything faster, and allows you to read through your code faster, due to less being repeated. The normal way to do this is to use functions, though in your case, the main problem that stands out is using a seperate "if" branch for each option, rather than grouping them together where applicable, i.e. "y" and "Y" sharing a branch, "n" and "N" sharing a branch.

--------------------

The way that I would recommend to do for your question would be to simply use a loop, and break from the loop when a valid answer comes. Here would be how I would do this:

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    //...
    cout << "Thats a nice name, can you fight at all? (Y/N):" << endl;
    while (true) {
        char response;
        cin >> response;
        if (response == 'y' || response == 'Y') {
            // Respond to option...
            break;    // break from the loop
        } else if (response == 'n' || response == 'N') {
            // Respond to option...
            break;
        } else {
            cout << "You have selected an unrecognized answer. \n\n"; 
            cout << "Can you fight at all? (Y/N): " << endl;
        }
    }
    // continue on... 
Last edited on
Thanks that helps alot and no i dont really know how to use classes
Topic archived. No new replies allowed.