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);
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.
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);