A program that converts Mexican Peso and USD.(C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nabilahsan
    New Member
    • Sep 2014
    • 15

    #1

    A program that converts Mexican Peso and USD.(C++

    Hey guys! So I have an assignment which requires me to make a program that will determine the buying and selling value of USD and Mexican Peso. The program is to run the entire day and at the end it will have to be terminated using a special sentinel. The idea is that since this is a business we will have to make a profit so that is how the calculate is set up. The program is not complete though but I am facing major problems. If anyone wants the actual assignment sheet only then I will post it as I am not sure whether it is allowed to post it or not. I can PM it too. Thanks.

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double setRate(double rate);
    double rate = 13.30;
    enum exType { BUY, SELL };
    int exchn;
    
    
    int main()
    {
    	double exchange(double rate, exType ex);
    	void showRates(double rate);
    	int showMenu(double rate);
    	void buyPesos();
    	void sellPesos();
    	double amount;
    	{
    		cout << "         CURRENCY EXCHANGE MEXICAN PESOS<>USD\n";
    		cout << "\n--------------------------------------------------\n";
    		cout << "     Buying Pesos" << "     " << " Selling Pesos\n";
    		cout << "\n";
    		cout << "$1 =    12.3690 MXN" << "     " << "14.2310 MXN\n";
    
    
    		cout << "\nSelect the option\n" << endl
    			<< "1. Pesos to Dollars" << endl
    			<< "2. Dollars to Pesos" << endl
    			<< "3. Change Exchange Rate" << endl
    			<< "4. Show Exchange Rate" << endl
    			<< "5. Exit" << endl;
    		system("pause");
    	}
    
    	{
    		enum exchange { SellPesos = 1, BuyPesos, ChangeRate, ShowRate, Exit };
    
    		while (exchn == 1 || exchn == 2 || exchn == 3 || exchn == 4 || exchn <= 0 || exchn >= 6)
    		{
    			int	showMenu(double rate);
    			cin >> exchn;
    			cout << endl;
    		}
    
    
    		switch (exchn)
    		{
    		case SellPesos:
    			cout << "Enter the amount to convert to Dollars: ";
    			/*one rate for changing the foreign currency to the $US*/
    			cin >> amount;
    			cout << endl;
    			cout << "You will get " << amount / (rate*0.93) << " USD" << endl;
    			break;
    
    		case BuyPesos:
    		{
    			cout << "Enter the amount to exchange in Pesos: "; //one rate for changing from $US to the foreign currency
    			cin >> amount;
    			cout << endl;
    			cout << "You will get " << amount*(rate*1.07) << " Pesos" << endl;
    			break;
    		}
    		case Exit:
    		{
    			cout << "Exiting" << endl << endl;
    			break;
    		}
    			cout << " INVALID " << endl;
    			system("pause");
    			return 0;
    		}
    	}
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What is your question?

    Comment

    • nabilahsan
      New Member
      • Sep 2014
      • 15

      #3
      It just prints the code below but nothing else.

      Code:
      cout << "         CURRENCY EXCHANGE MEXICAN PESOS<>USD\n";
      		cout << "\n--------------------------------------------------\n";
      		cout << "     Buying Pesos" << "     " << " Selling Pesos\n";
      		cout << "\n";
      		cout << "$1 =    12.3690 MXN" << "     " << "14.2310 MXN\n";
      
      
      		cout << "\nSelect the option\n" << endl
      			<< "1. Pesos to Dollars" << endl
      			<< "2. Dollars to Pesos" << endl
      			<< "3. Change Exchange Rate" << endl
      			<< "4. Show Exchange Rate" << endl
      			<< "5. Exit" << endl;
      		system("pause");
      	}

      Comment

      • nabilahsan
        New Member
        • Sep 2014
        • 15

        #4
        It exits. But doesn't do anything in between. Also I would like to point that I am new to C++. I am enrolled in an 8 week course so everything is pretty much rushed. I'm reading books and trying to implement the idea because obviously I do not want my homework to be done by others. Please help me out. Thank you.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Line 40 uses uninitialized variable exchn. This isn't an automatic variable, so it's initial value is zero. However, your program reacts to zero by skipping over all the interesting parts.

          Comment

          • nabilahsan
            New Member
            • Sep 2014
            • 15

            #6
            I initialized exchn as a global variable instead but it still doesn't seem to work. It builds fine but when I am debugging it once again skips over the interesting part.

            Code:
            #include <iostream>
            #include <cmath>
            
            using namespace std;
            
            double setRate(double rate);
            double rate = 13.30;
            enum exType { BUY, SELL };
            int exchn;
            
            
            int main()
            {
            	double exchange(double rate, exType ex);
            	void showRates(double rate);
            	int showMenu(double rate);
            
            	void buyPesos();
            	void sellPesos();
            	double amount;
            	{
            		cout << "         CURRENCY EXCHANGE MEXICAN PESOS<>USD\n";
            		cout << "\n--------------------------------------------------\n";
            		cout << "     Buying Pesos" << "     " << " Selling Pesos\n";
            		cout << "\n";
            		cout << "$1 =" << rate*0.93 << "     " << rate*1.07 ;
            
            
            		cout << "\nSelect the option\n" << endl
            			<< "1. Pesos to Dollars" << endl
            			<< "2. Dollars to Pesos" << endl
            			<< "3. Change Exchange Rate" << endl
            			<< "4. Show Exchange Rate" << endl
            			<< "5. Exit" << endl;
            		
            	}
            
            	{
            		enum exchange { SellPesos = 1, BuyPesos, ChangeRate, ShowRate, Exit };
            
            		while (exchn == 1 || exchn == 2 || exchn == 3 || exchn == 4 || exchn <= 0 || exchn >= 6)
            		{
            			int	showMenu(double rate);
            			cin >> exchn;
            			cout << endl;
            		}
            
            
            		switch (exchn)
            		{
            		case SellPesos:
            			cout << "Enter the amount to convert to Dollars: ";
            			/*one rate for changing the foreign currency to the $US*/
            			cin >> amount;
            			cout << endl;
            			cout << "You will get " << amount / (rate*0.93) << " USD" << endl;
            			break;
            
            		case BuyPesos:
            		{
            			cout << "Enter the amount to exchange in Pesos: "; //one rate for changing from $US to the foreign currency
            			cin >> amount;
            			cout << endl;
            			cout << "You will get " << amount*(rate*1.07) << " Pesos" << endl;
            			break;
            		}
            		case ChangeRate:
            		{
            			cout << "The current rate is : " << rate;
            			cout << "Enter the new exchange rate (Pesos per Dollar): ";
            			cin >> rate;
            			break;
            		}
            		case ShowRate:
            		{
            			cout << "The current rate is : " << rate;
            			break;
            		}
            		case Exit:
            		{
            			cout << "Exiting" << endl << endl;
            			break;
            		}
            		default:	cout << " INVALID " << endl;
            			
            		}
            	}
            	return 0;
            }

            Comment

            • nabilahsan
              New Member
              • Sep 2014
              • 15

              #7
              Okay so i made this work. Except for 3. Change rate. In this case the exchange rate will be updated by the user and the entire process continues with the new exchange rate. SO how do I do it?? And I am also very much confused about the sentinel thing that will terminate the program.

              Code:
              #include <iostream>
              #include <cmath>
              
              using namespace std;
              
              double setRate(double rate);
              double rate = 13.30;
              enum exType { BUY, SELL };
              int exchn;
              
              
              int main()
              {
              	double exchange(double rate, exType ex);
              	void showRates(double rate);
              	int showMenu(double rate);
              
              	void buyPesos();
              	void sellPesos();
              	double amount;
              	{
              		cout << "         CURRENCY EXCHANGE MEXICAN PESOS<>USD\n";
              		cout << "\n--------------------------------------------------\n";
              		cout << "     Buying Pesos" << "     " << " Selling Pesos\n";
              		cout << "\n";
              		cout << "$1 =" << rate*0.93 << "     " << rate*1.07;
              
              
              		cout << "\nSelect the option\n" << endl
              			<< "1. Pesos to Dollars" << endl
              			<< "2. Dollars to Pesos" << endl
              			<< "3. Change Exchange Rate" << endl
              			<< "4. Show Exchange Rate" << endl
              			<< "5. Exit" << endl;
              
              	}
              
              	{
              		enum exchange { SellPesos = 1, BuyPesos, ChangeRate, ShowRate, Exit };
              
              		while (exchn == 1 || exchn == 2 || exchn == 3 || exchn == 4 || exchn <= 0 || exchn >= 6)
              		{
              			{
              				int	showMenu(double rate);
              				cin >> exchn;
              				cout << endl;
              			}
              
              
              			switch (exchn)
              			{
              			case SellPesos:
              				cout << "Enter the amount to convert to Dollars: ";
              				/*one rate for changing the foreign currency to the $US*/
              				cin >> amount;
              				cout << endl;
              				cout << "You will get " << amount / (rate*0.93) << " USD" << endl;
              				break;
              
              			case BuyPesos:
              			{
              				cout << "Enter the amount to exchange in Pesos: "; //one rate for changing from $US to the foreign currency
              				cin >> amount;
              				cout << endl;
              				cout << "You will get " << amount*(rate*1.07) << " Pesos" << endl;
              				break;
              			}
              			case ChangeRate:
              			{
              				cout << "The current rate is : " << rate;
              				cout << "Enter the new exchange rate (Pesos per Dollar): ";
              				cin >> rate;
              				break;
              			}
              			case ShowRate:
              			{
              				cout << "The current rate is : " << rate;
              				break;
              			}
              			case Exit:
              			{
              				cout << "Exiting" << endl << endl;
              				break;
              			}
              			default:	cout << " INVALID " << endl;
              
              			}
              		}
              		return 0;
              	}
              }

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Looking at your latest post...
                1. You have function prototypes scattered throughout the body of your function. That is uncommon style that can be confusing. I recommend you move all of the function prototypes above the beginning of the main function. For that matter, I don't any place where you define or call these functions -- are you sure they exist?
                2. Line 39 defines enum exchange in the middle of your function body without declaring a variable. This is uncommon style that can be confusing. I recommend you move this line above the beginning of the main function.
                3. You want the line-41 loop to execute over and over again until the Exit value is entered. In general, a while loop executes 0 or more times, a do-while loop executes 1 or more times. You may be better served with a do-while loop that executes while exchn is not equal to the Exit value.
                4. Line 50 is a little tricky - it effectively casts int variable exchn to enum exchange. That will work, but it is a little tricky.
                5. You may need to terminate lines 70, 72, and 77 with an endl.
                6. You could put the return statement inside the ShowRate case and let your loop run forever: while(1).

                Comment

                • nabilahsan
                  New Member
                  • Sep 2014
                  • 15

                  #9
                  About the scattered function prototypes, I really don't know what to do but it's ok for the time being. When I keep it in do-while loop, it takes no input.

                  This is how the output is supposed to look like:

                  CURRENCY EXCHANGE MEXICAN PESOS<>USD
                  ------------------------------------
                  Buying Pesos Selling Pesos
                  ------------ -------------
                  $1 = 12.3690 MXN 14.2310 MXN
                  Select the option
                  1. Pesos to Dollars
                  2. Dollars to Pesos
                  3. Change Exchange Rate
                  4. Show Exchange Rates
                  5. Exit
                  >> 1
                  Enter the amount to exchange in Pesos: 100
                  You will get: 7.03 in USD.

                  CURRENCY EXCHANGE MEXICAN PESOS<>USD
                  ------------------------------------

                  Buying Pesos Selling Pesos
                  ------------ -------------
                  $1 = 12.3690 MXN 14.2310 MXN
                  Select the option
                  1. Pesos to Dollars
                  2. Dollars to Pesos
                  3. Change Exchange Rate
                  4. Show Exchange Rates
                  5. Exit
                  >> 2

                  Enter the amount to exchange in Dollars: 1000
                  You will get: 12369.00 in MXN.

                  CURRENCY EXCHANGE MEXICAN PESOS<>USD
                  ------------------------------------
                  Buying Pesos Selling Pesos
                  ------------ -------------
                  $1 = 12.3690 MXN 14.2310 MXN
                  Select the option
                  1. Pesos to Dollars
                  2. Dollars to Pesos
                  3. Change Exchange Rate
                  4. Show Exchange Rates
                  5. Exit
                  >> 3

                  The current rate is: 13.30.
                  Enter the new exchange rate (Pesos per Dollar):

                  CURRENCY EXCHANGE MEXICAN PESOS<>USD
                  ------------------------------------
                  Buying Pesos Selling Pesos
                  ------------ -------------
                  $1 = 12.4505 MXN 14.3247 MXN
                  Select the option
                  1. Pesos to Dollars
                  2. Dollars to Pesos
                  3. Change Exchange Rate
                  4. Show Exchange Rates
                  5. Exit
                  >> 5

                  Code:
                  #include <iostream>
                  #include <cmath>
                  
                  using namespace std;
                  
                  double setRate(double rate);
                  double rate = 13.30;
                  enum exType { BUY, SELL };
                  int exchn;
                  
                  
                  int main()
                  {
                  	double exchange(double rate, exType ex);
                  	void showRates(double rate);
                  	int showMenu(double rate);
                  	enum exchange { SellPesos = 1, BuyPesos, ChangeRate, ShowRate, Exit };
                  
                  	double amount;
                  	{
                  		cout << "         CURRENCY EXCHANGE MEXICAN PESOS<>USD\n";
                  		cout << "\n--------------------------------------------------\n";
                  		cout << "     Buying Pesos" << "     " << " Selling Pesos\n";
                  		cout << "\n";
                  		cout << "$1 =" << rate*0.93 << "     " << rate*1.07;
                  
                  
                  		cout << "\nSelect the option\n" << endl
                  			<< "1. Pesos to Dollars" << endl
                  			<< "2. Dollars to Pesos" << endl
                  			<< "3. Change Exchange Rate" << endl
                  			<< "4. Show Exchange Rate" << endl
                  			<< "5. Exit" << endl;
                  
                  	}
                  
                  	{
                  		while (exchn == 1 || exchn == 2 || exchn == 3 || exchn == 4 || exchn <= 0 || exchn >= 6)
                  			{
                  			{
                  				int	showMenu(double rate);
                  				cin >> exchn;
                  				cout << endl;
                  			}
                  
                  
                  			switch (exchn)
                  			{
                  			case SellPesos:
                  				cout << "Enter the amount to convert to Dollars: ";
                  				/*one rate for changing the foreign currency to the $US*/
                  				cin >> amount;
                  				cout << endl;
                  				cout << "You will get " << amount / (rate*0.93) << " USD" << endl;
                  				break;
                  
                  			case BuyPesos:
                  			{
                  				cout << "Enter the amount to exchange in Pesos: "; //one rate for changing from $US to the foreign currency
                  				cin >> amount;
                  				cout << endl;
                  				cout << "You will get " << amount*(rate*1.07) << " Pesos" << endl;
                  				break;
                  			}
                  			case ChangeRate:
                  			{
                  				cout << "The current rate is : " << rate << endl;
                  				cout << "Enter the new exchange rate (Pesos per Dollar): " << endl;
                  				cin >> rate;
                  				break;
                  			}
                  			case ShowRate:
                  			{
                  				cout << "The current rate is : " << rate << endl;
                  				break;
                  				return 0;
                  			}
                  			case Exit:
                  			{
                  				cout << "Exiting" << endl << endl;
                  				break;
                  
                  			default:	cout << " INVALID. Please try again. Thank You. " << endl;
                  			}
                  			}
                  		}
                  	}
                  }

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    do-while format looks like this...
                    Code:
                    do {
                       <print menu>
                       <get new value for exchn>
                       <switch statement to process each exchn value except 5>
                       } while (exchn != 5);
                    <process exchn value of 5>

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      From your most recent post...

                      Spurious function prototypes:
                      • Line 7 - setRate()
                      • Line 15 - exchange()
                      • Line 16 - showRates()
                      • Line 17 - showMenu()
                      • Line 41 - showMenu()

                      Get rid of these!

                      Enumerations that are being used like #define's:
                      • Line 9 - exType
                      • Line 18 - exchange

                      The C way to do this is with #define's. As I understand it, the C++ way to do this is with static const variables (you can do it this way in C too).
                      Code:
                      #define SellPesos 1             // C way.
                      static const int BuyPesos = 2;  // C++ way.

                      Comment

                      • nabilahsan
                        New Member
                        • Sep 2014
                        • 15

                        #12
                        As per the assignment I have to use the function prototypes that you mentioned needed to be removed. Also we haven't covered static const so it cannot be used in the assignment.

                        Here is the link for more clarification:

                        Comment

                        Working...