2

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.

  1. Bread
  2. Milk
  3. Eggs
  4. Deodorant
  5. 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.

  • Use [std::unordered_map](https://en.cppreference.com/w/cpp/container/unordered_map) to tie int and string values as key, value pair. – Aamir Mar 25 '23 at 20:41
  • *"I'm trying to have my program display my choice."* -- As in copying the line `cout << choice <<", "<< choiceToItem(choice)<< endl;` to right before `return 0;`? Seems strange that you know how to display all choices, but not the one the user picked. – JaMiT Mar 25 '23 at 20:43
  • *"My first attempt was to try something like this:"* -- your first attempt does not use a `switch`, so why is it not your final attempt? What's wrong with it? – JaMiT Mar 25 '23 at 20:44
  • @JaMiT when I try to output the choice, only the number shows up, I want the item to display. If I type in 1, I would like bread to appear. – Johng Jangus Mar 25 '23 at 21:05
  • @JohngJangus *"I would like bread to appear."* -- The same way that "Bread" appears when you list the choices? – JaMiT Mar 25 '23 at 21:26
  • @JaMiT Yes, like that, I'd like to have the actual string name appear whenever I put in an integer – Johng Jangus Mar 25 '23 at 21:31
  • @JohngJangus *"Yes, like that"* -- So do the exact same thing (call `choiceToItem`). What's the issue? – JaMiT Mar 25 '23 at 21:44

1 Answers1

3

this depends hugely on your exact problem, but assuming that you might not know the list of items fully when building the program (e.g. because they are loaded from a file, or the user is a allowed to manipulate the list) one datastructure that helps are maps, which are basically phone books.

there are different types, a simple one is just called map and you might want to use it something like this:

#include <map>
#include <string>

std::map<int, std::string> items = {
        {1,"Bread"},
        {2,"Milk"},
        {3,"Eggs"},
        {4,"Deoderant"},
        {5,"Cheese"},
};


int main(){
    //....
    int choice = 3; 
    
    auto it = items.find(choice); 
    std::string item_name = it == items.end()?"invalid item" : it.second;
    
    //...
}

if you need lookups in both directions (also from the item name to the number) then you would work with "bimaps". they consist of two maps, one for each lookup direction. those aren't in the c++ standard library, but there are many good implementations out there.

kritzikratzi
  • 19,662
  • 1
  • 29
  • 40