if statements and initializing problems

Hello, I'm new to c++ which is why I'm here, probably with a very dumb/simple question. I'm working on a large program but encountered problems in the early stages. Specifically I need my program to accept the char 'cars' as a correct answer and carry out the respected if statement. I've been told to initialize it, but I'm unsure on how to do that...I'm not looking for someone to write it for me, but please just tell me where I'm going wrong here. Source code below...

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
#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()
{
	char guessW;
	char cars = 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;
		}
}
Last edited on
We need the variables guessW and cars to hold sequences of characters
A variable of type char can hold only one character, use std::string to hold sequences of characters.
https://www.mochima.com/tutorials/strings.html

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
#include <iostream>
#include <string>
// #include <iomanip>
// using namespace std;

//prototype
void instruction();
void guessArray();

int main()
{
    instruction();

    // system("pause");
    std::cout << "Press the enter key to continue . . ." ;
    std::cin.get() ;

    guessArray();
    // return 0;
}

void instruction()
{
    std::cout << "\n*************************************************************\n";
    std::cout << "Welcome to the Guessing Game! It's like hangman, but no one dies! \n";
    std::cout << "A word with missing letters will appear.  It's your job to find \n";
    std::cout << "the word.  You will be allowed a maximum of 8 guesses for each \n";
    std::cout << "word.  If you guess correctly you win, if not you lose. GOOD LUCK! \n";
    std::cout << "*************************************************************\n\n" ;
}

void guessArray()
{
    // char guessW;
    // char cars = cars;
    const std::string cars = "cars" ;
    std::string guessW ;

    // char guessArray[5][4] = { { 'c', '*', '*', 's' } };
    const std::string guessArray[5] = { "c**s" } ;
    std::cout << "\nword: " << guessArray[0] << '\n' ;

    const int MAX_ATTEMPTS = 8 ;

    for( int i = 0 ; i < MAX_ATTEMPTS ; ++i  )
    {
        std::cout << "\nYou have " << MAX_ATTEMPTS - i << " attempts left to guess what the word is\n" ;
        std::cout << "please type in the whole word in lower case: " ;
        std::cin >> guessW;

        if (guessW == cars)
        {
            std::cout << "Correct! You Win!\n";
            return ;
        }
        else
        {
            std::cout << "Incorrect\n";
        }
    }

    std::cout << "You failed to guess the word in " << MAX_ATTEMPTS << " attempts\n" ;
}
It must be a const string..of course. I've tried just about everything except the most obvious choice. Thank you very much for the insight, sir. :)
Topic archived. No new replies allowed.