Connecting Tech Pros Worldwide Forums | Help | Site Map

Question why this is happening in a multi file program

Member
 
Join Date: Oct 2006
Posts: 57
#1: Oct 24 '06
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]

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: Oct 24 '06

re: Question why this is happening in a multi file program


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.
Reply