I am having a problem getting a certain part of my program to loop. I need the loop to repeat if 'Y' or 'y' is picked after a menu item is chosen. And if 'N' or "n" is picked print the receipt. I have tried numerous times to get the loop correct, but cannot figure it out. Any help would be beneficial. Also, I can now get the amount due to calculate but it does not seem to add the tax right, it adds extra after tax is calculated.

#include <iostream> 
#include <string> 
#include <iomanip> 
void showMenu(); 

using namespace std; 

const int menu = 1; 

struct menuItemType 

{ 
   string menuItem; 
   double menuPrice; 
}; 

void getData(menuItemType placeorder[8]); 
void printCheck(menuItemType printorder[]); 

int main() 

{ 
    cout <<"Welcome to Johnny's Restaurant"<< endl;
    cout <<"----Today's Menu----"<< endl; 
    showMenu();
    cout << endl;
    cout << "You can make up to 8 single order selections"<<endl; 
    cout << "Do you want to make a selection Y/y (Yes). N/n (No): ";
    char selection;
    cin >> selection;
    if (selection == 'Y' || selection == 'y')
    {
                    
    menuItemType menuList[8]; 
    menuItemType order[8]; 
    getData(order); 
    printCheck(order); 
    
    }
                  
    system("PAUSE");
    return 0; 
} 

void showMenu() 
{  
	cout << "1: Plain Egg.................$ 1.45" << endl; 
	cout << "2: Bacon and Egg.............$ 2.45" << endl; 
	cout << "3: Muffin....................$ 0.99" << endl; 
	cout << "4: French Toast..............$ 1.99" << endl; 
	cout << "5: Fruit Basket..............$ 2.49" << endl; 
	cout << "6: Cereal....................$ 0.69" << endl; 
	cout << "7: Coffee....................$ 0.50" << endl; 
	cout << "8: Tea.......................$ 0.75" << endl; 
} 

// puts data into the array
 
void getData(menuItemType placeorder[8]) 
{ 
	menuItemType menuList[8]; 
	menuList[0].menuItem = "Plain Egg     ";      
	menuList[1].menuItem = "Bacon and Egg "; 
	menuList[2].menuItem = "Muffin        ";          
	menuList[3].menuItem = "French Toast  ";      
	menuList[4].menuItem = "Fruit Basket  "; 
	menuList[5].menuItem = "Cereal        "; 
	menuList[6].menuItem = "Coffee        "; 
	menuList[7].menuItem = "Tea           "; 

	menuList[0].menuPrice = 1.45; 
	menuList[1].menuPrice = 2.45; 
	menuList[2].menuPrice = 0.99; 
	menuList[3].menuPrice = 1.99; 
	menuList[4].menuPrice = 2.49; 
	menuList[5].menuPrice = 0.69; 
	menuList[6].menuPrice = 0.50; 
	menuList[7].menuPrice = 0.75; 
	
    cout << endl; 
    
 
 for (int x=0; x<8; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
    if (selectionTwo == 'N' || selectionTwo == 'n')
    {  
                
                     } 
     
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   } 
} 

// calculates and prints the RECEIPT
 
void printCheck(menuItemType printorder[]) 
{ 
	const double taxRate = 0.05; 
	int x = 0;
	double amtDue = 0;
	double tax = 0; 
	
	cout << endl;
	cout << endl;
	cout << endl;

 
	cout << "Welcome to Johnny's Restaurant" << endl;  
	cout << endl;

	for (x=0; x<8; x++) 
	{ 
	cout.precision(3);
	cout << showpoint;
	tax += (printorder[x].menuPrice) * taxRate;
	cout << printorder[x].menuItem << setw(13) << "$ " << printorder[x].menuPrice << endl;
    amtDue += (printorder[x].menuPrice) + tax; 
	}

	cout.precision(2);
	cout << showpoint;
	cout<<"Tax                      $ " << tax << endl; 
	cout.precision(3);
	cout << showpoint;
	cout<<"Amount Due               $ " << amtDue << endl;
	

}

Recommended Answers

All 15 Replies

for (int x=0; x<8; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
    if (selectionTwo == 'N' || selectionTwo == 'n')
    {  
                
                     } 
     
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   }

You want to add another condition to your for loop. Right now, the loop repeats if x < 8. You want to keep that condition and add another to it. How about setting up a boolean variable called doneOrdering and placing it before the for loop?

