473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

For statement

62 New Member
Hi, im making a program that outputs change in terms of 50, 20, 10, 5 cent coins.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int integer_value, coin50, coin20, coin10, coin5, count50, count20, count10, count5; 
  3.  
  4.     printf ("Please enter your integer:\n"); 
  5.     scanf ("%i*c", &integer_value);  
  6.  
  7.     if (( integer_value < 5) || ( integer_value > 95 ) || ( integer_value % 5 != 0))  
  8.     {    
  9.         printf ("Invalid number ");
  10.         getchar (); 
  11.         return 0; 
  12.     }
  13.  
  14.     for (coin50 = 50, count50 = 0; integer_value > coin50; ++count50) 
  15.         integer_value = integer_value - coin50;
  16.  
  17.     for (coin50 = 20, count20 = 0; integer_value > coin20; ++count20) 
  18.         integer_value = integer_value - coin20;
  19.  
  20.     for (coin50 = 10, count10 = 0; integer_value > coin10; ++count10) 
  21.         integer_value = integer_value - coin10;
  22.  
  23.     for (coin50 = 5, count5 = 0; integer_value > coin5; ++count5) 
  24.         integer_value = integer_value - coin5;        
  25.  
  26.     printf ("Customer Change: %i 50cent Coins, %i 20cent Coins, %i 10cent Coins and %i 5cent Coins\n", count50, count20, count10, count5); 
  27.  
However i think theres something wrong with the for statement loops. The program compiles with no errors but when i enter a valid integer the command terminal goes to a new line and if enter is pressed again nothing happens?
I gather the loop is not working correctly, i tired an invalid number e.g 54 and that will be covered by the if clause.
Sep 5 '07 #1
11 1543
doskey
10 New Member
On line 6 i see no reason why you should have the ( integer_value % 5 != 0) whatsoever.
Sep 5 '07 #2
geebanga88
62 New Member
On line 6 i see no reason why you should have the ( integer_value % 5 != 0) whatsoever.
The reason for this is to only allow numbers divisible by 5 e.g. 5, 10 15 20 25, basically for currency purposes. As 1,2,3,4 cent coins arent used the lowest denominator is the 5 cent coin thus the number has to be a divisible by 5. E.g i dont 23 cents or 71 cents being entered.
Sep 5 '07 #3
kreagan
153 New Member
Hi, im making a program that outputs change in terms of 50, 20, 10, 5 cent coins.

However i think theres something wrong with the for statement loops. The program compiles with no errors but when i enter a valid integer the command terminal goes to a new line and if enter is pressed again nothing happens?
I gather the loop is not working correctly, i tired an invalid number e.g 54 and that will be covered by the if clause.
You need to set values to coin20, coin 10, and coin 5. They are uninitialized.
Sep 5 '07 #4
chansoli
1 New Member
every loop starts with (coin50,
other coin* are not initised
Sep 5 '07 #5
geebanga88
62 New Member
Ops yes bad mistake, i got the program to work expect i changed the loop experssion a bit so that the value can be equal to in order for the program statement to be executed.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  for (coin50 = 50, count50 = 0; integer_value => coin50; ++count50) 
  3.         integer_value = integer_value - coin50;
  4.  
  5.     for (coin20 = 20, count20 = 0; integer_value => coin20; ++count20) 
  6.         integer_value = integer_value - coin20;
  7.  
  8.     for (coin10 = 10, count10 = 0; integer_value => coin10; ++count10) 
  9.         integer_value = integer_value - coin10;
  10.  
  11.     for (coin5 = 5, count5 = 0; integer_value => coin5; ++count5) 
  12.         integer_value = integer_value - coin5; 
  13.  
  14.  
I get the error:
error: expected primary-expression before '>' token
for each of the for statment. Anyone know away around this error?
Sep 5 '07 #6
kreagan
153 New Member
Ops yes bad mistake, i got the program to work expect i changed the loop experssion a bit so that the value can be equal to in order for the program statement to be executed.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     for (coin10 = 10, count10 = 0; integer_value => coin10; ++count10) 
  4.         integer_value = integer_value - coin10;
  5.  
  6.  
I get the error:
error: expected primary-expression before '>' token
for each of the for statment. Anyone know away around this error?
use '>=' instead of '=>'. It's "greater than or equal to", not "equal to or greater than"!
Sep 5 '07 #7
geebanga88
62 New Member
ahhhhhh i see thanks :)
Sep 6 '07 #8
geebanga88
62 New Member
Hi again, I have added dollars, to the program to make things more complex. I got the program to compile ok in Dev C++. Invalid dollars and cents will generate the appropriate response however my initial problem has occurred again however this time im pretty sure that it is not variable initialization.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int dollar, cents, coin50, coin20, coin10, coin5, ccount50, ccount20, ccount10, ccount5; 
  3.     int dollar100, dollar50, dollar20, dollar10, dollar5, dollar2, dollar1, dcount100, dcount50, dcount20, dcount10, dcount5, dcount2, dcount1;   
  4.     float amount;
  5.  
  6.     printf ("Please enter your amount:\n"); 
  7.     scanf ("%f", &amount);  
  8.  
  9.     dollar = (int) amount;
  10.     cents = (int) (((amount - dollar)*100) + 0.5);
  11.  
  12.     if ( dollar < 1 )
  13.     {    
  14.         printf ("Invalid dollar amount");
  15.         getchar ();
  16.         getchar (); 
  17.         return 0; 
  18.     }
  19.     if ( cents % 5 != 0) 
  20.     {    
  21.         printf ("Invalid cents amount");
  22.         getchar ();
  23.         getchar (); 
  24.         return 0; 
  25.     }
  26.  
  27.     for (dollar100 = 100, dcount100 = 0; amount >=  dollar100; ++dcount100) 
  28.         amount = amount - dollar100;
  29.  
  30.     for (dollar100 = 50, dcount50 = 0; amount >=  dollar50; ++dcount50) 
  31.         amount = amount - dollar50;
  32.  
  33.     for (dollar20 = 20, dcount20 = 0; amount >=  dollar20; ++dcount20) 
  34.         amount = amount - dollar20;    
  35.  
  36.     for (dollar10 = 10, dcount10 = 0; amount >=  dollar10; ++dcount10) 
  37.         amount = amount - dollar10; 
  38.  
  39.     for (dollar5 = 5, dcount5 = 0; amount >=  dollar5; ++dcount5) 
  40.         amount = amount - dollar5; 
  41.  
  42.     for (dollar2 = 2, dcount2 = 0; amount >=  dollar2; ++dcount2) 
  43.         amount = amount - dollar2;
  44.  
  45.     for (dollar1 = 1, dcount1 = 0; amount >=  dollar1; ++dcount1) 
  46.         amount = amount - dollar1;
  47.  
  48.     for (coin50 = 50, ccount50 = 0; amount >=  coin50; ++ccount50) 
  49.         amount = amount - coin50;
  50.  
  51.     for (coin20 = 20, ccount20 = 0; amount >= coin20; ++ccount20) 
  52.         amount = amount - coin20;
  53.  
  54.     for (coin10 = 10, ccount10 = 0; amount >=  coin10; ++ccount10) 
  55.         amount = amount - coin10;
  56.  
  57.     for (coin5 = 5, ccount5 = 0; amount >=  coin5; ++ccount5) 
  58.         amount = amount - coin5;        
  59.  
  60.     printf ("Customer Change:\n %i 100 dollar note/s\n %i 50 dollar note/s\n %i 20 dollar note/s %i 10 dollar note/s, %i 5 dollar note/s\n %i 2 dollar coin/s\n %i 1 coin/s\n",dcount100, dcount50, dcount20, dcount10, dcount5, dcount2, dcount1); 
  61.  
  62.     printf ("Customer Change:\n %i 50 cent coin/s\n %i 20 cent coin/s\n %i 10 cent coin/s\n %i 5 cent coin/s\n", ccount50, ccount20, ccount10, ccount5); 
  63.  
