Trying to make a small program that can print out an invoice. It requires 4 - 6 different functions. The one I'm having the toughest time with however is trying to tie my string values to the integer and invoking them.
- Bread
- Milk
- Eggs
- Deodorant
- Cheese
What I'm trying to do is tie my integer value to the value in the string.
NOTE: I would like to do this Without using switch values, just with either the for or while loops. Want more practice with these in particular.
My first attempt was to try something like this:
int getIntValue(string toInput){
cout << "Please enter " << toInput << ": ";
int value;
cin >> value;
return value;
}
string choiceToItem(int choice){
return choice == 1 ? "Bread" :
choice == 2 ? "Milk" :
choice == 3 ? "Eggs" :
choice == 4 ? "Deoderant" :
choice == 5 ? "Cheese" :
"Invalid Item";
}
and then in the int main ():
int main()
{
const int MinGroceryValidChoice = 1;
const int MaxGroceryValidChoice = 5;
//we use for loops for known values
for (int choice = MinGroceryValidChoice; choice <= MaxGroceryValidChoice; ++choice){
cout << choice <<", "<< choiceToItem(choice)<< endl;
}
int choice = getIntValue("choice");
return 0;
}
I'm trying to have my program display my choice. Just a general point in the right direction would be much appreciated.