Thread: A simple question

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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,688
    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
    Double Helix STL

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, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM