Hi, I need help in understanding nested loops. Here's my problem.


I'm asked to write a code that emulates a cash register. Using only while loops and do while loops, and can be with the help of if/else statements.

Desired output :

Welcome to CrappyMart
type (-1) when done to calculate total:


Enter the price of 1st article : xx.xx
Subtotal: $xx.xx
Enter the price of 2nd article: xx.xx
Subtotal: $xx.xx
Enter the price of 3rd article: -xx.xx

OOPS Wrong Value entered.

Enter the price of 3rd article: xx.xx

Enter the price of the 4th article: -1

The total is : $ 50.00
The total number of items purchased is: XX

Enter the payment amount : $40.00
The amount is lower than the total of your purchase

Enter the payment amout: $60.00
Your change is $10.00

Thank you for shopping at CrappyMart.

Here's what I have so far. Can someone guide me into the light of the right direction. :'(
Surprisingly enough the code I wrote compiles but it doesn't gives the desired output =( :@

#include <iostream>

using namespace std;

int main () 

{

double itemPrice, subTotal, payment; 
double change;
int nItems;

//counter
	nItems = 1;
	itemPrice = 0.0;
	subTotal = 0.0;
	payment = 0.0;


	cout << " Enter the price of the purchase " <<endl;
	cout << " Press ( -1 ) to exit and calculate total" <<endl;
	
	do {
		if (itemPrice < 0 )
		{
			cout<< "Not an accepted price" <<endl;
			}
	cout << "Enter the price of article number "<<nItems<<" in dollars $";
	cin >> itemPrice;
		
	}


	while (itemPrice < 0);
	
		cout<<"Enter the price of the item number"<<nItems<<"in dollars $";
		cin>>itemPrice;

		if (itemPrice > 0){
			subTotal = subTotal + itemPrice;
			++nItems;
		}
		else (itemPrice == -1);
			
			cout <<" The total is: $" <<subTotal; 
			cout <<" The number of items is: "<<nItems;

			cout <<" Enter the payment amout $"<<payment;
			cin >> payment;
			do
			{
				if (payment < subTotal)
				{
					cout <<"The amount is lower than the total"<<endl;
				}
				cout<<"Enter the payment amount $"<<endl;
				cin >> payment;
			}
			while (payment < subTotal);

			change = subTotal - payment;

			cout<<"Your change is $"<<change;



cin.get();
return 0;


}

Recommended Answers

All 4 Replies

I'm not sure you need a NESTED loop. Seems to me a regular old do-while loop will suffice. However, be very careful of comparing a double to the EXACT value of -1 using ==. I think -1 is one of those values that a double stores EXACTLY without round-off error so it'll be OK, but in general it can be a bad idea.

do
{
    // prompt user for input
    cin >> itemPrice;
    if (itemPrice <= 0 && itemPrice != -1)
    {
        // error message.
    }
    else if (itemPrice > 0)
    {
         // adjust subtotal, number of items, etc.
    }
}
while (itemPrice != -1);

// now on to the payment part of the problem

Thank you very much for the reply. I took in consideration some of your ideas and this is what I have now:

I ran into two problems :
1.) if the user entered a negative item price it will loop infinitely on the error message.

2.) at the end of the program when the payment value is asked it loops properly to input the payment value. But how do I add a cout statement to that loop to warn the user about the wrong amount entered.

//revised version. 
#include <iostream>

using namespace std;

int main ()

{

double itemPrice, subTotal, payment;
double change;
int nItems;

//counter
	nItems = 1;
	itemPrice = 0.0;
	subTotal = 0.0;
	payment = 0.0;


	cout << " Enter the price of the purchase " <<endl<<endl<<endl;
	cout << " Press ( -1 ) to exit and calculate total" <<endl<<endl;
   
	do {
		while (itemPrice < 0 )  //Changed to while loop. 
		{
			cout<< "Not an accepted price" <<endl; 
			}
	cout << "Enter the price of article number "<<nItems<<" in dollars $";
	cin >> itemPrice;
		
	//calculations go here
		if (itemPrice > 0){
			subTotal = subTotal + itemPrice;
			++nItems;
			cout<<"\t \t \t \t \t \t Your SubTotal is: "<<subTotal<<endl;
		}



	}	while (itemPrice != -1.0);  //changed the condition to != -1.0 

		
		


		   
			cout <<" The total is: $" <<subTotal<<endl;
			cout <<" The number of items is: "<<nItems<<endl;


			do
			{
				if (payment > subTotal)
				{
					cout <<"The amount is lower than the total"<<endl;
				}
				cout<<"Enter the payment amount $";
				cin >> payment;
			}
			while (payment < subTotal);

			change = payment - subTotal;

			cout<<"Your change is $"<<change<<endl;



system ("pause");  
return 0;


}