bool doneOrdering = // think about whether to initialize it to false
                    // or true
for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
    if (selectionTwo == 'N' || selectionTwo == 'n')
    {  
                
                     } 
     
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   }

You want to exit the loop if the person is done ordering, so you'll want to change the value of doneOrdering inside your loop somewhere under certain conditions.

Thanks for the help! I now have a direction to work towards. Great explanation!

Ok I have changed a few things from the advice I was given. And the loop seems to work properly. But, now when the receipt is printed it has added numbers and characters. I assume it is from the loop I am using to change the value of doneOrdering. Am I doing this properly???

#include <iostream> 
#include <string> 
#include <iomanip> 
void showMenu(); 

using namespace std; 

const int menu = 1; 

struct menuItemType 

{ 
   string menuItem; 
   double menuPrice; 
}; 

void getData(menuItemType placeorder[8]); 
void printCheck(menuItemType printorder[]); 

int main() 

{ 
    cout <<"Welcome to Johnny's Restaurant"<< endl;
    cout <<"----Today's Menu----"<< endl; 
    showMenu();
    cout << endl;
    cout << "You can make up to 8 single order selections"<<endl; 
    cout << "Do you want to make a selection Y/y (Yes). N/n (No): ";
    char selection;
    cin >> selection;
    if (selection == 'Y' || selection == 'y')
    {
                    
    menuItemType menuList[8]; 
    menuItemType order[8]; 
    getData(order); 
    printCheck(order); 
    
    }
                  
    system("PAUSE");
    return 0; 
} 

void showMenu() 
{  
	cout << "1: Plain Egg.................$ 1.45" << endl; 
	cout << "2: Bacon and Egg.............$ 2.45" << endl; 
	cout << "3: Muffin....................$ 0.99" << endl; 
	cout << "4: French Toast..............$ 1.99" << endl; 
	cout << "5: Fruit Basket..............$ 2.49" << endl; 
	cout << "6: Cereal....................$ 0.69" << endl; 
	cout << "7: Coffee....................$ 0.50" << endl; 
	cout << "8: Tea.......................$ 0.75" << endl; 
} 

// puts data into the array
 
void getData(menuItemType placeorder[8]) 
{ 
	menuItemType menuList[8]; 
	menuList[0].menuItem = "Plain Egg     ";      
	menuList[1].menuItem = "Bacon and Egg "; 
	menuList[2].menuItem = "Muffin        ";          
	menuList[3].menuItem = "French Toast  ";      
	menuList[4].menuItem = "Fruit Basket  "; 
	menuList[5].menuItem = "Cereal        "; 
	menuList[6].menuItem = "Coffee        "; 
	menuList[7].menuItem = "Tea           "; 

	menuList[0].menuPrice = 1.45; 
	menuList[1].menuPrice = 2.45; 
	menuList[2].menuPrice = 0.99; 
	menuList[3].menuPrice = 1.99; 
	menuList[4].menuPrice = 2.49; 
	menuList[5].menuPrice = 0.69; 
	menuList[6].menuPrice = 0.50; 
	menuList[7].menuPrice = 0.75; 
	
    cout << endl; 
    
 bool doneOrdering = 0;
 
 for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
        if (selectionTwo == 'N' || selectionTwo == 'n')
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   } 
} 

// calculates and prints the RECEIPT
 
void printCheck(menuItemType printorder[]) 
{ 
	const double taxRate = 0.05; 
	int x = 0;
	double amtDue = 0;
	double tax = 0; 
	
	cout << endl;
	cout << endl;
	cout << endl;

 
	cout << "Welcome to Johnny's Restaurant" << endl;  
	cout << endl;

	for (x=0; x<8; x++) 
	{ 
	cout.precision(3);
	cout << showpoint;
	tax += (printorder[x].menuPrice) * taxRate;
	cout << printorder[x].menuItem << setw(13) << "$ " << printorder[x].menuPrice << endl;
    amtDue += (printorder[x].menuPrice) + tax; 
	}

	cout.precision(2);
	cout << showpoint;
	cout<<"Tax                      $ " << tax << endl; 
	cout.precision(3);
	cout << showpoint;
	cout<<"Amount Due               $ " << amtDue << endl;
	

}
bool doneOrdering = 0;
 
 for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
        if (selectionTwo == 'N' || selectionTwo == 'n')
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   }

