variable types

my program


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//cents

#include <iostream>
 using std::cout;
 using std::cin;
 using std::endl;
#include <conio.h>

int main ()
{
    
    double a, b , c, d, e; 
    double total;
    
    total = (.50*a) + (.20*b) + (.10*c) + (.05*d) + (.01*e); 
    
    cout<<"\tFifty cents? "; 
    cin>> a,
    cout<<"\n\tTwenty cents? "; 
    cin>> b,
    cout<<"\n\tTen cents? ";
    cin>> c,
    cout<<"\n\tFive cents? "; 
    cin>> d,
    cout<<"\n\tOne cents? "; 
    cin>> e,
    cout<<"\n\tYour Total is: Php " << total << endl;
 
    
    getch();
    return 0;
}

result:
        Fifty cents? 3

        Twenty cents? 2

        Ten cents? 1

        Five cents? 2

        One cents? 3

        Your Total is: Php 4.51831e-303


i dont understand why the printed total is different when in fact
if you would just calculate using the given variables the answer
would only be 2.13
Last edited on
do the declaration normally, but initialize it AFTERcin >> e;, perhaps.

If not that, then sorry.
The compiler reads from top to bottom. So in line 15, variables a, b, c, d, and e aren't set to any values. It doesn't remember that total = blah blah blah when you tell it to display the total. Put line 15 after cin >> e; like QWERTYman said, when a, b, c, d and e have values.
thanks a lot.. now my program is all alright..
Topic archived. No new replies allowed.