CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2015
    Posts
    21

    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;
    		}
    }

  2. #2
    Join Date
    Jan 2015
    Posts
    16

    Re: My "if" statements will not compile correctly

    Where do you assign values to both guessW and cars?

  3. #3
    Join Date
    May 2015
    Posts
    21

    Re: My "if" statements will not compile correctly

    Quote Originally Posted by jlb1 View Post
    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.

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: My "if" statements will not compile correctly

    Quote Originally Posted by deadfolx View Post
    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.

  5. #5
    Join Date
    May 2015
    Posts
    21

    Re: My "if" statements will not compile correctly

    Quote Originally Posted by Alterah View Post
    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...

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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
  •  





Click Here to Expand Forum to Full Width

Featured