how to pass information from a array to a variable based on user input.

This is a discussion on how to pass information from a array to a variable based on user input. within the C++ Programming forums, part of the General Programming Boards category; I am making a program and am trying to figure out how to let the user select a item from ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    34

    how to pass information from a array to a variable based on user input.

    I am making a program and am trying to figure out how to let the user select a item from a menu then print out a bill based on the item the user chose. But i can assume that the user only can select 1 of each item. Here is the code. I will continue to work on it. I am still learning about arrays.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    
    
    using namespace std;
    
    
    struct menuItemType
    {
        string menuItem;
        double menuPrice;
    };
    
    
    const int NUMBER_OF_ROWS = 8;
    const int NUMBER_OF_COLUMNS = 2;
    
    
    
    
    
    
    void showMenu(menuItemType menuList[NUMBER_OF_ROWS]);
    void getData(menuItemType menuList[NUMBER_OF_ROWS]);
    double printCheck(menuItemType menuList[NUMBER_OF_ROWS]);
    
    
    int main()
    {
        
        menuItemType menuList[NUMBER_OF_ROWS];
        getData(menuList);
        showMenu(menuList);
        printCheck(menuList);
    }
    
    
    void getData(menuItemType menuList[NUMBER_OF_ROWS])
    {
        
        ifstream inFile;
        inFile.open("C:\\Users\\Dylan\\Documents\\InfileTravischapter9Page629.txt");
        
        for (int i = 0; i < 8; i++)
        {
            inFile >> menuList[i].menuItem
                   >> menuList[i].menuPrice;
        }       
    }
    
    
    void showMenu(menuItemType menuList[NUMBER_OF_ROWS])
    {
        menuItemType menuList;
        getData(menuList);
        int choice;
        for(int i=0; i < 8; i++)
        { 
        cout << menuList->menuPrice << endl;
        }
        cout << "What would you like to eat, you can have up to one of each item on the menu" << endl;
        cout << "Press the following keys to select your items" << endl;
        cout << "1 = Plain\n2 = Bacon and Egg\n 3 = Muffin\n 4 = French Toast\n 5 = Fruit Basket \n 6 = Cereal \n 7 = Coffee \n 8 = Tea" << endl;
        cin >> choice;
     }
    
    
    double printCheck()

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not quite germane to the question, but if you go to the trouble of reading the menu in from a datafile, you shouldn't hardcode the menu in your showMenu function, especially since you passed the data into the function.

    Right now, your showMenu function only allows them to type in one number, which you promptly throw away. Perhaps you could return that number to your main? Although you'll still have to deal with how you want to get multiple items input. (Possibly a loop with a sentinel value to stop input?)

    And then you only need to store whether an item was ordered or not, so you really just a variable that can store yes/no.... (EDIT: I should perhaps say an array of those variables.)

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Why do you call getData twice?

    As for your question, you need to store or use the user choices somewhere.
    A simple option would be to ask the user to say yes or no in a loop, i.e. for each of the items.
    If yes, add to the price, otherwise continue.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    uhmm why I have getData in the main function is probably just me forgetting to erase it. and alright so would the code for the loop look something like this.

    Code:
    (show the menu get input)
    cin >> choice;
    bill = choice + bill;
    bool selectAnother;
    if(selectAnother = 'yes')
    {
     (show the menu options)
    cin >> choice;
    bill = choice + bill;
    }

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    crap i'm retarded. That would just be a way to do the bill calculation. It dosen't grab the price from the array.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    I gotcha, I made a loop but its not grabbing the price from the array. Thats what im trying to figure out. The array is set up like this essentially. I am just trying to make sure i am able to pull the price from the array based on the users choice
    muffin 1.99
    egg 3.99

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This seems like a do-while loop to me, since you want to make sure the loop happens at least once.

    You're going to have to ask the user whether to stop, and just about any way you want to do so should basically work.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    Alright i set up the loop but now the only problem is actually pulling the data from the array. So if the user enters 1 the array will know that that means whatever the first item in the list is. How could i go about this?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would suppose that for the first item you would need to add menuList[0].menuPrice....

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    i could do this in some if statements but i think there may be a easier way

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Liangine View Post
    i could do this in some if statements but i think there may be a easier way
    Since you can't let a person buy the same thing twice, I'm thinking you're going to have to have some if statements around.

  12. #12
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    Quote Originally Posted by tabstop View Post
    Since you can't let a person buy the same thing twice, I'm thinking you're going to have to have some if statements around.
    Will do. I'll make the if statements then post the new code

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    Here is the code after I made added loops and if statements. I now am passing the users choice to the printcheck function wrong. It says type int is incompatible with my struct menuItemType.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    
    
    using namespace std;
    
    
    struct menuItemType
    {
    	string menuItem;
    	double menuPrice;
    };
    
    
    const int NUMBER_OF_ROWS = 8;
    const int NUMBER_OF_COLUMNS = 2;
    
    
    
    
    
    
    double showMenu(menuItemType menuList[NUMBER_OF_ROWS]);
    void getData(menuItemType menuList[NUMBER_OF_ROWS]);
    double printCheck(menuItemType menuList[NUMBER_OF_ROWS]);
    
    
    int main()
    {
    	int choice;
    	menuItemType menuList[NUMBER_OF_ROWS];
    	getData(menuList);
    	showMenu(menuList);
    	
    	printCheck(choice);
    	
    }
    
    
    void getData(menuItemType menuList[NUMBER_OF_ROWS])
    {
    	
    	ifstream inFile;
    	inFile.open("C:\\Users\\Dylan\\Documents\\InfileTravischapter9Page629.txt");
    	
    	for (int i = 0; i < 8; i++)
    	{
    		inFile >> menuList[i].menuItem
    			   >> menuList[i].menuPrice;
    	}       
    }
    
    
    double showMenu(menuItemType menuList[NUMBER_OF_ROWS])
    {
    	menuItemType menuList;
    	getData(menuList);
    	int choice;
    	bool selectAnother;
    	for(int i=0; i < 8; i++)
    	{ 
    	cout << menuList->menuPrice << endl;
    	}
    	cout << "What would you like to eat, you can have up to one of each item on the menu" << endl;
    	cout << "Press the following keys to select your items" << endl;
    	cout << "1 = Plain\n2 = Bacon and Egg\n 3 = Muffin\n 4 = French Toast\n 5 = Fruit Basket \n 6 = Cereal \n 7 = Coffee \n 8 = Tea" << endl;
    	cin >> choice;
    	cout << "Would you like to select another item? Enter yes or no" << endl;
    	cin >> selectAnother;
    	if(selectAnother == 'yes')
    	{
        cout << "What would you like to eat, you can have up to one of each item on the menu" << endl;
    	cout << "Press the following keys to select your items" << endl;
    	cout << "1 = Plain\n2 = Bacon and Egg\n 3 = Muffin\n 4 = French Toast\n 5 = Fruit Basket \n 6 = Cereal \n 7 = Coffee \n 8 = Tea" << endl;
    	cin >> choice;
    	cout << "Would you like to select another item? Enter yes or no" << endl;
    	cin >> selectAnother;
    	}
    
    
    
    
    	return 0;
    }
    
    
    double printCheck(int choice)
    {
    	double bill;
    	double price;
    	double check;
    	showMenu(choice);
    	if(choice == 1)
    		price = 1.45;
    	if(choice == 2)
    		price = 2.45;
    	if(choice == 3)
    		price = .99;
    	if(choice == 4)
    		price = 1.99;
    	if(choice == 5)
    		price = 2.49;
    	if(choice == 6)
    		price = .69;
    	if (choice == 7)
    		price = .50;
    	if(choice == 8)
    		price = .75;
    
    
    
    
    	bill = bill + price;
    	check = bill * .05 + bill;
    
    
    	cout << "Your check is $" << check << endl;
        
    }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're trying to declare a variable with the same name as a variable that doesn't exist there in line 59 (as posted), so don't do that.

    A boolean cannot equal a string, it can only equal true or false.

    There's no reason to artificially limit them to two choices; use a loop to continue asking.

    All the variables you have called "choice" are separate and distinct; changing the value of choice in showMenu will not change the value of choice in main or in printCheck.

  15. #15
    Registered User
    Join Date
    Oct 2013
    Posts
    34
    What do you mean by separate and distinct? i fixed the bool to string problem and made the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User input variable to global array help
    By Bob_the_Builder in forum C Programming
    Replies: 5
    Last Post: 10-11-2013, 12:51 PM
  2. Printing a Solid Square based on user input
    By ThatBoy1124 in forum C Programming
    Replies: 3
    Last Post: 03-01-2011, 09:07 PM
  3. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 12:21 AM
  4. creating a struct based on user input
    By rodrigorules in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 06:16 PM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21