You can use true and false in place of 1 and 0, but yes, you did the loop correctly. Note that you have not changed the OTHER loop:

for (x=0; x<8; x++)

If I order three items, the first for loop executes three times, but the second loop executes eight times. They need to execute the same number of times, which they currently do not. So you need to store that 3 in some variable and that second for loop needs to use that variable. Let's pick a descriptive name. Lets call that variable numItemsOrdered. numItemsOrdered is equal to 3. Now we get to the for loop in your printCheck function:

for (x=0; x<8; x++)

Change this to use numItemsOrdered. Now the question is: how do you assign the value of 3 to numItemsOrdered and make it so it can be used in the printCheck function? Hint: you'll want to assign it somewhere in this for-loop:

bool doneOrdering = 0;
 
 for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
        if (selectionTwo == 'N' || selectionTwo == 'n')
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   }

There is more than one way to do this, but you need to somehow end up with a variable storing 3 AFTER this for-loop has finished and that 3 needs to exist somewhere AFTER the getData function has completed. So that means that numItemsOrdered must be either a global variable or you must pass it to both the getData and printCheck functions.

Better try these functions (don't cram a main story with <<...>> detail):

// Homework assignment: add comments...

bool ack(const char* prompt = 0);

inline 
bool ack(const std::string& prompt)
{
    return ack(prompt.c_str());
}

inline
bool nak(const char* prompt = 0)
{
    return !ack(prompt);
}

inline
bool nak(const std::string& prompt)
{
    return !ack(prompt);
}

bool ack(const char* prompt)
{
    std::string answer;
    const char* typeYN = "Enter [Y|y|N|n]: ";

    if (!prompt)
        prompt = typeYN;
    
    while (std::cout << prompt, std::cout.flush())
    {
        if (std::getline(std::cin,answer))
        {
            if (answer.length())
            {
                switch (answer[0])
                {
                case 'n': case 'N':
                    return false;
                case 'y': case 'Y':
                    return true;
                }
            }
        }
        else break;
        prompt = typeYN;
    }
    return false;
}

// Try it; press Ctrl+Z to quit.
int main()
{
    while (std::cin)
    {
        std::cout << ack("Are you ready to die\? ") 
            << std::endl;
    }
	return 0;
}

Ok I must not have understood the advice. I tried adding a global variable "numItemsOrdered". And, my function pritReceipt is still adding a bunch of numbers to the output. Is that because I am not adding the new variable to the printReceipt function?? Also, can I get any advice as to why my amount due is calculating more than it should when the tax is added. I appreciate all the help!

#include <iostream> 
#include <string> 
#include <iomanip> 
void showMenu(); 

using namespace std; 

const int menu = 1; 

struct menuItemType 

{ 
   string menuItem; 
   double menuPrice; 
}; 

void getData(menuItemType placeorder[8]); 
void printCheck(menuItemType printorder[]); 
int numItemsOrdered = 3;

int main() 

{ 
    cout <<"Welcome to Johnny's Restaurant"<< endl;
    cout <<"----Today's Menu----"<< endl; 
    showMenu();
    cout << endl;
    cout << "You can make up to 8 single order selections"<<endl; 
    cout << "Do you want to make a selection Y/y (Yes). N/n (No): ";
    char selection;
    cin >> selection;
    if (selection == 'Y' || selection == 'y')
    {
                    
    menuItemType menuList[8]; 
    menuItemType order[8]; 
    getData(order); 
    printCheck(order); 
    
    }
                  
    system("PAUSE");
    return 0; 
} 

void showMenu() 
{  
	cout << "1: Plain Egg.................$ 1.45" << endl; 
	cout << "2: Bacon and Egg.............$ 2.45" << endl; 
	cout << "3: Muffin....................$ 0.99" << endl; 
	cout << "4: French Toast..............$ 1.99" << endl; 
	cout << "5: Fruit Basket..............$ 2.49" << endl; 
	cout << "6: Cereal....................$ 0.69" << endl; 
	cout << "7: Coffee....................$ 0.50" << endl; 
	cout << "8: Tea.......................$ 0.75" << endl; 
} 

// puts data into the array
 
void getData(menuItemType placeorder[8]) 
{ 
	menuItemType menuList[8]; 
	menuList[0].menuItem = "Plain Egg     ";      
	menuList[1].menuItem = "Bacon and Egg "; 
	menuList[2].menuItem = "Muffin        ";          
	menuList[3].menuItem = "French Toast  ";      
	menuList[4].menuItem = "Fruit Basket  "; 
	menuList[5].menuItem = "Cereal        "; 
	menuList[6].menuItem = "Coffee        "; 
	menuList[7].menuItem = "Tea           "; 

	menuList[0].menuPrice = 1.45; 
	menuList[1].menuPrice = 2.45; 
	menuList[2].menuPrice = 0.99; 
	menuList[3].menuPrice = 1.99; 
	menuList[4].menuPrice = 2.49; 
	menuList[5].menuPrice = 0.69; 
	menuList[6].menuPrice = 0.50; 
	menuList[7].menuPrice = 0.75; 
	
    cout << endl; 
    
 bool doneOrdering = 0;
 
 for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
        if (selectionTwo == 'N' || selectionTwo == 'n' && numItemsOrdered == 3)
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   } 
} 

