New to C++ Help Change from Purchase/Date Class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2126 Views - Last Post: 03 September 2013 - 09:05 PM Rate Topic: -----

#16 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 04:25 PM

Well I tried the while loop around 13 - 57 and no luck in fact it wouldn't even compile at that point can you be more specific as to what I am trying to accomplish using the while loop?
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 05:34 PM

Show us your new code with the while loop and tell us the exact error you are getting.
Was This Post Helpful? 0
  • +
  • -

#18 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 07:41 PM

Ok so I trashed my original (thought I had saved it and apparently overwrote the file ://>) code. So I started over. This is what I now have.


#include <iostream>  

using namespace std;  

int main() // start int main
{   

int costc;
int costc2;
int costc3; 
int costd; 
int costd2;      
int costd3;
costc3 = 1;
costd3 = 1;

cout << "Hello! The sunglasses you would like to purchase are $10.20" << endl;
cout << "So how much money do you have to pay for these with today?" << endl;

while (costd3 = 1, costc3 = 1) // start while loop
{	cout << "Dollars: ";
	cin >> costd;
	costd3 = 2;
	costc3 = 1;
}
while (costd3 = 2, costc3 = 1, costd = 10)
{
	cout << "And how much change will you have today?" << endl;
	cout << "Cents: ";
	cin >> costc;
	
}
while (costd3 = 2, costc3 = 1, costd < 10)
{
	cout << "im sorry " << costd << " is not enough to pay for this item." << endl;
	cout << "do you have a different amount of dollars?" << endl;
	costd3 = 1;
	costc3 = 1;
}
while (costd3 = 2, costc3 = 1, costd > 10)
{
	cout << "Ok so you have " << costd << " dollars to pay for this item." << endl;
	cout << "Please give me a moment while I fetch your change." << endl;
	costd2 = costd - 11;
	costc2 = 100 - 20;
	costd3 - 3;
	costc3 - 1;
}



return 0;

} //end int main



Im about to tear my eyeballs out of my head what am I doing wrong and why is the code just returning to the top? Do I need to nest my while loops or should I be using do and for in this case along with the while? I thought creating a cost3 int would cause the loop to return to the top check over? No errors with this coding just the program does not function correctly and return to the top when the amount of dollars (costd) is below 10 ://> AHHHH! *laughs*
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 07:48 PM

The first issue is that the suggestion in post #14 was to put your code between lines 13-57 in a (singular) while loop. You have created multiple while loops.

The second issue, is that you are not constructing your while loops correctly. If the expression between the parenthesis of a while loop evaluates to non-zero, it will cause the loop to cycle again.

So for example, in your code:
while (costd3 = 1, costc3 = 1)



The expression is costd3 = 1, costc3 = 1. This evaluates to 1. What is happening is that 1 will be assigned to costd3, then the , operator will cause 1 to be assigned to costc3, and finally the same operator will return 1 since 1 was the last value as the result of the assignment.

I don't expect you to have covered the , comma operator, but I assume you should have at least covered flow control and looping including while loops. I'm making that assumption because you mention for loops. If you don't understand while loops, I suggest you make time to talk to your teacher and work with them to help you get caught up.
Was This Post Helpful? 1
  • +
  • -

#20 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 07:53 PM

Ok so I am going to pull my orginial code from here and then add the while loop. But if you can't tell its obvious that I am having a hard time understanding how the loop works. How would I get it to return 0? *laughs* wait return 0;... is this how you get the while loop to return 0 rather than a non zero? Am I overthinking it with adding 2 more ints to try and create some sort of counter?
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 08:18 PM

This is a simple while loop the counts down to 0:
int value = 10;
std::cout << "Starting countdown at T minus " << value << std::endl;
while (value > 0)
{
    std::cout << value << std::endl;
    value--;
}
std::cout << "Blast off!" << std::endl;


Was This Post Helpful? 1
  • +
  • -

#22 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 08:32 PM

I will admit I attended a course for data structures 1 BUT it was years ago... Now I am back taking data structures 2 and I have apparently not retained nearly what I thought I could. I have been hobbling through this class and my teacher has not responded to any of my emails about needing help (its an online course only). I have been searching for an example of what I am trying to do here for the last 2 days on the internet and finally ended up here because I could tell the people on here are extremely knowledgeable.

Let me give you the assignment question with the understanding I DO NOT expect you to do this for me but I would like to know if I am going about this in a completely wrong way and if there is something better I should be doing.

The price of an item you want to buy is given in dollars and cents. You pay for it in cash by giving the clerk d dollars and c cents. Write specifications for a function that computes the change, if any, that you should receive. Include a statement of purpose, the preconditions and postconditions, and a description of the arguments.

Our current chapter is on ADT...

#include <iostream>    
  
using namespace std;    
    
int main()    
{     

int costc;  
int costc2;    
int costd;   
int costd2;        


while () //start of while loop
{  
cout << "The price of this item you would like to buy is $10.20" << endl;  
cout << "How much money do you have in dollars and cents?" << endl;  
cout << "Dollars: $";  
cin >> costd;  

	if (costd < 10)  
	{  
	cout << "I'm sorry this is not enough money for this item." << endl;  
	cout << "A different amount of dollars perhaps?";  
	cin >> costd;  
	}  
    
	else if (costd > 10)  
	{  
	costd2 = costd - 10;  
	costc2 = 100 - 20;  
	cout << "Your change is "<< costd2 << " dollars and "<< costc2 << " cents." << endl;  
	cout << "Thank you for your purchase." << endl;  
	}  

	else if (costd == 10)  
	{  
	cout << "Any cents?: ";  
	cin >> costc;  

		if (costc > 20)  //nested if loop
		{  
			costc2 = costc - 20;  
			costd2 = 0;  
			cout << "Your change is " << costd2 << " dollars and " << costc2 << " cents." << endl;  
			cout << "Thank you for your purchase today." << endl;  
		}  

		else if (costc < 20)  
		{  
		cout << "I'm sorry this is not enough money for this item." << endl;  
		cout << "A different amount of cents perhaps?" << endl;  
		cin >> costc;  
		}  

		else (costc == 20);  
		{  
			cout << "Thank you for your purchase. You have no change today." << endl;  
		}
	}
} //end of while loop
    
system("PAUSE");  
return 0;  
}



I have reworked my scope brackets so Its correct I believe and easier to read. I don't know where to start as far as everything but I can't help but thinking if he asked for only c (cents) and d (dollars) the only int I would need to initialize in the main before the loop would be int c and int d. I need to pass this class or I will actually lose my place im living I am a vet working off my GI bill... a failed class is... well its less pay from my GI bill which is crap. I am doing what I can right now. I am already 3 weeks behind in class because of this.

Let me also say I appreciate the patience and help everyone has given so far, specially Skydiver and JackOfAllTrades.
Was This Post Helpful? 0
  • +
  • -

#23 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 10:34 PM

Quote

Write specifications for a function that computes the change, if any, that you should receive. Include a statement of purpose, the preconditions and postconditions, and a description of the arguments.


This means that you don't need to write any code to implement anything. All you need to do is write the expected behavior of such code.

For example, here are the specifications of a function that computes the square root of a floating point number:

// Function: ComputeSquareRoot
//
// Summary:
//     Computes the square root of a positive floating point number.
//
// Parameters:
//     radicand
//         Type: double
//         Description: The number whose square root is to be computed.
//
// Details:
//     Computes the square root of a positive floating number by using the Babylonian Method.
//
//     Performs simple parameter checks for parameters which are zero or negative.
//
// Preconditions:
//    The radicand should be a positive floating point number.
//
// Postconditions:
//    If radicand is equal to zero, the function returns zero.
//    If the radicand is positive, then the value returned when squared should approximately
//         be equal to the original radicand passed in.
//    If the radicand is negative, the function returns a negative value to indicate an error
//         condition.

double ComputeSquareRoot(double radicand);


Was This Post Helpful? 0
  • +
  • -

#24 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 11:11 PM

so are you really telling me this whole time I shouldn't have been writing any code like I thought earlier? haha. Oh man... ok well. That's not really any easier I better get started :).
Was This Post Helpful? 0
  • +
  • -

#25 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 03 September 2013 - 11:47 AM

So I have come up with something like this using your explanation. Would you mind looking it over and see if I have the hang of it?

FUNCTION:
FindDollarsCents

SUMMARY:
Computes two integers dollars and cents and finds the change, if there is any, for the user.

PARAMETERS:
c
Type: int
Description: The number of cents to be computed to find change.

d
Type: int
Description: The number of dollars to be computed to find change.

DETAIL:
Computes the change of two positive integers by comparing them to the price of an item.
Performs checks that the ints are greater than or equal to the price of item.

PRECONDITION:
The ints c and d should be positive numbers and work with the computation of

POSTCONDITION:
If the ints are greater than cost of item , function returns the change.
If the ints are the same as the cost of the item, function returns no change.
If the ints are less than the cost of the item, function returns to beginning to accept new amount.

int FindDollarsCents(int c, int d)
Was This Post Helpful? 0
  • +
  • -

#26 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 03 September 2013 - 11:58 AM

Quote

PRECONDITION:
The ints c and d should be positive numbers and work with the computation of


Change to:

PRECONDITION:
The ints c and d should be positive numbers that can be compared to the price of the item.
Was This Post Helpful? 0
  • +
  • -

#27 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 03 September 2013 - 06:09 PM

Unfortunately, your function currently only returns a single integer. Is that integer in dollars or cents or some combination of both? What would be cool is if you had a class that kept the dollars and cents together. That way you can pass one instance of that class as the input to the function, and the function can return another instance that contains the change that should be returned.

