My first program. I need some help.

Okay I am on a debian linux system using Code:Blocks ide. I'm trying to make a simple Currency converter (USD to CDN & vice versa) that runs on a console. I set the exchange rate to be 1.0188 (CDN(x) * 1.0188), but when I run the program it converts it like it's only multiplying by 1 not 1.0188. How can I get my program to multiply it by 1.0188 not 1. and show a value rounded up to 2 decimal points.

Thanks in advance, ace.
you might be trying to store a floating point value in an int and having it truncate. You'll have to post some code before we can really know.
Here's what I have so far. Please note that I'm planning to make this look neater by moving the functions CDNtoUSD and USDtoCDN(currently print2) to another .cpp file and make a header file.

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
#include <iostream>
#include <iomanip>

using namespace std;

int CDNtoUSD(int x, int y)
{
    return x * y;
}

void print2() //Declaration of print2. This will eventually becom USDtoCDN
{
    cout << "print2 start";
}

int main()
{
    int n;
    cout << "Type 1 for CDN to USD" << endl; //Giving a choice between CDNtoUSD and print2
    cout <<"Type 2 for print2: ";
    cin >> n;

    if (n == 1)
    {
      int x;       //Amount in CDN entered
      int y;
      y = 1.0188;  //Exchange rate
      cout << "please enter amount: ";
      cin >> x;
      cout << x << "=" << CDNtoUSD(x , y);
    }
    else if (n == 2)
      print2();

    return 0;
}


EDIT: okay I changed the x and y in CDNtoUSD to float. and the x and y in main() to float as well. but how do I get it to show an answer rounded up in 2 digits after the decimal point.
Last edited on
cout << setiosflags(ios::fixed) << setprecision(2) << x;

where 'x' is whatever you want to print.

Interesting enough is that you will need iomanip and you have already included it without using methods from it...
Interesting enough is that you will need iomanip and you have already included it without using methods from it...


I like to get ahead of myself :)

but anyways It's still multiplying by 1 instead of 1.0188
Here is my new code:
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
#include <iostream>
#include <iomanip>

using namespace std;

int CDNtoUSD(float x, float y)
{
    return x * y;
}

void print2() //Declaration of print2. This will eventually becom USDtoCDN
{
    cout << "print2 start";
}

int main()
{
    int n;
    cout << "Type 1 for CDN to USD" << endl; //Giving a choice between CDNtoUSD and print2
    cout <<"Type 2 for print2: ";
    cin >> n;

    if (n == 1)
    {
      float x;       //Amount in CDN entered
      float y;
      float a;
      y = 1.0188;  //Exchange rate
      cout << "please enter amount: ";
      cin >> x;
      cout << x << "=";
      a = CDNtoUSD(x , y);
      cout << setiosflags(ios::fixed) << setprecision(2) << a;
    }
    else if (n == 2)
      print2();

    return 0;

}


I entered 10 and it responded with 10.00. I entered 1000 and it gave me 1018.00
int CDNtoUSD(float x, float y)
You're multiplying two floats... and returning the truncated int?
woops! Thank you wolfgang. All I had to do was change that int to float.
Topic archived. No new replies allowed.