// calculates and prints the RECEIPT
 
void printCheck(menuItemType printorder[]) 
{ 
	const double taxRate = 0.05; 
	int x = 0;
	double amtDue = 0;
	double tax = 0; 
	
	cout << endl;
	cout << endl;
	cout << endl;

 
	cout << "Welcome to Johnny's Restaurant" << endl;  
	cout << endl;

	for (x=0; x<8 && numItemsOrdered == 3; x++) 
	{ 
	cout.precision(3);
	cout << showpoint;
	tax += (printorder[x].menuPrice) * taxRate;
	cout << printorder[x].menuItem << setw(13) << "$ " << printorder[x].menuPrice << endl;
    amtDue += (printorder[x].menuPrice) + tax; 
	}

	cout.precision(2);
	cout << showpoint;
	cout<<"Tax                      $ " << tax << endl; 
	cout.precision(3);
	cout << showpoint;
	cout<<"Amount Due               $ " << amtDue << endl;
	

}

I Think, There is stdin flush problem!
use fflush(stdin); after all cin statments!
your problem will get solved!
all the best!

It's not allowed to flush input streams in C and C++.
Search Google, search this forum...

Ok I must not have understood the advice. I tried adding a global variable "numItemsOrdered". And, my function pritReceipt is still adding a bunch of numbers to the output. Is that because I am not adding the new variable to the printReceipt function?? Also, can I get any advice as to why my amount due is calculating more than it should when the tax is added. I appreciate all the help!

O.K. You are making numItemsOrdered global, so that means you do not need to pass it to the functions. I had numItemsOrdered equal to 3 for my particular example. It may not be, so in line 19, you are declaring it in the right place (globally), but you don't need to initialize it. You can, but it's unnecessary because if you code the getData function correctly it's going to get overwritten, so don't confuse things. If you want to initialize it to something, just initialize it to 0 because at the time it's declared, nothing has been ordered yet, so at least it'll be accurate if you initialize it to 0. Now let's look at your getData loop (lines 85 - 103). You have a loop counter x that starts at 0 and is increased by one every time an order is made, which is exactly what you want in your variable numItemsOrdered. So I think the best bet is to simply replace x with the variable numItemsOrdered everywhere in those lines. So line 85 becomes:

for (numItemsOrdered = 0; numItemsOrdered <8 && !doneOrdering; 
          numItemsOrdered++)

Note that I have removed the word "int" that used to be there:

for (int x=0; x<8 && !doneOrdering; x++)

This is because I do not want to create a NEW variable called numItemsOrdered , but instead want to use the global variable called that. It's an important distinction and it has to do with the "scope" of the variable. So when you are done with that for-loop you should have an accurate value in numItemsOrdered. To make sure, add this line between lines 103 and 104:

cout << "You have ordered " << numItemsOrdered << " items " << endl;

Delete it later, but it's a good line for temporary verification purposes. Replace x with numItemsOrdered everywhere you see it in this function. As an experiment, put the int back in line 185 and see how doing so can result in the line above that I added where it says how many items you've ordered being inaccurate. Make sure this line accurately says how many items you've ordered. Experiment by ordering all sorts of different numbers of items. Don't work on the printCheck function until this line displays accurately.

