Question:
What is wrong with this simple C++ program?
2013-11-08 00:08:18 UTC
I have recently started learning C++ on www.learncpp.com and I'm currently at switches. I tried writing this simple program that also included enumerated vaues.

The program:

#include

enum Color
{
COLOR_BLACK,
COLOR_WHITE,
COLOR_RED,
COLOR_BLUE,
COLOR_GREEN,
};

void PrintColor(Color eColor)
{
using namespace std;
switch(eColor)
{
case COLOR_BLACK:
cout << "Black" << endl;
break;
case COLOR_WHITE:
cout << "White" << endl;
break;
case COLOR_RED:
cout << "Red" << endl;
break;
case COLOR_BLUE:
cout << "Blue" << endl;
break;
case COLOR_GREEN:
cout << "Green" << endl;
break;
}
}

int main()
{
using namespace std;
cout << "Choose a color!" << endl << endl;
cout << "Black, White, Red, Blue or Green" << endl << endl;
cin >> eColor;
PrintColor(eColor);
return 0;
}

Unfortunately it's giving me the error: eColor was not declared in this scope (error in line 40: 'cin >> eColor'). If anyone knows how to solve this problem, please help me, and IF POSSIBLE, could you maybe also give me a link to a tutorial where I can start learning programming to create windows and simple games etc. I want to start looking into this as I set goals for myself and really want to look at it. Thank you for the help :D.
Six answers:
cja
2013-11-08 04:21:44 UTC
One option would be to take user input as a string, and have a map to associate the string with the enum. Or, you could have the user enter an integer corresponding to the Color enum values. You can overload the istream extraction operator to allow input to a variable of type Color. Like this:



#include



using namespace std;



enum Color {

    COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_BLUE, COLOR_GREEN

};



istream &operator>>(istream &is, Color &c) {

    int color;



    is >> color;

    c = static_cast(color);

    return is;

}



void PrintColor(Color eColor) {

    switch(eColor) {

    case COLOR_BLACK:

        cout << "Black" << endl;

        break;

    case COLOR_WHITE:

        cout << "White" << endl;

        break;

    case COLOR_RED:

        cout << "Red" << endl;

        break;

    case COLOR_BLUE:

        cout << "Blue" << endl;

        break;

    case COLOR_GREEN:

        cout << "Green" << endl;

        break;

    default:

        cout << "?" << endl;

    }

}



int main() {

    Color eColor;



    cout << "Choose a color!" << endl;

    cout << "Black(" << COLOR_BLACK << "), White(" << COLOR_WHITE

              << "), Red(" << COLOR_RED << "), Blue(" << COLOR_BLUE

              << ") or Green(" << COLOR_GREEN << "): ";

    cin >> eColor;

    PrintColor(eColor);

    return 0;

}



#if 0



Sample run:



Choose a color!

Black(0), White(1), Red(2), Blue(3) or Green(4): 3

Blue



#endif
hey
2013-11-08 11:09:34 UTC
if you want the user to input words when making color choices then eColor needs to be a char or string type. the case values needs to be changed as well. there are easier and more efficient ways to execute this besides using a switch but if using a switch is a requirement you can try this



#include



using namespace std;



enum Color

{

COLOR_BLACK,

COLOR_WHITE,

COLOR_RED,

COLOR_BLUE,

COLOR_GREEN,

};



void PrintColor(string eColor)

{



eColor[0]=tolower(eColor[0]); //set to change to lowercase in case user typed all caps



switch(eColor[0])

{

case 'n':

cout << "Noir" << endl;

break;

case 'w':

cout << "White" << endl;

break;

case 'r':

cout << "Red" << endl;

break;

case 'b':

cout << "Blue" << endl;

break;

case 'g':

cout << "Green" << endl;

break;

default:

cout << "Invalid input"<
}

}



int main()

{



string eColor;



cout << "Choose a color!" << endl << endl;

cout << "Noir, White, Red, Blue or Green" << endl << endl;

cin >> eColor;

PrintColor(eColor);

return 0;

}
2013-11-08 00:10:02 UTC
you must declare eColor, use

int main()

{

using namespace std;

cout << "Choose a color!" << endl << endl;

cout << "Black, White, Red, Blue or Green" << endl << endl;

Color eColor;

cin >> eColor;

PrintColor(eColor);

return 0;

}
tumbleweed_biff
2013-11-08 00:41:53 UTC
You already have your answer, but I will add a comment: it is a wise thing to always provide a default at the bottom of a switch case. What happens if the none of the cases you list match to the value ... ?



Also, I don't believe your current items will work ... you are entering "Black, White, Red, Blue or Green" but the switch case is looking for values like "COLOR_RED" ...
Vladimir
2013-11-08 00:29:08 UTC
Well, obviously you haven't declared 'eColor' variable inside 'main()' function.

Insert ' Color eColor; ' (without quotes) inside main() function, right after line 'using namespace std;' ,and it should work. You will need to enter one of the named constants from Color enumeration.



Some good tutorials on C++:

- http://www.tutorialspoint.com/cplusplus/index.htm

- http://www.cplusplus.com/doc/tutorial/

- http://www.learncpp.com/
Ebrahim A
2013-11-08 00:18:50 UTC
exactly. You must define/declare every variable in C++ before use it .


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...