I also compiled i it in miracle c and comes up wtih the error:
line 37: unrecognised types in comparison
'for (dollar100 = 100, dcount100 = 0' ?
Sep 6 '07 #9
kreagan
153 New Member
Hi again, I have added dollars, to the program to make things more complex. I got the program to compile ok in Dev C++. Invalid dollars and cents will generate the appropriate response however my initial problem has occurred again however this time im pretty sure that it is not variable initialization.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int dollar, cents, coin50, coin20, coin10, coin5, ccount50, ccount20, ccount10, ccount5; 
  3.     int dollar100, dollar50, dollar20, dollar10, dollar5, dollar2, dollar1, dcount100, dcount50, dcount20, dcount10, dcount5, dcount2, dcount1;   
  4.     float amount;
  5.  
  6.     for (dollar100 = 100, dcount100 = 0; amount >=  dollar100; ++dcount100) 
  7.         amount = amount - dollar100;
  8.  
  9.     for (dollar100 = 50, dcount50 = 0; amount >=  dollar50; ++dcount50) 
  10.         amount = amount - dollar50;
  11.  
  12.  

I also compiled i it in miracle c and comes up wtih the error:
line 37: unrecognised types in comparison
'for (dollar100 = 100, dcount100 = 0' ?
I could compile it. The only problem I had was the 2nd for loop. dollar50 doesn't contain a value due to a typo. Instead of for(dollar100 = 50 ... ), it should be dollar50.

As for your compile issue, maybe your compiler cannot compair a float to an int. This is only a guess. I would suggest typcasting.

Expand|Select|Wrap|Line Numbers
  1. for (dollar100 = 100, dcount100 = 0; amount >=  (float)dollar100; ++dcount100) 
  2.         amount = amount - dollar100;
Other than that, I don't know. Seems to be compiler specific.
Sep 6 '07 #10
Ganon11
3,652 Recognized Expert Specialist
This code will not work: Your coin5 variable holds the same value as your dollar5 variable, and so each are being treated as $5. You need to use doubles for your coins to get the proper results.
Sep 6 '07 #11
geebanga88
62 New Member
That one typo fixed it. I can also compile it in dev c++, just not in miracle c. Thanks for spotting that out, i think the code is a poorly design because of to many variables and loops being used which makes it difficult to spot those kinds of errors.
Sep 7 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

28
3537
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
15
2777
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
2519
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
37
3242
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
2681
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
2904
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
2672
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
19
8339
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
18
7924
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
23
2040
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.