2015-01-12 28 views
-2

你好,我剛開始學習C++和我試圖做一個計算器,現在有一個問題,我只是不知道如何修復C++。C++第一個程序 - 計算器

#include <iostream> 
using namespace std; 


int main() { 
int a, b, c, d; 

cout << "1. Saskaitiissana(+)" << endl; 
cout << "2. atnnemssana(-)" << endl; 
cout << "3. daliissana(/)" << endl; 
cout << "4. reizinaassana(*)" << endl; 

cin >> d; 

switch(d){ 
case 1 : 
cout << "ievadiet a un b lai saskaitiitu(+)" << endl; 

cin >> a; 
cin >> b; 
c = a + b; 

cout << "The sum of number 1 and number 2 is " << c << "\n" <<endl; 
break; 


case 2 : 
cout << "ievadiet a un b lai atnnemtu(-)" << endl; 

cin >> a; 
cin >> b; 
c = a - b; 

cout << c << endl; 
break; 


case 3 : 
cout << "ievadiet a un b lai reizinaatu(*)" << endl; 

cin >> a; 
cin >> b; 
c = a * b; 

cout << c << endl; 
break; 

case 4 : 
cout << "ievadiet a un b lai dal'itu(/)" << endl; 
cin >> a; 
cin >> b; 

if(b==0) 
{ 
cout<<"Nulle neder! start over."<<endl; 
} 

c = a/b; 

cout << c << endl; 
break; 
} 



return 0; 
} 

我仍然需要做的事情。 找到程序僅使用數字的最簡單方法。另外,當我輸入一個數字時,它不能是「空白空間」。 另外我怎樣才能讓它完成後給你的結果,回到開始菜單的開始?如果我想退出程序,請按Esc或5?

此外與退出選項我正在考慮使用做「5」時按下,可以在C++中工作嗎?

現在我最感興趣的是如何檢查程序只使用數字,添加數字時沒有空格。

感謝您的時間:)

+0

關於限制字符輸入到只有數字檢查http://stackoverflow.com/questions/18936664/how-to-limit-input-to-numbers-只有和http://www.cplusplus.com/forum/beginner/21595/ – Dzyann

+0

請花一些時間閱讀[幫助]。這不是提供廣泛建議和指導的一般論壇。我們希望關注具體的編程問題。 – BartoszKP

+0

使用建議的鏈接改善您的代碼,並在單獨發佈問題後遇到問題。 – Dzyann

回答

2

忽視非數字輸入,您可以這段代碼:

std::cin >> d; 
while(std::cin.fail()) 
{ 
    std::cin.clear(); 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
    std::cout << "Bad entry. Enter a NUMBER: "; 
    std::cin >> d; 
} 

或C風格:

while(scanf("%i",&d)!=1) 
{ 
    fseek(stdin,0,SEEK_END); 
} 

你也可以將您的整個代碼放在while語句中,以在一次操作後重新運行計算器。

1

考慮到安全輸入:

//---------------------------------------------------------------------------- 
#include <iostream> 
using namespace std; 

//---------------------------------------------------------------------------- 
void SafeDouble (double &d) 
{ 
    while (!(cin >> d)) 
    { cin.clear(); 
     while (cin.get() != '\n'); 
     cout << "\tIncorrect. Try again\n\t"; 
    } 
    cin.sync(); 
} 
//---------------------------------------------------------------------------- 
int main() 
{ 
     cout << "The simpliest calculator\n"; 

     double a = 0.,b = 0.; 
     cout << "\na = "; 
     SafeDouble (a); 
     cout << "b = "; 
     SafeDouble (b); 

     cout << "\nEnter operation sign: +, -, * or /\n"; 
     char op; 
     cin >> op; 
     cin.sync(); 

     switch (op) 
     { 
      case '+': cout << a << " + " << b << " = " << a + b; 
         break; 
      case '-': cout << a << " - " << b << " = " << a - b; 
         break; 
      case '*': cout << a << " - " << b << " = " << a * b; 
         break; 
      case '/': if (b == 0.0) 
          cout << "Division by zero"; 
         else 
          cout << a << "/" << b << " = " << a/b; 
         break; 
      default: cout << "Incorrect operation sign"; 
     } 

     cin.get(); 
     return 0; 
} 
//-----------------------------------------------------------------------------