Thread: Need help with this simple conversion program

  1. #1
    Registered User
    Join Date
    Apr 2006

    Need help with this simple conversion program

    How do i get the constant value to be a value entered by the user. The only way i can get this to work is to make it a constant.

    The program works fine for now. I just dont get why my exchange cannot use the value when its not a constant?????

    Also i want to figure out how to get the exRate entered once in the loop instead of over and over.

    Thanks in advance for your help.

    #include <iostream>
    #include <math.h>
    #include <iomanip>
    using namespace std;
    class Exchange
    float exRate;
    float BritishPound;
    float USDollar;
    void main()
    	cout << "Lets process 3 british pound amounts and convert to US dollars" << "\n";
    	int const values = 3;
    	if (values==0) {
    		cout << "Error with input! please run program again!";
        Exchange test[values];  
    	for(int i=0; i < values; i++)
    			cout << "Enter in British to US conversion rate:  ";
    			cin >> test[i].exRate;
    			cout << "Enter British Value to convert: ";
                cin  >> test[i].BritishPound;
    			test[i].USDollar = (test[i].BritishPound * test[i].exRate);
    cout << setw(10) << "\nExchange Rate      " <<setw(15) << "British pounds to convert     "
    		 << setw(10) << "US money    " << "\n";
    	for(int j=0; j < values; j++)
    		cout<<setiosflags(ios::fixed | ios::showpoint)<<setprecision(2)
    			<< setw(3) << test[j].exRate<<setw(20)<< test[j].BritishPound
    			<<setw(30) << test[j].USDollar << "\n";

  2. #2
    I am me, who else?
    Join Date
    Oct 2002
    You can't change a const value at all. It is meant to never ever change while the program is running. You can change it in the code, but not while running the program. Also, before other people decide to get you for this, change void main to int main... void is just a bad thing for main...

    In fact I am having a hard time figuring out what you want. Did you want to always have 3 British pound values or do you want that to change?

  3. #3
    Registered User
    Join Date
    Jan 2005
    In C++ you must use a constant size for an array (although it is allowed in C and on some compilers as an extension). The alternative is to use dynamic memory allocation.

    The best way to do that in C++ is to use a vector to store your Exchange objects. You can also use new[] to allocate space dynamically for the array, but you also must remember to call delete[] in order to free the memory and destruct all the objects.

    That might be overkill for this program. You could just make a really big array (like 100) and then error out if the user enters more than 100 for the number of values.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > void main()
    Still not read the FAQ huh?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM