Thread: how can i make this work?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    13

    how can i make this work?

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string option;
        
        cout << "What Would You Like To Do?" << endl;
        cout << "To Withdraw Please Type withdraw" << endl;
        cout << "To Deposit Please Type  deposit" << endl;
        cin >> option;
        
        switch (option)
        {
               case 'withdraw':
                    cout << "hi" << endl;
                    break;
                    
               case 'deposit':
                    cout << "bye" << endl;
                    break;
               
               default:
                    cout << "NO" << endl;
                    break;
                    }
                    
                    
                    
         system("PAUSE");
         return 0;
         }
    I want to make a little bank program but i keep getting an error....whats the problem?
    thanks

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Read your compiler's output - it should tell you that you can't use strings in switch statements - you have to use a simpler type. My suggestion would be to use a character or an integer, e.g. for withdrawals, enter 1, and in the switch statement have a case which handles that value. Your variable called option should be changed to an integer to do this.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You can't use strings in a switch and string literals have " around them, not '.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 09:45 AM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. cant make it work
    By papous27 in forum Linux Programming
    Replies: 0
    Last Post: 03-05-2002, 09:49 PM
  5. how to make files
    By quiksilver9531 in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2002, 06:44 PM