Strange Problem with Inputting

I made this program that takes a letter from the user, then outputs the guitar chord that corresponds with that letter. It works fine except for one thing. If I were to type a wrong character in, the program outputs the error message along with the ability to retry (as expected) but if you type 'w' (or any other incorrect character) and then type 'a' it will say 'a' is non correct. If you do the same thing it with another character it will work. Here's what I mean:
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
Please enter a chord letter...
> w 
I don't think that's a chord...
Try again
> a
I don't think that's a chord... //although it is a chord...
Try again
> b

The chord you entered was the B chord //so b works?

Is this correct? <y> or <n>
> n
Please enter a chord letter...
> a

The chord you entered was the A chord //so does a work or not?

Is this correct? <y> or <n>
> n
Please enter a chord letter...
> w
I don't think that's a chord...
Try again
> b

The chord you entered was the B chord //b works after an error message

Is this correct? <y> or <n>
>


Here is the source:

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
#include <iostream>

int main()
{       
    using namespace std;
    
    bool mainrepeat = true;  
      
    while (mainrepeat == true){ //<--- there is a bracket
           
    char Chords[2][7] = {{'a', 'b', 'c', 'd', 'e', 'f', 'g'}, {'A', 'B', 'C', 'D', 'E', 'F', 'G'}};
    char chord, repeatc;
    int i, j;
    bool repeatb = true;
    
    while (repeatb == true){  //<--- there is a bracket
    
    cout << "Please enter a chord letter...\n> ";
    cin >> chord;
    
    for (i = 0; i <= 7; i++)
    {
        if (chord == Chords[0][i] || chord == Chords[1][i])
        {
                  break;
        }
        if (i == 7)
        {
                  cout << "I don't think that's a chord...\nTry again\n> ";
                  cin >> chord;
                  i = 0;
                  continue;
        } 
    }
    
    cout << "\nThe chord you entered was the " << Chords[1][i] << " chord\n"; 
    cout << "\nIs this correct? <y> or <n>\n> ";
    cin >> repeatc;
    
    if (repeatc == 'n')
    repeatb = true;
    
    else if (repeatc == 'y')
    repeatb = false;
} //<--- chord confirmation while loop ending bracket       
    
    cout << endl;
    
    switch (i)
    {
          case 0: //A
               cout << "e-----0-----\n";
               cout << "B-----2-----\n";
               cout << "G-----2-----\n";
               cout << "D-----2-----\n";
               cout << "A-----0-----\n";
               cout << "E-----------\n"; 
               break;
               
          case 1: //B 
               cout << "e-----------\n";
               cout << "B-----4-----\n";
               cout << "G-----4-----\n";
               cout << "D-----4-----\n";
               cout << "A-----2-----\n";
               cout << "E-----------\n"; 
               break;
               
          case 2: //C 
               cout << "e-----0-----\n";
               cout << "B-----1-----\n";
               cout << "G-----0-----\n";
               cout << "D-----2-----\n";
               cout << "A-----3-----\n";
               cout << "E-----------\n"; 
               break;  
               
          case 3: //D 
               cout << "e-----2-----\n";
               cout << "B-----3-----\n";
               cout << "G-----2-----\n";
               cout << "D-----0-----\n";
               cout << "A-----------\n";
               cout << "E-----------\n"; 
               break;
               
          case 4: //E 
               cout << "e-----0-----\n";
               cout << "B-----0-----\n";
               cout << "G-----1-----\n";
               cout << "D-----2-----\n";
               cout << "A-----2-----\n";
               cout << "E-----0-----\n"; 
               break;     
             
          case 5: //F 
               cout << "e-----1-----\n";
               cout << "B-----1-----\n";
               cout << "G-----2-----\n";
               cout << "D-----3-----\n";
               cout << "A-----------\n";
               cout << "E-----------\n"; 
               break;
               
          case 6: //G 
               cout << "e-----3-----\n";
               cout << "B-----0-----\n";
               cout << "G-----0-----\n";
               cout << "D-----0-----\n";
               cout << "A-----2-----\n";
               cout << "E-----3-----\n"; 
               break; 
    }  
    
    cout << "\nAre you finished? <y> or <n>\n> ";
    cin >> repeatc;
    
    if (repeatc == 'n')
    mainrepeat = true;
    
    else if (repeatc == 'y')
    mainrepeat = false;
} //<--- main while loop ending bracket    
                                     
    cout << "\nPress ENTER to exit\n";
    cin.get();
    cin.get();
    return 0;
}


Any help would be appreciated. Thanks in advance.
Last edited on
I have a suspicion of what might be going on...

1
2
3
4
5
6
7
if (i == 7)
{
     cout << "I don't think that's a chord...\nTry again\n> ";
     cin >> chord;
     i = 0; //<- do this i = -1;
     continue;
}

Tell me if it worked.
Last edited on
It did work! Thanks.

Why did it work though?
You see, when you use the keyword continue i is increased by one (but I think it would also increase even if you omitted the keyword). So, when you start over the loop it starts with i==1 thus it never checks if the character is 'a' or 'A'. If you make the assignment like i=-1; the loop will start over with i==0, which is what you want. :P
Last edited on
Thanks a lot. I've been able to expand it, now it handles major, minor, and 7th chords. It also has playing suggestions. Thanks again for the help :D
Topic archived. No new replies allowed.