用C++結構寫貨幣轉換程式..我卡關了..請高手相救

Home Home
引用 | 編輯 zptdaniel
2007-03-28 03:15
樓主
推文 x0
用C++的結構寫一個貨幣轉換程式,要有一個貨幣物件其中有貨幣型態與此貨幣轉換至台幣的倍率。並且要有一個清單可以讓使用者選擇轉換的方式(我是設定美金跟台幣互轉)

程式要印出使用者的輸入值與輸出值..輸入負數就結束.

PS:附上我所寫的程式碼..一直顯示錯誤:36 expected primary-expression before ')' token
30 expected primary-expression before ')' token

有點急..請高手救命


#include<iostream>
#include<iomanip>
using namespace std;

struct money
{
double US;
double NT;
};
double conversion1(money &money) ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 jenyen63
2007-03-29 16:27
1樓
  
看了一下, 你的if else 語句中的else 部分少了括號

獻花 x0
引用 | 編輯 jc_taiwan
2007-04-02 12:52
2樓
  
我知道卡關的感覺不好受,不過我想你在迷宮已經頭暈了吧...
你用太多money,有些是型態(struct),有些是未宣告的東西(因為未宣告所以不知怎麼稱呼)

你先將struct外加上範圍子的宣告,會比較好釐清你的問題
複製程式
#include<iostream>
#include<iomanip>
using namespace std;

namespace my_struct
{
       struct money
       {
              double US;
              double NT;
       };  
}

double conversion1(my_struct::money &money);
double conversion2(my_struct::money &money);

int main()
{
  int i,j;
  double US=0,NT=0;
 
  cout<<"請選擇轉換方式"<<endl<<endl;
  cout<<"(1)台幣 ---> 美金"<<endl;
  cout<<"(2)美元 ---> 台幣"<<endl;
  cin>>j;
 
    while((US >0 && NT >0) && j >0)
    {
      if(j==1)
        {
            cout<<"請輸入台幣金額: ";
            cin>>NT;
            cout<<"您輸入的台幣金額為: "<<NT;
            conversion1(money);
            cout<<"換算後的美元金額為: "<<US;
        }else{
            cout<<"請輸入美元金額: ";
            cin>>US;
            cout<<"您輸入的美元金額為: "<<US;
            conversion2(money);
            cout<<"換算後的美元金額為: "<<NT;
              }
    }
 
 
system("PAUSE");
return 0;
}
double conversion1(my_struct::money& money)
      {
          int rate = 35,US,NT;
          US = NT * rate;
          return US;
      }
double conversion2(my_struct::money& money)
      {
          int rate = 35,NT,US;
          NT = US / rate;
          return NT;
      }
        

結果編譯出來會出現'money' : undeclared identifier

你需要用my_struct::money這個形態去產生一個實體money
然後將NT及US assign到那個實體,之後將實體傳進你的函式
再用這個實體的NT及US做運算...

然後初步的程式就變成
複製程式
#include<iostream>
#include<iomanip>
using namespace std;

namespace my_struct
{
       struct money
       {
              double US;
              double NT;
       };  
}

double conversion1(my_struct::money &money);
double conversion2(my_struct::money &money);

int main()
{
  int i,j;
  double US=0,NT=0;
 //用my_struct::money這個形態去產生一個實體money
  my_struct::money money;
  cout<<"請選擇轉換方式"<<endl<<endl;
  cout<<"(1)台幣 ---> 美金"<<endl;
  cout<<"(2)美元 ---> 台幣"<<endl;
  cin>>j;
 
    while((US >0 && NT >0) && j >0)
    {
      if(j==1)
        {
                     
            cout<<"請輸入台幣金額: ";
            cin>>NT;
                     //將NT及US assign到那個實體
                     money.NT = NT;
            cout<<"您輸入的台幣金額為: "<<money.NT;
            US = conversion1(money);
            cout<<"換算後的美元金額為: "<<US;
        }else{
            cout<<"請輸入美元金額: ";
            cin>>US;
                    //將NT及US assign到那個實體
                     money.US = US;
            cout<<"您輸入的美元金額為: "<<money.US;
            NT = conversion2(money);
            cout<<"換算後的美元金額為: "<<NT;
              }
    }
 
 
system("PAUSE");
return 0;
}
double conversion1(my_struct::money& money)
      {
          int rate = 35,US,NT;
          US = money.NT * rate;
          return US;
      }
double conversion2(my_struct::money& money)
      {
          int rate = 35,NT,US;
          NT = money.US / rate;
          return NT;
      }
   
當然還沒有結束,你的程式還是有些陷阱
在剛開始的時候,你MAIN()裡頭的US跟NT都宣告為0
所以While一直都進不去...

(時間緊迫時,還是要看清楚money的定義....)

獻花 x0
引用 | 編輯 mt9261
2008-04-23 21:47
3樓
  
#include <iostream.h>

typedef double MONEY;

void main()
{
     double      rate;
     MONEY      usd,ntd;

     cout << "Please Tell Me the Rate :" <<"\n";
     cout << "USD : NT = 1 : A"<<"\n";
     cout << "A = ";
     cin >> rate;

     cout << "How Many New Taiwan Dollar You Have : ";
     cin >> ntd;

     usd = (1/rate) * ntd;
     
     cout <<endl;
     cout <<ntd<<" NT = "<<usd<<" USD"<<"\n";
}

獻花 x0