Thread: error c2059 'constant' if else statements

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    error c2059 'constant' if else statements

    Hey guys I am trying to write a program for my computer science and I keep getting an error c2059 on line 6 where I declare char choice1, b,l. I have been trying various things yet I can't get the error to disappear. Also I am using visual studio 2008. Any help would be appreciated, also this is my first post so sorry if I messed anything up.

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    		char choice1,'b','l';
    		
    		cout<< "You are going on a camping trip and decide to stop at a store. You have enough \nmoney for either beef jerky or lighter fluid. So you have to choose one or \nthe other. (b/l)"<<endl;
    		cin>> choice1; 
    			
    			if(choice1== 'b')
    			{ 
    				cout<< "You have decided to buy the beef jerky and continue to the campground."<<endl;
    			}
    			else if(choice1=='l')
    			{
    				cout<< " You have decided to buy the beef jerky and continue to the campground."<<endl;
    			}
    			else
    			{
    				cout<< "Invalid response please try again."<<endl;
    			}
     
    	system("PAUSE");
    		return 0;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    char choice1,'b','l';
    'b' and 'l' are just a characters, not a variable of type char, you do not need, nor can you, declare them, in exactly the same way that you do not need to declare 0 or 1

    char choice;

    is what you need
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    thank you so much. I keep making random errors and I couldn't figure that one out at all. Thanks again

  4. #4
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    alright
    you dont need to declare 'b' or 'l', that is what the char choice1; is for. when you cin>>choice1 you can enter anything you want. by putting the if statment you want the program to do something for exactly what you put in for that if statment. it would also be helpful if you make it so the 'l' looked more like an 'L' instead of a 1. you wrote the program where if you chose l for lighter you get beef jerky too. to get the program to except both upper and lower case letters try this
    Code:
    if(choice1=='b' || choice1=='B')             // the || stands for 'or'
    {
    blah blah blah
    }

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    thanks guys. I appreciate the quick response

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM