I have displayed an array in which I have added numbers in. Numbers 1 through 9, with 9 being a sentinel value. That I cannot figure out how to assign these numbers to the corresponding indicies. Please, please help

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
const int SENTINEL = 9;

struct menuItemType
{
    string menuItem;
    double menuPrice;
    bool selected;

};

void getData(ifstream & infile, menuItemType menu[], int & size);
void displayMenu(const menuItemType menu[], int & size);
void printCheck(menuItemType menu[], int & size);

int main()
{
    menuItemType menu[15];
	menuItemType orderNumber;
    int size;
	ifstream infile;

	getData(infile, menu, size);
	displayMenu(menu, size);
	printCheck(menu, size);

    return 0;

}

void getData(ifstream & infile, menuItemType menu[], int & size)
{
    int i = 0;

    infile.open("menu.txt");
    if(infile.fail())
    {
        cout << "The file could not be uploaded" << endl;
        exit(-1);
    }

	do
    {
        getline(infile, menu[i].menuItem);
        infile >> menu[i].menuPrice;
        infile.get();
        menu[i].selected = false;
        i++;
    }
	while(infile);
	size = i - 1;
}
void displayMenu(const menuItemType menu[], int & size)
{

    cout << fixed << showpoint << setprecision(2);
	cout << endl << "Menu Choices:" << endl;
    for(int i= 0; i < size; i++)
    {
        cout << left << i + 1 << ". " << setw(18) << menu[i].menuItem << setw(5) 
			 << right << menu[i].menuPrice << endl;
    }
	cout << "9. Finished Ordering" << endl;
	cout << endl;

}

// This is where I am stuck.  The user is supposed to pick breakfast choices from the menu
// using the numbers 1-9.  9 being the sentinel to finish their ordering and formulate a check
//  I cannot figure out how to associate the numbers 1. through 9. to the indicies where the 
// menu items are stored.  
void printCheck(menuItemType menu[], int & size)    
{
	
	int i = 0;
	char orderNumber;  // I Cant figure out how to declare this variable  //
cout << "Please enter the number of the choice you'd like to order: " << endl;
		cin >> orderNumber;

while(orderNumber != SENTINEL)
{
		cout << "Please enter the number of the choice you'd like to order: " << endl;
		cin >> orderNumber;
	
	
	if(orderNumber == 1)
	{
		cout << left << setw(18) << menu[0].menuItem << setw(5) 
		 << right << menu[0].menuPrice << endl;
	}
	else if(orderNumber == 2)
	{
		cout << left << setw(18) << menu[1].menuItem << setw(5) 
		 << right << menu[1].menuPrice << endl;
	}
	else if(orderNumber == 3)
	{
		cout << left << setw(18) << menu[2].menuItem << setw(5) 
		 << right << menu[2].menuPrice << endl;
	}
	else if(orderNumber == 4)
	{
		cout << left << setw(18) << menu[3].menuItem << setw(5) 
		 << right << menu[3].menuPrice << endl;
	}
	else if(orderNumber == 5)
	{
		cout << left << setw(18) << menu[4].menuItem << setw(5) 
		 << right << menu[4].menuPrice << endl;
	}
	else if(orderNumber == 6)
	{
		cout << left << setw(18) << menu[5].menuItem << setw(5) 
		 << right << menu[5].menuPrice << endl;
	}
	else if(orderNumber == 7)
	{
		cout << left << setw(18) << menu[6].menuItem << setw(5) 
		 << right << menu[6].menuPrice << endl;
	}
	else if(orderNumber == 8)
	{
		cout << left << setw(18) << menu[7].menuItem << setw(5) 
		 << right << menu[7].menuPrice << endl;
	}
	else(orderNumber == 9);
	{
		break;
	}

}
	

}

Recommended Answers

All 5 Replies

orderNumber is a char variable so it's value will be '1', '2', ... '9', not 1, 2, ... 9.

If you need it to become an integer, subtract '0' and it will be correct.

Thank you for your prompt response. That does make sense with the char variable and likewise input, but unfortunately that does not solve my program dilemma. Theres gotta be something wrong with the syntax of my loop of something within that last function that I am just not seeing. I've tried different things over and over with no avail. I'm sure its just staring right at me...but I dont see it. Thus I come here to see if someone whos maybe not so frustrated and has a clear head may look and see the issue at once

If you go back to what waltP said, orderNumber is a char, not an integer. So the line

while(orderNumber != SENTINEL)

will always be true!

So what happened when you fixed what I mentioned? And did you look at everything involved in that change?

I did get the program to run, but not to the results that I desire. I know there is something wrong with this loop and frankly, I don't really know how or where to begin to fix it. What I need to do is erase it altogether and start anew. From what I am gathering from the problem statement is that I should be using a bool controlled loop. I am assuming that I need to use the

bool selected;

that I had declared in the

menuItemType struct

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.