Help editing my C++ Program
Shadow Kurimeki May 17, 2008 at 11:05pm UTC
Hey I posted this once already but twas gone the next day My problem is that when ever I enter the correct answer for my Cin statement it will still go to the next cout statement and wont cut the the next question1 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
#include<iostream>
#include<string>
using namespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
cout<<"Hello this is your standard True and False quiz" <<endl;
cout<<"Please enter 'T' or 'F'" <<endl;
cout<<"No#1 George Washington invented the toilet?" <<endl;
cin>>char1;
if ( char1 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char1;
};
if ( char1 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char1;
};
if ( char1 == "T" )
{
cout<<"You entered the incorrect answer. The answer is False" <<endl;
};
cout<<"No#2 The Squareroot of 3136 is 56?" <<endl;
cin>>char2;
if ( char2 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char2;
};
if ( char2 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char2;
};
if ( char2 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True" <<endl;
};
cout<<"No#3
system(" PAUSE");
return 0;
} "
I want it to go and skip the you entered the incorrect character
Shadow Kurimeki May 17, 2008 at 11:05pm UTC
Here is the finished program but with the same problem with the incorrect character issue1 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
#include<iostream>
#include<string>
using namespace std;
int main()
{
string char1, char2, char3, char4, char5, char6;
cout<<"Hello this is your standard True and False quiz" <<endl;
cout<<"Please enter 'T' or 'F'" <<endl;
cout<<"No#1 George Washington invented the toilet?" <<endl;
cin>>char1;
if ( char1 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char1;
};
if ( char1 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char1;
};
if ( char1 == "T" )
{
cout<<"You entered the incorrect answer. The answer is False" <<endl;
};
cout<<"No#2 The Squareroot of 3136 is 56?" <<endl;
cin>>char2;
if ( char2 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char2;
};
if ( char2 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True" <<endl;
};
cout<<"No#3 Do you capitalize the seasons?" <<endl;
cin>>char3;
if ( char3 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char3;
};
if ( char3 == "T" )
{
cout<<"You entered the incorrect answer. The answer is False" <<endl;
};
cout<<"No#4 Is a drum in the percussion area?" <<endl;
cin>>char4;
if ( char4 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char4;
};
if ( char4 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True" <<endl;
};
cout<<"No#5 The Egg came before the Chicken?" <<endl;
cin>>char5;
if ( char5 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char5;
};
if ( char5 == "F" || "T" )
{
cout<<"Ha! trick question the answer depends on if you a " <<endl;
cout<<"Creationist or an Evolutionist " <<endl;
};
cout<<"No#6 Computers help everyday life?" <<endl;
cin>>char6;
if ( char6 != "T" || "F" )
{
cout<<"You entered an incorrect character please reenter True of False" <<endl;
cin>>char6;
};
if ( char6 == "F" )
{
cout<<"You entered the incorrect answer. The answer is True" <<endl;
};
cout<<"Your answers where" <<endl;
cout<<"" <<endl;
cout<<char1<<char2<<char3<<char4<<char6<<endl;
cout<<"" <<endl;
if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 0 correct and 5 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 1 correct and 4 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 1 correct and 4 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "T" )
{
cout<<"You got 2 correct and 3 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "T" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "F" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "T" && char4 == "T" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "T" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 3 correct and 2 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "F" )
{
cout<<"You got 4 correct and 1 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "F" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "T" && char4 == "T" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect" <<endl;
};
if (char1 == "F" && char2 == "F" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect" <<endl;
};
if (char1 == "T" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 4 correct and 1 incorrect" <<endl;
};
if (char1 == "F" && char2 == "T" && char3 == "F" && char4 == "T" && char6 == "T" )
{
cout<<"You got 5 correct and 0 incorrect" <<endl;
};
system("PAUSE" );
return 0;
}
Long isnt it
Zhuge May 17, 2008 at 11:05pm UTC
The first few if statements, where you use the logical or (||) need to have the variable specified for each statement. The compiler is reading:
if ( char1 != "T" || "F" ) And getting if(char1 != "T") and if("T"). Since if("T") is always true, it always sayings the character is incorrect. You want:
if (char1 != "T" && char != "F" ) You want a logical and because no matter what character you put in, it cannot be both T and F. EDIT: Also you check every combination of true and false answers. This seems very redundant; why not just increment a variable for each correct answer?
Last edited on May 17, 2008 at 11:05pm UTC
Zaita May 17, 2008 at 11:05pm UTC
Ahhh my eyes =\ This code needs to be tidied up a lot. Some comments: 1) use functions. You have A LOT of duplicated code. 2) store your functions in an array, or vector so you can iterate through them. 3) use a parseResponse() or similar function. 4) Dynamically build your response string. 5) If I entered an incorrect response, I am asked to re-enter only once. There is no loop to ensure I enter a valid response. e.g.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
#include <vector>
#include <iostream>
using namespace std;
typdef struct Question {
bool bIsTrue;
string sQuestion;
}
int main() {
vector<Question> vQuestionList;
Question q1;
q1.sQuestion = "2 + 2 = 4" ;
q1.bIsTrue = true ;
Question q2;
q2.sQuestion = "2 + 2 = 5" ;
q2.bIsTrue = false ;
vQuestionList.push_back(q1);
vQuestionList.push_back(q2);
// Now loop through and ask the questions
vector<Question>::iterator vPtr = vQuestionList.begin();
while (vPtr != vQuestionList.end()) {
cout << (*vPtr).sQuestion << endl;
string sAnswer = "" ;
cin >> sAnswer;
while ( (sAnswer != "T" ) && (sAnswer != "F" ) )
cout << "Invalid Answer, Please Use 'T' or 'F'" << endl;
cin >> sAnswer;
}
parseResponse( (*vPtr).bIsTrue, sAnswer);
vPtr++;
}
}
The parseResponse() function should return True if the answer was right. False if it was incorrect. This should save you ALOT of code, and make finding bugs a lot quicker.
DiptenduDas May 17, 2008 at 11:05pm UTC
Same Problem with two different heading posted by same person!!!!!! 1. Quiz Help 2. Help editing my C++ Program
zachlr May 17, 2008 at 11:05pm UTC
By the way, your true/false questions are a bunch of BOOL! Ha ha get it it's a pun! I crack myself up.
Registered users can reply in this forum.