Change line 95 back to how you had it before. There's nothing about the number 3 that should be hard-coded. In my particular hypothetical example, I ordered 3 items, but that'll vary, so don't put 3 in the program. Line 95 should be this:

if (selectionTwo == 'N' || selectionTwo == 'n')

Line 123 is wrong.

for (x=0; x<8 && numItemsOrdered == 3; x++)

Again, 3 was an example and shouldn't be hard-coded in the program. Think how many times you need to go through this loop and adjust accordingly. Should the number 8 be there at all? Keep in mind that in the first loop we didn't know how many times we were going through the loop before the loop started and that's why we needed to two conditions. Think about how many times the loop above needs to be executed and design it accordingly.

#include <iostream> 
#include <string> 
#include <iomanip> 
void showMenu(); 

using namespace std; 

const int menu = 1; 

struct menuItemType 

{ 
   string menuItem; 
   double menuPrice; 
}; 

void getData(menuItemType placeorder[8]); 
void printCheck(menuItemType printorder[]); 
int numItemsOrdered = 3;

int main() 

{ 
    cout <<"Welcome to Johnny's Restaurant"<< endl;
    cout <<"----Today's Menu----"<< endl; 
    showMenu();
    cout << endl;
    cout << "You can make up to 8 single order selections"<<endl; 
    cout << "Do you want to make a selection Y/y (Yes). N/n (No): ";
    char selection;
    cin >> selection;
    if (selection == 'Y' || selection == 'y')
    {
                    
    menuItemType menuList[8]; 
    menuItemType order[8]; 
    getData(order); 
    printCheck(order); 
    
    }
                  
    system("PAUSE");
    return 0; 
} 

void showMenu() 
{  
	cout << "1: Plain Egg.................$ 1.45" << endl; 
	cout << "2: Bacon and Egg.............$ 2.45" << endl; 
	cout << "3: Muffin....................$ 0.99" << endl; 
	cout << "4: French Toast..............$ 1.99" << endl; 
	cout << "5: Fruit Basket..............$ 2.49" << endl; 
	cout << "6: Cereal....................$ 0.69" << endl; 
	cout << "7: Coffee....................$ 0.50" << endl; 
	cout << "8: Tea.......................$ 0.75" << endl; 
} 

// puts data into the array
 
void getData(menuItemType placeorder[8]) 
{ 
	menuItemType menuList[8]; 
	menuList[0].menuItem = "Plain Egg     ";      
	menuList[1].menuItem = "Bacon and Egg "; 
	menuList[2].menuItem = "Muffin        ";          
	menuList[3].menuItem = "French Toast  ";      
	menuList[4].menuItem = "Fruit Basket  "; 
	menuList[5].menuItem = "Cereal        "; 
	menuList[6].menuItem = "Coffee        "; 
	menuList[7].menuItem = "Tea           "; 

	menuList[0].menuPrice = 1.45; 
	menuList[1].menuPrice = 2.45; 
	menuList[2].menuPrice = 0.99; 
	menuList[3].menuPrice = 1.99; 
	menuList[4].menuPrice = 2.49; 
	menuList[5].menuPrice = 0.69; 
	menuList[6].menuPrice = 0.50; 
	menuList[7].menuPrice = 0.75; 
	
    cout << endl; 
    
 bool doneOrdering = 0;
 
 for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
        if (selectionTwo == 'N' || selectionTwo == 'n' && numItemsOrdered == 3)
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   } 
} 

// calculates and prints the RECEIPT
 
void printCheck(menuItemType printorder[]) 
{ 
	const double taxRate = 0.05; 
	int x = 0;
	double amtDue = 0;
	double tax = 0; 
	
	cout << endl;
	cout << endl;
	cout << endl;

 
	cout << "Welcome to Johnny's Restaurant" << endl;  
	cout << endl;

	for (x=0; x<8 && numItemsOrdered == 3; x++) 
	{ 
	cout.precision(3);
	cout << showpoint;
	tax += (printorder[x].menuPrice) * taxRate;
	cout << printorder[x].menuItem << setw(13) << "$ " << printorder[x].menuPrice << endl;
    amtDue += (printorder[x].menuPrice) + tax; 
	}

	cout.precision(2);
	cout << showpoint;
	cout<<"Tax                      $ " << tax << endl; 
	cout.precision(3);
	cout << showpoint;
	cout<<"Amount Due               $ " << amtDue << endl;
	

}

