Switch Statement Problem
Apr 22, 2014 at 5:15am UTC
So... I know it is a long code and messy but bear with me on this.
The problem is that when I enter the same letter twice, it works, but then it jumps to case 'P' after it checks for the same letter. I don't know why this happens because before i added the check for consonant function, it worked fine.
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>
#include <cstring>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
int test = 0;
string phrase, sentence;
std::vector<char > guesses;
srand (time(NULL));
test = rand() % 4 + 1;
cout << "#" << test << endl;
switch (test) {
case 1:
phrase = "thing" ;
sentence = "computer science" ;
break ;
case 2:
phrase = "Subject" ;
sentence = "math and science" ;
break ;
case 3:
phrase = "Subject" ;
sentence = "pony and unicorn" ;
break ;
case 4:
phrase = "Subject" ;
sentence = "dinosaurs and rhino" ;
break ;
}
cout << "The phrase is..." << phrase << endl;
cout << "Here is your sentence..." << sentence << endl;
int length;
length = sentence.length();
char letter;
int arysize[length];
string rounds = "" ;
string again = "" ;
for (int z = 0; z < length; z++) {
arysize[z] = 0;
}
int count = 0;
while (count != 10) {
cout << "Type S (to spin wheel), P(to guess word), or V(to buy a vowel)" << endl;
char option;
while (cin) {
cin >> option;
if (option == 'S' || option == 'P' || option == 'V' )
break ;
else {
cout << "You have to enter either S, P or V." << endl;
}
}
switch (option) {
case 'S' :
{
int wordsCorrect = 0;
int wordsCount = 0;
cout << "Enter a letter" << endl;
cin >> letter;
bool letterCheck = false ;
guesses.push_back(letter);
int size = guesses.size();
while (letterCheck != true ) {
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u' ) {
letterCheck = false ;
guesses.pop_back();
cout << "You cannot enter a vowel. Please enter a consonant." << endl;
cin >> letter;
} else {
letterCheck = true ;
break ;
}
}
//Checks for duplicate letters
if (size > 1) {
for (int y = 0; y < size-1 ; y++) {
if (letter == guesses[y]) {
letterCheck = false ;
guesses.pop_back();
cout << "You cannot enter the same letter twice." << endl;
break ;
} else
letterCheck = true ;
}
}
if (letterCheck == true )
{
for (int j = 0; j < length; j++) {
if (sentence[j] == letter) {
arysize[j] = 1;
wordsCorrect++;
} else if (sentence[j] == ' ' )
arysize[j] = 1;
}
for (int m = 0; m < length; m++) {
if (arysize[m] == 1) {
cout << sentence[m];
} else
cout << "_" ;
}
cout << "\nThe amount of words correct is... " << wordsCorrect << endl;
for (int a = 0; a < length; a++) {
if (arysize[a] == 1)
wordsCount++;
}
if (wordsCount == length) {
cout << "You win this round!" << endl;
break ;
}
count++;
cout << "\nYou have " << 10 - count << " tries left." << endl;
break ;
}
}
case 'P' : {
string guessPhrase = "" ;
cout << "Enter your guess here." << endl;
cin.ignore();
getline(cin,guessPhrase);
if (sentence == guessPhrase) {
cout << "You win!" << endl;
int temp = (10 - count);
count += temp;
} else {
count++;
cout << "You lose all your money." << endl;
cout << "\nYou have " << 10 - count << " tries left." << endl;
break ;
}
}
}
}
}
Apr 22, 2014 at 10:16am UTC
You are missing a break ;
at the end of your case 'S'
and case 'P'
blocks. The only breaks I see are inside IF statements, but of those IF statements are not true then no break will be run and it will continue and run the case 'P' code.
Apr 22, 2014 at 2:43pm UTC
You are missing a few breaks within your switch, if you correct this your code should run.
Topic archived. No new replies allowed.