0

so my friend and I are trying to make a text-based video game and I have been doing some research on how to get the programming down. Here is our c++ program so far:

#include <iostream>
#include <stdio.h>

char Choice;

using namespace std;

int main()
{
    printf("You wake up to darkness.\n");
    printf("Confused and tired, you walk to the nearest house.\n");
    printf("You notice it's abandoned. What do you do?\n");
    printf("1. Walk Away.\n");
    printf("2. Jump.\n");
    printf("3. Open Door.\n");
    printf("Choose wisely.\n");
    cin >> Choice;

    if(Choice=1)
    {
        printf("The House seems to have a gravital pull on you. How strange.\n");
    }

    else if(Choice=2)
    {
        printf("Having Fun?\n");
    }

    return 0;
}

But when I build and run it will display everything but all answers will be the if(Choice=1) answer. Is there something missing from my program that is needed or do parts contradict each other?

  • 1
    char: '1' not equal to 1 – Mitch Wheat Nov 23 '13 at 04:05
  • Style note: You probably do not want to mix `printf` with `cin`/`cout`. It'll mostly work, but they can interact in strange ways, largely because they do not coordinate with each other. It's not the issue you're having currently. Rather, if you're learning C++, it's best to get off on the right foot. Good luck, and happy programming! – Joe Z Nov 23 '13 at 04:10

1 Answers1

7

You want the comparison operator ==, not the assignment operator =. These are distinct operators. Using = will change the value of Choice, which is not what you want. (Your compiler should warn you about the use of = in an if statement.)

1 is the integer 1. You want to check against the character '1' (ASCII value 49), which is different. Use '1' instead of 1.

if (Choice == '1')
{
    printf("The House seems to have a gravital pull on you. How strange.\n");
}
else if (Choice == '2')
{
    printf("Having Fun?\n");
}

Also, you're mixing two types of I/O. Using cin for input is good. You should use its counterpart cout for output, rather than printf.

cout << "You wake up to darkness." << endl;
cout << "Confused and tired, you walk to the nearest house." << endl;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578