I used the fflush(stdin); after all cin statements and that did not seem to change a thing. Any other advice? Maybe I used it wrong?? Calculations for amount due are still wrong and output for printReceipt is still adding numbers and characters to the output.

#include <iostream> 
#include <string> 
#include <iomanip> 
void showMenu(); 

using namespace std; 

const int menu = 1; 

struct menuItemType 

{ 
   string menuItem; 
   double menuPrice; 
}; 

void getData(menuItemType placeorder[8]); 
void printCheck(menuItemType printorder[]); 
int numItemsOrdered = 3;

int main() 

{ 
    cout <<"Welcome to Johnny's Restaurant"<< endl;
    cout <<"----Today's Menu----"<< endl; 
    showMenu();
    cout << endl;
    cout << "You can make up to 8 single order selections"<<endl; 
    cout << "Do you want to make a selection Y/y (Yes). N/n (No): ";
    char selection;
    cin >> selection;
    fflush(stdin); 
    if (selection == 'Y' || selection == 'y')
    {
                    
    menuItemType menuList[8]; 
    menuItemType order[8]; 
    getData(order); 
    printCheck(order); 
    
    }
                  
    system("PAUSE");
    return 0; 
} 

void showMenu() 
{  
	cout << "1: Plain Egg.................$ 1.45" << endl; 
	cout << "2: Bacon and Egg.............$ 2.45" << endl; 
	cout << "3: Muffin....................$ 0.99" << endl; 
	cout << "4: French Toast..............$ 1.99" << endl; 
	cout << "5: Fruit Basket..............$ 2.49" << endl; 
	cout << "6: Cereal....................$ 0.69" << endl; 
	cout << "7: Coffee....................$ 0.50" << endl; 
	cout << "8: Tea.......................$ 0.75" << endl; 
} 

// puts data into the array
 
void getData(menuItemType placeorder[8]) 
{ 
	menuItemType menuList[8]; 
	menuList[0].menuItem = "Plain Egg     ";      
	menuList[1].menuItem = "Bacon and Egg "; 
	menuList[2].menuItem = "Muffin        ";          
	menuList[3].menuItem = "French Toast  ";      
	menuList[4].menuItem = "Fruit Basket  "; 
	menuList[5].menuItem = "Cereal        "; 
	menuList[6].menuItem = "Coffee        "; 
	menuList[7].menuItem = "Tea           "; 

	menuList[0].menuPrice = 1.45; 
	menuList[1].menuPrice = 2.45; 
	menuList[2].menuPrice = 0.99; 
	menuList[3].menuPrice = 1.99; 
	menuList[4].menuPrice = 2.49; 
	menuList[5].menuPrice = 0.69; 
	menuList[6].menuPrice = 0.50; 
	menuList[7].menuPrice = 0.75; 
	
    cout << endl; 
    
 bool doneOrdering = 0;
 
 for (int x=0; x<8 && !doneOrdering; x++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    fflush(stdin); 
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo;
    fflush(stdin); 
        if (selectionTwo == 'N' || selectionTwo == 'n' && numItemsOrdered == 3)
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[x].menuItem = menuList[answer-1].menuItem; 
    placeorder[x].menuPrice = menuList[answer-1].menuPrice; 
   } 
} 

// calculates and prints the RECEIPT
 
void printCheck(menuItemType printorder[]) 
{ 
	const double taxRate = 0.05; 
	int x = 0;
	double amtDue = 0;
	double tax = 0; 
	
	cout << endl;
	cout << endl;
	cout << endl;

 
	cout << "Welcome to Johnny's Restaurant" << endl;  
	cout << endl;

	for (x=0; x<8 && numItemsOrdered == 3; x++) 
	{ 
	cout.precision(3);
	cout << showpoint;
	tax += (printorder[x].menuPrice) * taxRate;
	cout << printorder[x].menuItem << setw(13) << "$ " << printorder[x].menuPrice << endl;
    amtDue += (printorder[x].menuPrice) + tax; 
	}

	cout.precision(2);
	cout << showpoint;
	cout<<"Tax                      $ " << tax << endl; 
	cout.precision(3);
	cout << showpoint;
	cout<<"Amount Due               $ " << amtDue << endl;
	

}

