GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Mar-2004, 21:38
ewallo ewallo is offline
New Member
 
Join Date: Feb 2004
Posts: 4
ewallo is on a distinguished road

Repetition Structure


I am a beginner with C++ that is in need of some advice. I am writing a program for my class that just work work. The program us intended to have have the following output:
Amount Owed:
Amount Received:
Change:
Fifty:
Twenty:
Ten:
Five:
Dollar
quarter:
Dime:
Nickel:
Penny:

We need the program to run again without exiting in order to proceed with the next customer. Here is some of the code I have in the function:

CPP / C++ / C Code:
//*****program-defined functions*****
float getInput(float &owed, float &rec, float &amount)

{
	int amount = 0;
	int next = 0;
	int order = 0;
	int update = 0;
	
	//input items
	cout << "Enter amount owed:";
	cin >> owed;
	while (owed >=0)
	{
	
	amount = amount + 1;
	cout << "Enter amount received:";
	cin >> rec;
	next order ++;

	//update counter
	counter --;
	
	
	if (rec > owed)
		return rec - owed;
	
	
	cout << "Enter the next amount owed:";
	cin >> owed;
	cout << "Enter the next amount received:";
	cin >> rec;

return 0;
}

    //more calculations
int calcChange(int &chg, float denom)
{
	int val;
	//int den = denom * 1000;
	val = chg / den;
	chg = chg % den;
	//return val;

return 0;
}

Is this even close or I am way off. I just need to be put in the right direction. Any help will be greatly appreciaed.

Eric
Sorry for the long thread..
Last edited by dsmith : 23-Mar-2004 at 08:53. Reason: Updated syntax highlighting tag
  #2  
Old 22-Mar-2004, 22:12
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Aww... you were so close to the syntax hilighting! Your closing tag needs to look like this!: [/c]

As far as the code, it's close. Where's the ending brace on your while loop? Also, when you use a return statement with operators, you should enclose that statement in parenthesis like so:
CPP / C++ / C Code:
return (rec - owed);
__________________
-Aaron
  #3  
Old 23-Mar-2004, 02:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Ahhh, the change program. Next time try the Preview button to see if your post is correct.

Quote:
Originally Posted by ewallo
Is this even close or I am way off. I just need to be put in the right direction. Any help will be greatly appreciaed.
What does it do when you compile it? Run it?

If you feel you need help, ask a specific question so we know what you're having trouble with.
__________________

Age is unimportant -- except in cheese
  #4  
Old 23-Mar-2004, 09:29
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
i fail to understand the logic behind both of your functions. Maybe you should rethink the logic behind them. Spotted an error:
the line after
CPP / C++ / C Code:
cin>>rec;  next order++;
what is next supposed to be doing?
also where have u declared the counter? and why are u decrementing it? and why is amount being incremented by 1?

you should first try to do this on a piece of paper. work out the proper logic in your own words: for example:

i will first get the amount owed.

then i will run a while loop till the amount that is owed is satisfied.
(while inside the loop)
if i receive amount less than what is owed, i will credit that to the amount owed
and keep asking the user for the remaining amount till i get it all
if i recieve more than what is owed i will credit what is owed and return the rest
while loop ends

it's called pseudocode if im not mistaken

make it a habit to spend more time with ur pen and paper than with ur compiler.
You will save lots of frustration and time.
Many programmers especially the beginners only think a few minutes about the problem and then get right to programming thinking they can figure it out on the fly and just keep on compiling and fixing errors till there are no errors anymore.

concentrate on the general big picture first. Then divide your big picture into smaller parts or functions. Concentrate on smaller functions one by one. and then make changes after you are done to make them work with each other.

Quote:
Originally Posted by ewallo
I am a beginner with C++ that is in need of some advice. I am writing a program for my class that just work work. The program us intended to have have the following output:
Amount Owed:
Amount Received:
Change:
Fifty:
Twenty:
Ten:
Five:
Dollar
quarter:
Dime:
Nickel:
Penny:

We need the program to run again without exiting in order to proceed with the next customer. Here is some of the code I have in the function:

CPP / C++ / C Code:
//*****program-defined functions*****
float getInput(float &owed, float &rec, float &amount)

{
	int amount = 0;
	int next = 0;
	int order = 0;
	int update = 0;
	
	//input items
	cout << "Enter amount owed:";
	cin >> owed;
	while (owed >=0)
	{
	
	amount = amount + 1;
	cout << "Enter amount received:";
	cin >> rec;
	next order ++;

	//update counter
	counter --;
	
	
	if (rec > owed)
		return rec - owed;
	
	
	cout << "Enter the next amount owed:";
	cin >> owed;
	cout << "Enter the next amount received:";
	cin >> rec;

return 0;
}

    //more calculations
int calcChange(int &chg, float denom)
{
	int val;
	//int den = denom * 1000;
	val = chg / den;
	chg = chg % den;
	//return val;

return 0;
}

Is this even close or I am way off. I just need to be put in the right direction. Any help will be greatly appreciaed.

Eric
Sorry for the long thread..
  #5  
Old 25-Mar-2004, 17:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by ewallo
CPP / C++ / C Code:

    //more calculations
int calcChange(int &chg, float denom)
{
	int val;
	//int den = denom * 1000;
	val = chg / den;
	chg = chg % den;
	//return val;

return 0;
}

Is this even close or I am way off. I just need to be put in the right direction. Any help will be greatly appreciaed.

Eric
Sorry for the long thread..

Well, Eric, It's hard to say how close you are; but I think I see what you have in mind, and it's possible that you are not too far off.

The idea of using / and % to get a number and its remainder can be applied to your problem, but they work with ints. Here's a suggestion: why not convert all calculations to pennies (so you are always working with integers internally)?

Does this make sense to you? (I've done it many times, so it makes sense to me, but ...)


Dave
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
my compiler says I have to have a variable at the end of my structure ambeco C Programming Language 10 24-Feb-2004 10:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.