Yen to Dollars conversion problem

This is a discussion on Yen to Dollars conversion problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to build a program that inputs deposits into an array by dollar amount, and displays them back ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    22

    Yen to Dollars conversion problem

    I am trying to build a program that inputs deposits into an array by dollar amount, and displays them back in both dollars and Yen. I successfully built the program in C, but for some reason I can't figure it out in C++. What is wrong here?

    Code:
    // Evaluate Yen
    #include <iostream>
    
    using namespace std;
    
    
    const int MAX_CURRENCYS = 100;          // Max number of Bank Deposits 
    const float DOLLARS_TO_YEN = 102.2;   // 1 Dollar = 102.2 Yen 
    
    
    // Function prototypes
    void readDollars ( float Dollars[], int count );
    void DollarsToYen ( float Dollars[], float Yen[], int count );
    void displayData ( float Dollars[], float Yen[], int count );
    
    int main (void)
    {
        int nums;               // Actual number of  deposits 
        float Dollars[MAX_CURRENCYS];    //Dollars  
        float Yen[MAX_CURRENCYS];      // Yen 
    
        // Prompt the user for the number of deposits. 
        cout << endl << "\n Enter the number of  deposits: ";
        cin >> nums;
    
        // Read the amount in each deposit .
        readDollars(Dollars,nums);
    
        // Convert Dollars to Yen.
        DollarsToYen(Dollars,Yen,nums);
    
        // Display the amount in each deposit
        displayData(Dollars,Yen,nums);
        
        return 0;
    }
    
    // read number of Dollars in each  account from the keyboard 
    
    void readDollars ( float Dollars[], int count )
    {
       int j;
       cout << "Enter the  dollar amount for each : ";
       for ( j = 0; j < count; j++ )
           cin >> Dollars[j];
    }
    
    
    //gives number of Yen in each account
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ );
            Yen[j]=Dollars[j]*DOLLARS_TO_YEN;
        
    }
    
    
    // displays the amount in each account in both Dollars and Yen 
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
        cout <<  "\n Dollars       Yen     \n" << endl;
        for ( j=0;j<count;j++)
        {
           cout << "  %7.2f        %7.2f   \n" << Dollars[j] << Yen[j];
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    Hmm I have another version setup with setw, but when I try to compile dev gives me an "outdated header" error with the iomanip?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Make sure you type <iomanip> and not <iomanip.h> (or <iostream.h>).

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    *slaps forehead*

    That was not my only problem fortunately. I'm not getting values to display still.

    Code:
    // Evaluate Yen
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const int MAX_CURRENCYS = 100;          // Max number of Bank Deposits 
    const float DOLLARS_TO_YEN = 102.2;   // 1 Dollar = 102.2 Yen 
    
    
    // Function prototypes
    void readDollars ( float Dollars[], int count );
    void DollarsToYen ( float Dollars[], float Yen[], int count );
    void displayData ( float Dollars[], float Yen[], int count );
    
    int main (void)
    {
        int nums;               // Actual number of  deposits 
        float Dollars[MAX_CURRENCYS];    //Dollars  
        float Yen[MAX_CURRENCYS];      // Yen 
    
        // Prompt the user for the number of deposits. 
        cout << endl << "\n Enter the number of  deposits: ";
        cin >> nums;
    
        // Read the amount in each deposit .
        readDollars(Dollars,nums);
    
        // Convert Dollars to Yen.
        DollarsToYen(Dollars,Yen,nums);
    
        // Display the amount in each deposit
        displayData(Dollars,Yen,nums);
        
        return 0;
    }
    
    // read number of Dollars in each  account from the keyboard 
    
    void readDollars ( float Dollars[], int count )
    {
       int j;
       cout << "Enter the  dollar amount for each : ";
       for ( j = 0; j < count; j++ )
           cin >> Dollars[j];
    }
    
    
    //gives number of Yen in each account
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ );
        
    }
    
    
    // displays the amount in each account in both Dollars and Yen 
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
        cout << "\n Dollars       Yen  \n" << endl;
        for ( j=0;j<count;j++)
        {
            cout << setw(4) << j+1 << setw(7) << "" << setprecision(12) << "" << endl;
        }
    }
    Thanks

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Well, gee. You might want to do something useful with your arrays in these functions:

    Code:
    //gives number of Yen in each account
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ );
        
    }
    
    
    // displays the amount in each account in both Dollars and Yen 
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
        cout << "\n Dollars       Yen  \n" << endl;
        for ( j=0;j<count;j++)
        {
            cout << setw(4) << j+1 << setw(7) << "" << setprecision(12) << "" << endl;
        }
    }

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    Code:
    
    //gives number of Yen in each account
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ );
            Yen[j]=Dollars[j]*DOLLARS_TO_YEN;
    }
    
    
    // displays the amount in each account in both Dollars and Yen 
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
        cout << "\n Dollars       Yen  \n" << endl;
        for ( j=0;j<count;j++)
        {
            cout << setw(4)<< j+1 << setw(7) << "" << setprecision(12) << "" << endl;
            cout << Dollars[j] << Yen[j] << endl;
        }
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    setprecision(12)! Did you read what setprecision actually does?

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    Thank you for your help. I get the correct dollar amount displayed back out of the array, but the Yen values appear to be arbitrary. For example, if I enter 2 Deposits, both at 1 Dollar and 1 Dollar, I get back from the array (despite having set setprecision(3)) 3.21e-039 (for the first deposit), then for the second deposit it simply displays "0" in Yen. What can I do to correct this?

    Code:
    // Evaluate Yen
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const int MAX_CURRENCYS = 100;          // Max number of Bank Deposits 
    const float DOLLARS_TO_YEN = 102.2;   // 1 Dollar = 102.2 Yen 
    
    
    // Function prototypes
    void readDollars ( float Dollars[], int count );
    void DollarsToYen ( float Dollars[], float Yen[], int count );
    void displayData ( float Dollars[], float Yen[], int count );
    
    int main (void)
    {
        int nums;               // Actual number of  deposits 
        float Dollars[MAX_CURRENCYS];    //Dollars  
        float Yen[MAX_CURRENCYS];      // Yen 
    
        // Prompt the user for the number of deposits. 
        cout << endl << "\n Enter the number of  deposits: ";
        cin >> nums;
    
        // Read the amount in each deposit .
        readDollars(Dollars,nums);
    
        // Convert Dollars to Yen.
        DollarsToYen(Dollars,Yen,nums);
    
        // Display the amount in each deposit
        displayData(Dollars,Yen,nums);
        
        return 0;
    }
    
    // read number of Dollars in each  account from the keyboard 
    
    void readDollars ( float Dollars[], int count )
    {
       int j;
       cout << "Enter the  dollar amount for each : ";
       for ( j = 0; j < count; j++ )
           cin >> Dollars[j];
    }
    
    
    //gives number of Yen in each account
    
    void DollarsToYen ( float Dollars[], float Yen[], int count )
    {
        int j;
        for ( j = 0; j < count; j++ );
            Yen[j]=Dollars[j]*DOLLARS_TO_YEN;
    }
    
    
    // displays the amount in each account in both Dollars and Yen 
    
    void displayData ( float Dollars[], float Yen[], int count )
    {
        int j;
        cout << "\n    Dollars       Yen  \n" << endl;
        for ( j=0;j<count;j++)
        {
            cout << setw(1) << j+1 << "     " << setprecision(3) << Dollars[j] << "          " << setprecision(3) << Yen[j] << endl;
        }
    }
    Thanks in advance.
    -JJ

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    for ( j = 0; j < count; j++ );

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    Commenting that out of the DollarsToYen function correctly displays the Yen amount, but for only one deposit. Every deposit after is still listed as 0.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by JJohnson View Post
    Commenting that out of the DollarsToYen function correctly displays the Yen amount, but for only one deposit. Every deposit after is still listed as 0.
    Look at it again. Notice what has been bolded and re-dyed, and ponder its existence.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    22
    *bangs head on desk*

    You have no idea how many hours I've paged through the same three chapters in my book trying to figure this out. Thanks a TON for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Currency Conversion Table- Pounds to Dollars
    By phillysbest29 in forum C Programming
    Replies: 3
    Last Post: 09-23-2009, 08:37 PM
  2. 200,000 dollars to get linux to run on xBox
    By cpluspluser__ in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-06-2003, 10:23 PM
  3. 240 dollars and a coke later...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-19-2002, 07:56 AM
  4. Someone grab me those dollars
    By Mario in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 07:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21