473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Question why this is happening in a multi file program

I am writing a multifile program and it worked when it was all in one file. I have gotten all of the errors out of the program except when I go to output the change the quarters are right but the dimes and pennies print out a long string of numbers. I can't figure out were these numbers are coming from. If anyone can help I will be appreciated.

This is my main.cpp code for the body of my program.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include"vendlib.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. char another;
  9.  
  10. cout << "Welcome to online vendiing machine." << endl;
  11. cout << "Product list: 1. M&M($0.65) 2. Chips ($1.16) 3. Pepermint gum ($0.28)."<< endl;
  12.  
  13. cout << "would you like to make a purchase?(y/n)" << endl;
  14. cin >> another;
  15. int dollars,cents;
  16.  
  17. while (another =='y')
  18. {
  19. cout << "Enter amount of money deposited in dollars first then cents:";
  20. cout << "dollars";
  21. cin >> dollars;
  22. cout << "cents";
  23. cin >> cents;
  24.  
  25.  
  26. int product_number;
  27. cout << "Enter Product number.";
  28. cin >> product_number;
  29.  
  30. int a,b,c,d;
  31. a = product_price(product_number);
  32. b = dollars_cents(dollars);
  33. c = total_deposit(b,cents);
  34.  
  35. d = total_cents(c,a);
  36. cout << d << endl;
  37.  
  38. int count=1;
  39. while (count <= 3)
  40. {
  41. int a,b,c,C1,C2;
  42.  
  43. a = quarters(d);
  44. count++;
  45. b = dimes(d,C1)
  46. count++;
  47. c = pennies(d,C1,C2);
  48. count++;
  49.  
  50. cout << "Your change is";
  51. cout << a  << "quarters " <<" " <<  b << "dimes " <<" " <<"and "<< c <<"pennies" <<  endl;
  52.  }cout << "would you like another purchase. (y/n)" << endl;
  53. cin >> another;
  54. }
  55.  
  56. return 0;
  57. }
  58.  
This is my code to my vendlib.cpp file

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include "vendlib.h"
  4.  
  5. int a,b,d,e,f,tc,C1,C2,C3;
  6.  
  7. //assigns the value of puchase to selection.
  8. int product_price(int product_number)
  9. {
  10.  
  11.         if(product_number == 1)
  12.         {
  13.                  a = 65;
  14.         }
  15.  
  16.         if(product_number == 2)
  17.         {
  18.                 a = 116;
  19.         }
  20.  
  21.         if(product_number == 3)
  22.         {
  23.                 a = 28;
  24.         }
  25.  
  26.         return a;
  27. }
  28. // takes input from user in dollars and converts it to cents.
  29. int dollars_cents(int dollars)
  30. {
  31.         int b = (dollars * 100);
  32.  
  33.         return b;
  34. }
  35.  
  36.  
  37. //takes both converted dollars and cents and puts them in one variable
  38. int total_deposit(int dollars_cents, int cents)
  39. {
  40.         int c = dollars_cents + cents;
  41.  
  42.  
  43.  
  44.         return c;
  45. }
  46.  
  47. // total_cents is the the amount of money left after there purchase.
  48. int total_cents(int total_deposit, int product_price)
  49. {
  50.         tc =(total_deposit - product_price);
  51.  
  52.         return tc;
  53.  
  54. }
  55.  
  56.  
  57. //quarters will take the amount after purchase and give how many quarters
  58. //are given back.
  59. int quarters(int total_cents)
  60. {
  61.         d = (total_cents/25);
  62.         if (d > 0)
  63.                 C1 = (d);
  64.         if (d == 0)
  65.                 C1 = 0;
  66.         return d;
  67. }
  68.  
  69. //dimes wil take the amount left after quarters and give how many dimes
  70. //are given back.
  71. int dimes(int total_cents,int C1)
  72. {
  73.         e  = ((total_cents - C1)/10);
  74.         if (e > 0)
  75.                 C2 = (e*10);
  76.         if (b == 0)
  77.                 C2 = 0;
  78.         return e;
  79. }
  80.  
  81. //penny will take anything that is left after quarters and dimes, and
  82. //assign it to pennies.
  83. int pennies(int total_cents,int C1,int C2)
  84. {
  85.         f = ((total_cents - C1 - C2)/1);
  86.         if (f > 0)
  87.                 C3 = f;
  88.         if (f == 0)
  89.                 C3 = 0;
  90.  
  91.         return f;
  92. }
  93.  
  94.  

this is a sample of the output when the program is run.

[PHP]Welcome to online vendiing machine.
Product list: 1. M&M($0.65) 2. Chips ($1.16) 3. Pepermint gum ($0.28).
would you like to make a purchase?(y/n)
y
Enter amount of money deposited in dollars first then cents:dollars2
cents50
Enter Product number.1
185
134521512
134516300
134516477
Your change is7quarters -13452132dimes and -269037627pennies
would you like another purchase. (y/n)
n[/PHP]
Oct 24 '06 #1
1 1514
Banfa
9,065 Expert Mod 8TB
In main

Expand|Select|Wrap|Line Numbers
  1. int a,b,c,C1,C2;
  2.  
  3. a = quarters(d);
  4. count++;
  5. b = dimes(d,C1)
  6. count++;
  7. c = pennies(d,C1,C2);
  8. count++;
  9.  
You use C1 and C2 without initialising them to anything

in vendlib.cpp
Expand|Select|Wrap|Line Numbers
  1. int a,b,d,e,f,tc,C1,C2,C3; 
Having global variables like this is very very poor style (especially given that they don't have meaningful names).

Remove this line and fix compile errors by creating local variables in the functions themselves. You will then find that you also have some unreferenced variables (an unreferenced variable is 1 that is declared and may be assign to but the value of it is never used), you compiler may or may not warn you about these so check the code for variables whos values are not used.

Looking at this program there is no need to it to have any global data.
Oct 24 '06 #2

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

Similar topics

43
by: nospam | last post by:
I got three (3) files (1) Untitled.aspx (2) Untitled.aspx.1.cs (3) Untitled.aspx.2.cs These three files must be used together to make file #1, Untitled.aspx, page work via J.I.T. when the...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
12
by: Corey Burnett | last post by:
I have a client that has a split database (front-end/back-end). They are also using Access security - MDW file. The front end MDE file, the back end MDB file, and the MDW file are all located on...
18
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers....
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
1
by: DBC User | last post by:
I am using VS2005 c# project. While debugging my program, (pressing F10) after a while the debug disappears(the yellow line is gone). Does anyone know what causes this to happen? It is not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.