Thread: Problem with a string statement

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Problem with a string statement

    Hi, C++ noob here. I'm trying to have it so that my program reads numeric values from variables declared earlier in the code (which were calculated from user input) and outputs those numeric values as text at the end. Basically this secret code decoder thing. However, I'm ripping my hair out trying to get it displayed properly.

    I want to use a "switch" statement that sets my string variable to a certain text depending on the number calculated earlier. I also want it so it outputs it at the very end so the text is displayed with the string values displayed. Here's a modified bit of my code:

    Code:
    	int RendPoint;
    	string RendPointC = "";
    	cin >> RendPoint;
    
                               switch(RendPoint)
    				{
    						case '2':
    							RendPointC = RendPointC + "fountain ";
    							break;
    						case '7':
    							RendPointC = RendPointC + "large tree ";
    							break;
    						case '8':
    							RendPointC = RendPointC + "church ";
    							break;
    						default:
    							cout << "Error!!! ";
    					}
    
    					cout <<  RendPointC << endl;
    When I try entering in 2, 7, or 8 I only get the default statement. Please help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    '2' is a char literal but RendPoint has the type int. The two aren't the same. Change the cases to use ints or change the variable to be a char.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Red face

    wow, such a stupid mistake . Fixed it perfectly, thanks

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    RendPointC = RendPointC + "fountain ";
    is the same as
    Code:
    RendPointC = "fontain ";
    since RendPointC is "".

  5. #5
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Quote Originally Posted by cyberfish View Post
    Code:
    RendPointC = RendPointC + "fountain ";
    is the same as
    Code:
    RendPointC = "fontain ";
    since RendPointC is "".
    I'm sure he meant to say:
    Code:
    RendPointC = RendPointC + "fountain ";
    is the same as
    Code:
    RendPointC += "fontain ";
    Since RendPointC could be changed within some of the missing code...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. String Statement problem
    By snowy101 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 01:44 PM