I used the fflush(stdin); after all cin statements and that did not seem to change a thing. Any other advice? Maybe I used it wrong?? Calculations for amount due are still wrong and output for printReceipt is still adding numbers and characters to the output.

Flushing isn't the problem. Look at my last post.

Ok I followed the advice and have worked out the problems. My last problem concerns a few items from my menu. When I buy anything less than 1.00 such as .75 I get an added zero in the output .750 which changes the amount due. What can I do to stop this problem??
Try ordering item number 8 to see my results.
Here is my updated code:

#include <iostream> 
#include <string> 
#include <iomanip> 
void showMenu(); 

using namespace std; 

const int menu = 1; 

struct menuItemType 

{ 
   string menuItem; 
   double menuPrice; 
}; 

void getData(menuItemType placeorder[8]); 
void printCheck(menuItemType printorder[]); 
int numItemsOrdered;

int main() 

{ 
    cout <<"Welcome to Johnny's Restaurant"<< endl;
    cout <<"----Today's Menu----"<< endl; 
    showMenu();
    cout << endl;
    cout << "You can make up to 8 single order selections"<<endl; 
    cout << "Do you want to make a selection Y/y (Yes). N/n (No): ";
    char selection;
    cin >> selection; 
    if (selection == 'Y' || selection == 'y')
    {
                    
    menuItemType menuList[8]; 
    menuItemType order[8]; 
    getData(order); 
    printCheck(order); 
    
    }
                  
    system("PAUSE");
    return 0; 
} 

void showMenu() 
{  
	cout << "1: Plain Egg.................$ 1.45" << endl; 
	cout << "2: Bacon and Egg.............$ 2.45" << endl; 
	cout << "3: Muffin....................$ 0.99" << endl; 
	cout << "4: French Toast..............$ 1.99" << endl; 
	cout << "5: Fruit Basket..............$ 2.49" << endl; 
	cout << "6: Cereal....................$ 0.69" << endl; 
	cout << "7: Coffee....................$ 0.50" << endl; 
	cout << "8: Tea.......................$ 0.75" << endl; 
} 

// puts data into the array
 
void getData(menuItemType placeorder[8]) 
{ 
	menuItemType menuList[8]; 
	menuList[0].menuItem = "Plain Egg     ";      
	menuList[1].menuItem = "Bacon and Egg "; 
	menuList[2].menuItem = "Muffin        ";          
	menuList[3].menuItem = "French Toast  ";      
	menuList[4].menuItem = "Fruit Basket  "; 
	menuList[5].menuItem = "Cereal        "; 
	menuList[6].menuItem = "Coffee        "; 
	menuList[7].menuItem = "Tea           "; 

	menuList[0].menuPrice = 1.45; 
	menuList[1].menuPrice = 2.45; 
	menuList[2].menuPrice = 0.99; 
	menuList[3].menuPrice = 1.99; 
	menuList[4].menuPrice = 2.49; 
	menuList[5].menuPrice = 0.69; 
	menuList[6].menuPrice = 0.50; 
	menuList[7].menuPrice = 0.75; 
	
    cout << endl; 
    
 bool doneOrdering = 0;
 
 for (numItemsOrdered = 0; numItemsOrdered <8 && !doneOrdering; numItemsOrdered++) 
   { 
	
    cout << " " << endl;
    int answer; 
    cout << "Enter item number: ";
    cin >> answer;
    cout << "Select another item Y/y (Yes), N/n (No): "; 
    char selectionTwo;
    cin >> selectionTwo; 
        if (selectionTwo == 'N' || selectionTwo == 'n')
        {  
         doneOrdering = 1;      
                      
                      }
                      
    placeorder[numItemsOrdered].menuItem = menuList[answer-1].menuItem; 
    placeorder[numItemsOrdered].menuPrice = menuList[answer-1].menuPrice; 
   } 
   cout << "You have ordered " << numItemsOrdered << " items " << endl;
} 

// calculates and prints the RECEIPT
 
