A simple question

This is a discussion on A simple question within the C++ Programming forums, part of the General Programming Boards category; Now that I got Dev-C++ working I started looking at the tutorials on this site. After a while I wanted ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    10

    A simple question

    Now that I got Dev-C++ working I started looking at the tutorials on this site. After a while I wanted to see if I could put together a tiny program without making any mistakes...I failed.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char choice;
        
        cout<<"Would you like to start the game? (Y/N) ";
        cin>> choice;
        cin.ignore();
        if ( choice == Y ) {
             cout<<"You are in a large cave. There is an exit to the north and the west.\nWhich path do you take? (S/W) ";
             cin>> letter;
             cin.ignore();
             if ( Choice == S ) {
                  cout<<"You fall into a large pit full of snakes. GAME OVER!\n";
                  }
             if ( choice == W ) {
                  cout<<"You escape the cave! Congratulations!\n";
                  }
        if ( choice == N ) {
             cout<<"Thank you for playing.\n";
             }
        cin.get();
    }}
    It's crappy I know but for some reason it wont let me use characters as a variable. I've changed it to int choice and that worked I just don't know why it won't let me use char. It's probably an obvious mistake I've made but if anyone can help me...

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    choice == 'Y'
    You need single quotes around your character constant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Oh right. Thanks ^_^

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Darth_Paul View Post
    I wanted to see if I could put together a tiny program without making any mistakes...I failed.
    I'm always suspicious of programs that compile first time :-P
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The variables Y, S, W and N (yes, they are just variablenames if you don't use quotes) are all undeclared, so it wouldn't compile...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Your cin.ignore() could be placed under your cin.get().

    Also as int main() needs to return a value, place return 0 before the closing brace
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21