You still have a nested loop (a while loop inside a do-while loop). You don't want the error message to be in a loop. A simple if statement is fine for that. You have an infinite loop because you give the user no option to enter in a good price after failing the first time. You just have a cout statement inside which is an infinite loop because the condition never changes.

One loop. At the top of the loop, ask for input. Then check the input. If it's illegal input, give an error message. If it's good input and not -1, add that item's price. If it's -1, do nothing. One loop. There is no need for a nested loop, and there is no need to ask for input BEFORE the loop (even if you turn it from a do-while loop to a while loop - just make sure it ENTERS the loop).

do
{
    // prompt user for input
    cin >> itemPrice;
    if (itemPrice <= 0 && itemPrice != -1)
    {
        // error message.
    }
    else if (itemPrice > 0)
    {
         // adjust subtotal, number of items, etc.
    }
}
while (itemPrice != -1);

As to part 2 of your question, look carefully at lines 55 to 58 that you posted. You already have it handled in an if statement, but look at that if statement and see whether what is displayed matches the if-condition. And look at lines 59 - 60. Should you test the input before you even read in the input?

start quote:

do
{
    // prompt user for input
    cin >> itemPrice;

    if (itemPrice <= 0 && itemPrice != -1)
    {
        // error message.
    }
    else if (itemPrice > 0)
    {
         // adjust subtotal, number of items, etc.
    }
}
while (itemPrice != -1);

end quote.

I took out the while statement you were referring to but took a slightly dfferent approach because I'm still have difficulties understanding the compound logical statements if (itemPrice <=0 && itemPrice != -1). But so far My code runs smoothly and does everything it is supposed to perform.

This is my final version :

//solved version woohoo
#include <iostream>

using namespace std;

int main ()

{

double itemPrice, subTotal, payment;
double change;
int nItems;

//counter
    nItems = 1;
    itemPrice = 0.0;
    subTotal = 0.0;
    payment = 0.0;


    cout << " Enter the price of the purchase " <<endl<<endl<<endl;
    cout << " Press ( -1 ) to exit and calculate total" <<endl<<endl;

    do {
        //while (itemPrice < 0 )  //Changed the while loop to if itemPrice <0 and  it works wooohooo it did solve the loop
        if (itemPrice < 0) 
        {
            cout<< "Not an accepted price" <<endl; 
            }
    cout << "Enter the price of article number "<<nItems<<" in dollars $";
    cin >> itemPrice;

    //calculations go here
        if (itemPrice > 0){
            subTotal = subTotal + itemPrice;
            ++nItems;
            cout<<"\t \t \t \t \t \t Your SubTotal is: "<<subTotal<<endl;
        }



    }   while (itemPrice != -1.0);  //changed the condition to != -1.0 while not -1.0 it will keep looping n times until stopped with -1.00



            cout <<" The total is: $" <<subTotal<<endl;
            cout <<" The number of items is: "<<nItems - 1 <<endl;


            do
            {
                cout<<"Enter the payment amount $"; // moving this cout cin from below the if to above the if solved the loop
                cin >> payment;
                //if (payment > subTotal)
                    //changed to 
                if (payment < subTotal)  // when I changed this it payment < subTotal and the the cin was in place it worked ok
                {
                    cout <<"The amount is lower than the total"<<endl;
                }

                //This is were the original cout cin statements were
                //cout<<"Enter the payment amount $";
                //cin >> payment;
                //to this
            }
            while (payment < subTotal);

            change = payment - subTotal;

            cout<<"Your change is $"<<change<<endl<<endl;

            cout<<"Thank you for your purchase, please come again"<<endl<<endl;


            system ("pause");
return 0;

}

Thank you very much for your guidance.

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.