4 Replies - 721 Views - Last Post: 15 April 2015 - 12:54 PM Rate Topic: -----

#1 pmendez   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 25-March 15

Im sure I am coding this loop wrong :/

Posted 15 April 2015 - 11:32 AM

I was asked to create a loop that outputs until Quit is entered for customer name. This is what my output is suppose to look like:

I will also attach the code I made. Any suggestions. Im sure there is something wrong with my while command. But I believe there is something else I have to add to print out the last customer name: quit output. I am stumped at the moment.



Lab 28 – Your Name

Input Customer Name: xxxxxxxxxx
Input Customer State: xx
Input Product Price: xxx.xx

Customer Name: xxxxxxxxxxx
Customer State: xx
Product Price: xxx.xx
Product Discount: xxx.xx
Discounted Price: xxx.xx
Sales Tax: xx.xx
Total Cost: xxx.xx

Input Customer Name: Quit

End of Lab 28 for Your Name

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

double pPrice , pDiscount , dPrice , sTax , tCost ;
int cName , cState ;

int main()

{
	cout << "Lab 28 - Pedro Mendez"  ;
	cout << endl ;
	cout << endl ;
	cout << "Input Customer Name:          "             ;
	cin >> cName                                         ; 
	cout << "Input Customer State:         "			 ;
	cin >> cState									     ;
	cout << "Input Product Price:		   "			 ;
	cin >> pPrice										 ;
	cout << endl ;
	cout << endl ;
	
	// begin loop

	while (cName != "Quit")
		if (pPrice > 50)
			pDiscount = pPrice *0.15 ;
		else pDiscount = 0 ;
		dPrice = pPrice - pDiscount ;
		sTax = dPrice * 0.07       ;
		tCost = dPrice - sTax       ; 
		
		
	// begin outputting
	
	cout << "Customer Name:                " << cName ;
	cout << endl ;
	cout << "Customer State:               " << cState ;
	cout << endl ;
	cout << "Product Price:                " << pPrice ;
	cout << endl ;
	cout << "Discounted Price:             " << pDiscount ;
	cout << endl ;
	cout << "Sales Tax:                    " << sTax ;
	cout << endl ;
	cout << "Total Cost:                   " << tCost ; 
	cout << endl ;
	cout << endl ;
}
	



Is This A Good Question/Topic? 0
  • +

Replies To: Im sure I am coding this loop wrong :/

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Im sure I am coding this loop wrong :/

Posted 15 April 2015 - 11:56 AM

While loops need curly braces if their bodies are more than one line. Same with things like if statements. You should always get in the habit of putting braces even if you have one line. It does two things, avoids errors like this and also makes it easier for future coders to simply add in another line to a loop without having to worry about adding the braces themselves. Plus they help with intent. Without them we don't know what you intended to have under the control of the while loop and not. So put in the curly braces on the while loop and if statement. That is part of your problem btw because the lines like dPrice =, sTax = and tCost are not seen as part of the loop.

Last point I want to make is that when you use a while loop, you have to make sure that its condition will eventually be false by changing something in the body. So in your while loop you have to somehow make cName equal quit so that the while loop will eventually exit. Otherwise you get an infinite loop. :)

This post has been edited by Martyr2: 15 April 2015 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 pmendez   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 25-March 15

Re: Im sure I am coding this loop wrong :/

Posted 15 April 2015 - 12:42 PM

Thank you. I did this and also added the new if statement to end the loop but I am having problems with the input Quit. Why? I have tried a few different things and cannot get it. I am not saying there is nothing else wrong with the rest of the code, which there probably is, but what?

I have never coded a loop and I am really lost.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

double pPrice , pDiscount , dPrice , sTax , tCost ;
int cName , cState ;

int main()

{
	cout << "Lab 28 - Pedro Mendez"  ;
	cout << endl ;
	cout << endl ;
	cout << "Input Customer Name:          "             ;
	cin >> cName                                         ; 
	cout << "Input Customer State:         "			 ;
	cin >> cState									     ;
	cout << "Input Product Price:		   "			 ;
	cin >> pPrice										 ;
	cout << endl ;
	cout << endl ;
	
	// begin loop

	{	while (cName != "Quit") 
			if (pPrice > 50)
				pDiscount = pPrice *0.15 ;
			else pDiscount = 0 ;
		dPrice = pPrice - pDiscount ;
		sTax = dPrice * 0.07       ;
		tCost = dPrice - sTax       ; 
		
		
	// begin outputting
	
	cout << "Customer Name:                " << cName ;
	cout << endl ;
	cout << "Customer State:               " << cState ;
	cout << endl ;
	cout << "Product Price:                " << pPrice ;
	cout << endl ;
	cout << "Discounted Price:             " << pDiscount ;
	cout << endl ;
	cout << "Sales Tax:                    " << sTax ;
	cout << endl ;
	cout << "Total Cost:                   " << tCost ; 
	cout << endl ;
	cout << endl ;
	if (cName = "Quit")
		return 0;
	}	
	cout << endl ;
	cout << endl  ;
	cout << "End of Lab for Pedro Mendez" ;
	
}



Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Im sure I am coding this loop wrong :/

Posted 15 April 2015 - 12:46 PM

Yeah you put curly braces in the completely wrong spots....

while (condition) {
  // Body of loop
}



If statements are like...

if (condition) {
  // Body if true
}
else {
  // Body if false
}



:)

P.S. An if statement that has a condition using just the assignment operator (a single equal sign) it will always be true. You need to use double equal signs to test inequality equals. But also you defined cName to be of type "int" (an integer) so why are you trying to compare it to a string? For instance how does 1 == "Quit"? Doesn't make much sense does it.

This post has been edited by Martyr2: 15 April 2015 - 12:50 PM

Was This Post Helpful? 1
  • +
  • -

#5 pmendez   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 25-March 15

Re: Im sure I am coding this loop wrong :/

Posted 15 April 2015 - 12:54 PM

Okay will fix that right away.
How do you end while loops.

Is that right?

if (cName == "Quit"
return 0 ;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1