void printCheck(menuItemType printorder[]) 
{ 
	const double taxRate = 0.05; 
	int x = 0;
	double amtDue = 0;
	double tax = 0; 
	
	cout << endl;
	cout << endl;
	cout << endl;

 
	cout << "Welcome to Johnny's Restaurant" << endl;  
	cout << endl;

	for (x=0; x<numItemsOrdered; x++) 
	{ 
	cout.precision(3);
	cout << showpoint;
	tax += (printorder[x].menuPrice) * taxRate;
	cout << printorder[x].menuItem << setw(13) << "$ " << printorder[x].menuPrice << endl;
    amtDue += (printorder[x].menuPrice) + tax; 
	}

	cout.precision(2);
	cout << showpoint;
	cout<<"Tax                      $ " << tax << endl; 
	cout.precision(3);
	cout << showpoint;
	cout<<"Amount Due               $ " << amtDue << endl;
	

}

Regarding the formatting and the desire to have two digits to the right of the decimal point, check out this link towards the bottom:

http://www.cs.fsu.edu/~myers/c++/notes/basics1.html

The relevant portion is:

Some special formatting for decimal numbers

* By default, decimal (floating-point) numbers will usually print out only as far as needed, up to a certain preset number of decimal places (before rounding the printed result)

double x = 4.5, y = 12.666666666666, z = 5.0;

cout << x; // will likely print 4.5
cout << y; // will likely print 12.6667
cout << z; // will likely print 5

* A special "magic formula" for controlling how many decimal places are printed:

cout.setf(ios::fixed); // specifies fixed point notation
cout.setf(ios::showpoint); // so that decimal point will always be shown
cout.precision(2); // sets floating point types to print to
// 2 decimal places (or use your desired number)

Any cout statements following these will output floating-point values in the usual notation, to 2 decimal places.

double x = 4.5, y = 12.666666666666, z = 5.0;

cout << x; // prints 4.50
cout << y; // prints 12.67
cout << z; // prints 5.00

* An alternative to fixed point notation is scientific notation:

cout.setf(ios::scientific); // float types formatted in exponential notation

You still have some problems with the way you are calculating tax and amount due in the printCheck function. For example, when I order two coffees I get this:


Coffee $ 0.500
Coffee $ 0.500
Tax $ 0.050
Amount Due $ 1.08

Thanks your information helped a lot! I got it fixed and runs fine now. Except the amount due is wrong (Like you stated). I cannot figure out why though!! Any more clues?

Thanks your information helped a lot! I got it fixed and runs fine now. Except the amount due is wrong (Like you stated). I cannot figure out why though!! Any more clues?

Let's assume I've ordered two cups of coffee, each for 50 cents. That's $1.00. With a 5% tax rate, I should be charged $0.05 in taxes. $1.00 + $0.05 = $1.05. I am currently being charged $1.075 (ignoring round-off approximation in display), which means $0.075 is being added to my bill, not $0.05.

Let's look at the pertinent code:

const double taxRate = 0.05; 
int x = 0;
double amtDue = 0; 
double tax = 0; 
	
for (x=0; x<numItemsOrdered; x++) 
{ 
     tax += (printorder[x].menuPrice) * taxRate;
     amtDue += (printorder[x].menuPrice) + tax; 
}

In this particular case, numItemsOrdered==2 and printorder[x].menuPrice==0.50 and taxRate==0.05 , so I'm going to change the code to zone into that (note: don't change the code of the program).

for (x=0; x<2; x++) 
{ 
     tax += (0.50 * 0.05);
     amtDue += 0.50 + tax; 
}

Doing the multiplication of 0.50 * 0.05, we get 0.025, so let's do another substitution.

for (x=0; x<2; x++) 
{ 
     tax += 0.025;
     amtDue += 0.50 + tax; 
}

So here's the code, tailored to when we buy two coffees (again, this is just a temporary change in the code to zone in on the problem. Don't hard code 2 and 0.50 in the real program, or if you do, change it back once you solve it):

double amtDue = 0;
double tax = 0; 
for (x=0; x<2; x++) 
{ 
     tax += 0.025;
     amtDue += 0.50 + tax; 
}

First, without using a computer, on paper, pretend you were a cashier and someone ordered 2 coffees from you and you had to figure out tax and amtDue. Think about the steps you are going through math-wise. The math might be easy enough in this case to do in your head, but write the steps you followed out on paper since you are looking for the process to follow, not just the answer. Then go through the code above (again on paper) and see if you are going through those same steps that you did when you didn't need to program it. Hopefully that process will help you spot your error.

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.