As for this post condition:

Quote

If the ints are less than the cost of the item, function returns to beginning to accept new amount.


How will the function be able to do that? The original user input was passed in which implies that the UI code is outside of the function. How will the function go back to the UI code?
Was This Post Helpful? 0
  • +
  • -

#28 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 03 September 2013 - 07:35 PM

View PostSkydiver, on 03 September 2013 - 06:09 PM, said:

Unfortunately, your function currently only returns a single integer. Is that integer in dollars or cents or some combination of both? What would be cool is if you had a class that kept the dollars and cents together. That way you can pass one instance of that class as the input to the function, and the function can return another instance that contains the change that should be returned.

As for this post condition:

Quote

If the ints are less than the cost of the item, function returns to beginning to accept new amount.


How will the function be able to do that? The original user input was passed in which implies that the UI code is outside of the function. How will the function go back to the UI code?



When it checks it runs a loop to return to the top unless it returns true and then it breaks. I don't necessarily think I have to go into that much detail as the next part of the problem is to create the class to do this. I thought that FindDollarsCents(int c, int d) would allow int c and int d to be returned since the function is not set to return void? Or am I misunderstanding and that int c and d outside of the scope of the function are not the same as int c and d in the scope of main?
Was This Post Helpful? 0
  • +
  • -

#29 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,488
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 03 September 2013 - 07:46 PM

No, you aren't missing anything with regards to the parameters to the function. I was just starting to come up with a more consistent API since a function can only return one thing, but you need to return two things. The int that you had the front of the function name indicates that you are returning an in.

If your function returns true to cause the loop to return to the top, then that means your function can't return the amount for the change, unless you setup a special protocol. Something like if the function returns -1, then that is an indicator that the loop should return to the top. If that is the case, then your post condition should say so.
Was This Post Helpful? 0
  • +
  • -

#30 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 03 September 2013 - 07:57 PM

Ok so let me ask this question then would it be easier and better coding that runs before this function to check the input of the user against the item to make sure it is equal to or more than the cost of the item which then will stop the loop and move to the function to find change?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3