suggestions for functions

Can someone provide suggestions for converting this code to functions, instead of using the switch? The code takes an item number from the user and outputs the color, as long as the item number is 7 characters and the 4th character is either b, g, r, or w.

int main()
{




while(true)
{
string itemNumber = "";
cout << "Enter item number (Q to quit): ";
getline(cin, itemNumber);

if (itemNumber == "q" || itemNumber == "Q") {
cout << " Thank you, goodbye." << endl;
break;
}

if (itemNumber.length() != 7) {
cout << "Invalid item number length." << endl;
continue;
}

char color = tolower(itemNumber.at(3));

string output = "";
switch (color) {
case 'b':
output = "Blue";
break;
case 'g':
output = "Green";
break;
case 'r':
output = "Red";
break;
case 'w':
output = "White";
break;
default:
output = "Invalid color.";
}

cout << output << endl;
}
system("pause");
return 0;
Do you mean to use if-else or what?
Functions and switches serve different purposes. What you can do is put the switch in a const char* getNameByChar(char c);.
I was looking for functions like a void function (where you have to indicate the prototype, then define and call it.
How about:

string GetColor();

void WriteColor(char color);


Or you could just return the char in GetColor. Whatever you feel like doing.
Last edited on
I'm pretty green at this....would the prototype then be

void writeColor(char);

then for the definition you would declare

string getColor();
void writeColor(char){

or how would it be done?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

char GetColor();    // declares it

char GetColor() {

// implement it

}

void WriteColor(char color);  // declares it

void WriteColor(char color) {

// implement it

}



If you do this right you can call it inline like:


1
2
3
4
5
6
7
int main() {

WriteColor(GetColor());

return 0;
}


Pretty slick huh?


Thge definition must match the declaration. Ok, so the variable names don't have to match but the name of the function and the data types have to. So this is valid.

1
2
3
4
5
6
7
8
void WriteColor(char color);  

void WriteColor(char thing) {   

// implement it

}

Last edited on
thank you!
okay, I can't seem to get the hang of how to get this into code so that it works. HELP. I'm trying to use the functions to replace the original code that I submitted at the beginning of this thread.

#include <iostream>
#include <string>

using namespace std;

// INSERT YOUR FUNCTION PROTOTYPES HERE
char getColor();
void writeColor(char color);

int main()
{
// declare variables
string getColor();

// code the algorithm

writeColor(getColor());



}
system("pause");
return 0;
} //end of main function

// INSERT YOUR FUNCTION DEFINTIONS HERE

char GetColor()
{
while(true)
{
string itemNumber = "";
cout << "Enter item number (Q to quit): ";
getline(cin, itemNumber);

if (itemNumber == "q" || itemNumber == "Q") {
cout << " Thank you, goodbye." << endl;
break;
}

if (itemNumber.length() != 7) {
cout << "Invalid item number length." << endl;
continue;
}

void writeColor(char color)
{
char color = tolower(itemNumber.at(3));
cout << "Item you ordered is: " <<
}
Topic archived. No new replies allowed.