[C++] Why won't this compile?

Results 1 to 7 of 7
  1. #1
    Infraction Banned 2GetherWeRise is offline
    MemberRank
    Nov 2009 Join Date
    29Posts

    [C++] Why won't this compile?

    So those of you who saw my topic yesterday told me that the variable type "char" can only store one letter, as opposed to a word. So i made a new script using letters instead of words for the variable, yet my compiler still says there is an error. Can someone tell me what the error is?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char choice;
    
    	cout<<"Welcome to the Super Quiz Game!\n";
    	cout<<"I will be asking you a series of\n";
    	cout<<"of multiple choice questions, and\n";
    	cout<<"see how many you can answer! Ready?\n";
    	cin.get();
    	cout<<"lets begin\n";
    	cin.get();
    	cout<<"What two nations fought in the War of 1812?\n";
    	cout<<"A. France and Canada\n";
    	cout<<"B. America and Britian\n";
    	cout<<"C. Mexico and America\n";
    	cin>> choice;
    	cin.ignore();
    
    	if ( choice == b ) {
    		cout<<"You are correct!\n";
    	}
    	cin.get();
    }
    Thank you to all who answer.


  2. #2
    Account Upgraded | Title Enabled! IQstim is offline
    MemberRank
    Jan 2009 Join Date
    632Posts

    Re: [C++] Why won't this compile?

    quote tags my man. qoute tags.

    choice == b
    choice == "b"

    BAM BAM BAM! you're dead.

  3. #3
    Infraction Banned 2GetherWeRise is offline
    MemberRank
    Nov 2009 Join Date
    29Posts

    Re: [C++] Why won't this compile?

    i put quote tags and now the compiler says theres an error in the "==" and it says operand types are incompatible.

  4. #4
    Account Upgraded | Title Enabled! Generic230 is offline
    MemberRank
    Jul 2009 Join Date
    506Posts

    Re: [C++] Why won't this compile?

    Limited knowledge:
    You use '' instead of "" for chars. Speaking without any C++ knowledge, but in Java you use '' for a char

  5. #5
    Infraction Banned 2GetherWeRise is offline
    MemberRank
    Nov 2009 Join Date
    29Posts

    Re: [C++] Why won't this compile?

    Uh, run that by me again? i don't really understand what you just said.

  6. #6
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [C++] Why won't this compile?

    A single character is written like 'b'. If you type "b", it would mean it's a string with just one letter, and behind the curtains the actual data is two bytes, one for the character b and one 0, as C-strings need the 0 for marking where the string ends in memory. If there wasn't such a marker, there would be no way for the program to know how long the actual string is. Again in your listing (choice == b) is interpreted like both choice and b are variable names.

    So, obviously you can't store "b", being 2 bytes, to a char that only holds one.
    Last edited by Negata; 08-03-10 at 01:01 AM.

  7. #7
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++] Why won't this compile?

    Quote Originally Posted by IQstim View Post
    quote tags my man. qoute tags.

    choice == b
    choice == "b"

    BAM BAM BAM! you're dead.
    BAM BAM BAM! you're dead too!

    "b" is a pointer to an array of characters in the stack
    Checking if it's equal to 'b' would be the right thing to do here, the input is also put in a characterm so that's valid.

    Btw, to the OP; did you not get a compiler warning saying the variable b is never declared? Or did you ignore it / did not understand it?
    I also suggest leaving out the 'using' line, and prefix the standard lib with 'std::', but that's me.

    Also, you get a warning for not returning an integer. End main with 'return 0;'.



Advertisement