Hello, I am currently going through the tutorials on cplusplus.com. I am all the way at the bottom of the page about Enumerations. I am using Visual C++ 2005 Express Edition for a compiler on a windows XP Professional Machine. I have the following code and my question is below it: [SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<iostream> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<string> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<ctime> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]<sstream> [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][SIZE=2] std; [/SIZE] int main () { typedef string str; str mystring; [SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]/* Enumerations */ [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]enum[/COLOR][/SIZE][SIZE=2] colors_t {black=1, blue, green, cyan, red, purple, yellow, white}; [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] test; colors_t mycolor; cout<<[/SIZE][SIZE=2][COLOR=#800000]"Please enter a number between 1-8\n"[/COLOR][/SIZE][SIZE=2]; getline(cin,mystring); stringstream(mystring)>>test; cout<<mycolor<<endl; string colors_a[black,blue]; [/SIZE][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2](test;test<8;test++) { cout<<black<<endl; cout<<blue<<endl; cout<<green<<endl; } [/SIZE][/COLOR][/SIZE] system("pause"); return 0; } Code (markup): I would like to do 2 things. 1. I would like to be able to print out the word black, blue, green instead of the constant number. 2. I would like to be able to ask someone to input a name of a color instead of a number and have it associate it with the constant number. I am not sure how to do that with enumerations and have not been able to find it on Google. Is this possible? This is not for anything other than learning. Thanks for any help or tips.
You could add a conditional statement to translate: if (mycolor == BLACK) cout << "BLACK" << endl; As for reading in a color instead of a number, you could just read the string. Prepare for pseudocode: if (foo == BLACK OR foo == 1) Make sense?
for part 2, you just have to do the comparisons with "Black", ex: if (mystring == "BLACK") mycolor = BLACK;
I must have done something incorrectly because I received the following error: : cannot convert from 'const char [6]' to 'main::colors_t I will work on it for a little bit and see if I cannot correct the error. Thanks for the advice.
Sorry dude, you can't compare string class object with colors_t object Anyway, I don't know why you're usin' that class...
[addon] Sorror, I am just learning. I have no idea what can be used with what yet. That is probably why.. lol [/addon] I am still working on this. I rewrote the code. This is a little off the enumerations part of it however I cannot figure out what I am doing wrong with the following code. I want the person to enter a color by name or number and it will repeat the name to them. I will get into the enumerations after that. I have this working if the user only enters the color name but not the number. I am not sure if this is the proper way to go about this but I was checking the string length of what the user entered and if the string length is under 3 characters then I would tell them they did not enter a color from the list. Thanks for any suggestions. #include <iostream> #include <string> #include <ctime> #include <sstream> using namespace std; typedef string str; int main () { str mystr; str mystring; str slen; enum colors_t {black=1, blue, green, cyan, red, purple, yellow, white}; colors_t mycolor; string colors[9]; colors[0] = ""; colors[1] = "black"; colors[2] = "blue"; colors[3] = "green"; colors[4] = "cyan"; colors[5] = "red"; colors[6] = "purple"; colors[7] = "yellow"; colors[8] = "white"; for (int x=1;x<9;x++) { if (x==1) { cout<<"Please choose your favorite color:"; } cout<<endl<<x<<") "<<colors[x]; //cout<<endl<<"x = "<<x; //This is here for debugging purposes only if(x==8) { cout<<endl; getline(cin,mystring); stringstream(mystring)>>mystr; if(mystr == "") { cout<<"You have not entered a color.\n"; x=0; } slen=strlen(mystr); [COLOR="Red"][B]else if(slen<3)[/B][/COLOR] { mystr = colors[mystr]; } } } cout<<mystr<<" is a very good color."<<endl; cout<<mycolor<<endl; /* string colors_a[black,blue]; */ for(int test;test<1;test++) { cout<<mycolor; cout<<black<<endl; cout<<blue<<endl; cout<<green<<endl; } return 0; } Code (markup): The error I receive is: error C2664: 'strlen' : cannot convert parameter 1 from 'str' to 'const char *' However, I am not sure what that means. I have Googled it to no avail.
Here ya go: #include "stdafx.h" #include<iostream> #include<string> #include<ctime> #include<sstream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char mystring [20]; enum colors_t {black=1, blue, green}; cout<<"Please enter a number between 1-3\n"; cin >> mystring; //Test for length. A number should be length of 1 while a word should be > 1 if (strlen(mystring)==1) { //since we assume it's a number, we convert the string to a number with atoi int test = atoi(mystring); //once we have a number, we just test it against your enum above (you need to fill in the rest of the conditions) if (test == black) cout<<"Black"<<endl; } else { //we get here if the length test failed meaning it's a color //here we compare the string to the color. this function returns a 0 on a match. just add the rest of the colors if (strcmp (mystring,"black") == 0) cout<<"Black"<<endl; } system("pause"); return 0; } Code (markup): Hopefully this makes sense.
Everything makes sense but I have a few questions: 1) What does #include "stdafx.h" allow? 2) What does int _tmain(int argc, _TCHAR* argv[]) do? Is this the same thing as int main ()? I received the following error: fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory Where would I get that file to put into the VC++ library? Thanks,
I'm using Visual C++ and it put that there in the default program. I'm not exactly sure what it does. You probably don't need it. Also, int _tmain is just the Visual C++ way of writing "main". You can change that as well. The rest of it should work fine.
It works great when I removed those items. Thank You. I can break it down from here. I appreciate a working program... Thanks for helping me learn.
You're welcome. Glad it worked out for you. C++ has a lot of tools for a job, it's just learning what tools are available and how to speak the language. It was nice to get my head back into it today.
This allows you to pass command line arguments to your program - argc is the number of arguments passed, argv[] is an array of char containing the arguments passed; argv[0] is the path of the program. Try this: #include <iostream> using namespace std; int main (int argc, char* argv[]) { int x=0; while (x < argc) cout << x << " : " << argv[x++] << "\n"; return 0; } Code (markup): Compile the program and run from the command line with arguments separated by spaces, eg: >myprog testing hello foo bar 0 : /Users/sfryett/Xcode/myprog/build/myprog 1 : testing 2 : hello 3 : foo 4 : bar Your compiler may have an option to specify command line arguments to save you having to switch to a terminal/console. Regards, Stu