-
June 7th, 2015, 07:15 PM
#1
My "if" statements will not compile correctly
This is just the start to a very big project, but I'm stuck. Everything works fine up until I type in the value for "guessW".
No matter what I put in for my answer, the wrong "if" statement will appear even though I have put in two separate ones to distinguish the correct answer. Please help.
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//prototype
void instruction();
void guessArray();
int main()
{
instruction();
system("pause");
guessArray();
return 0;
}
void instruction()
{
cout << "*************************************************************" << endl;
cout << "Welcome to the Guessing Game! It's like hangman, but no one dies! " << endl;
cout << "A word with missing letters will appear. It's your job to find " << endl;
cout << "the word. You will be allowed a maximum of 8 guesses for each " << endl;
cout << "word. If you guess correctly you win, if not you lose. GOOD LUCK! " << endl;
cout << "*************************************************************" << endl;
}
void guessArray()
{
string guessW;
string cars;
char guessArray[5][4] = { { 'c', '*', '*', 's' } };
for (int row = 0; row < 1; row++)
{
for (int column = 0; column < 4; column++)
{
cout << guessArray[row][column];
}
cout << endl;
}
cout << "You have 8 attempts to guess what the word is, you must type in the whole word each time. " << endl;
cin >> guessW;
if (guessW == cars)
{
cout << "Correct!" << endl;
}
else
{
cout << "Incorrect" << endl;
}
}
-
June 7th, 2015, 07:56 PM
#2
Re: My "if" statements will not compile correctly
Where do you assign values to both guessW and cars?
-
June 7th, 2015, 08:13 PM
#3
Re: My "if" statements will not compile correctly
Originally Posted by jlb1
Where do you assign values to both guessW and cars?
they're both assigned in the upper part of the function, as 'string'. I tried to declare them as 'int' and 'char' as well, but nothing seemed to work.
-
June 7th, 2015, 08:26 PM
#4
Re: My "if" statements will not compile correctly
Originally Posted by deadfolx
they're both assigned in the upper part of the function, as 'string'. I tried to declare them as 'int' and 'char' as well, but nothing seemed to work.
You've only gotten input for guessW. Your cars variable has been declared, but not initialized to anything. So, it likely has garbage data which is why regardless of what you enter for guessW, it likely won't match the value of cars, so the else statement would get executed.
Last edited by Alterah; June 7th, 2015 at 08:28 PM.
-
June 7th, 2015, 08:34 PM
#5
Re: My "if" statements will not compile correctly
Originally Posted by Alterah
You've only gotten input for guessW. Your cars variable has been declared, but not initialized to anything. So, it likely has garbage data which is why regardless of what you enter for guessW, it likely won't match the value of cars, so the else statement would get executed.
Okay I see what you're saying, thanks. What would I have to do make the if statement execute properly when the user enters "cars". Basically the word cars, is the correct answer and I want to make the program distinguish the word cars from any other input the user puts in. cats,caps,cabs,etc...
-
June 8th, 2015, 03:03 AM
#6
Re: My "if" statements will not compile correctly
The simplest way of making this work is just to assign "cars" to the variable cars.
Code:
string cars { "cars" };
However, whilst this works in this particular case it is not a general solution that can be applied where multiple words are to be guessed. Also why is guessArray a 2 dimensional char array and not an array of string? eg
Code:
string guessArray[4] {"c**s");
One way would be to use a 2 dimensional array of string where one column is the answer and one is what to display eg
Code:
string guessArray[2][2] {{"cars", "c**rs"}, {"guru", "g**u"}};
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|