2015-05-14 91 views
1

我一直在用C++編寫一個非常基本的計算程序。它計算一個數字的平方根,並且如果用戶想要的話也將其平方。這是我迄今爲止(我知道這可能是垃圾代碼,但我是一個初學者剛剛嘗試,看看它是如何工作的任何建議,雖然不勝感激。):無論我輸入什麼字符,我的程序都會循環

#include <iostream> 
#include <stdlib.h> 
#include <math.h> 

using namespace std; 

int number;   // Global variables to be used in void functions as well as main. 
int squaredNumber; 
double sqrtResult; 
char input; 
char useAgain; 

void squareNum();  // Prototypes for the void functions 
void sqrtNum(); 
void useAgainQuery(); 

int main() 
{ 
retry:      // Establishing somewhere to send user if their input is invalid. 
    system("cls"); 
    cout << "Square Calcualtions" << endl; 
    cout << "******************" << endl; 
    cout << endl; 
    cout << "Do you want to square a number or find the square root of a number?" << endl; 
    cout << "Please select 1 or 2 respectively." << endl; 
    cout << endl; 
    cin >> input; 
    if (input == '1') 
    { 
     cout << "Please press ENTER to continue." << endl; 
     cin.ignore().get(); 
     squareNum();    // If the input is 1, run the void to square a number. 
    } 
    else if (input == '2') 
    { 
     cout << "Please press ENTER to continue." << endl; 
     cin.ignore().get(); 
     sqrtNum();     // If the input is 2, run the void to sqrt a number. 
    } 
    else if (input != '1' || '2') 
    { 
     system("cls"); 
     cout << "Square Calcualtions" << endl; 
     cout << "******************" << endl; 
     cout << endl; 
     cout << "Your selection was invalid, please enter 1 or 2." << endl; 
     cin.ignore().get(); 
     goto retry;    // If the input isn't either 1 or 2, send back to the start of program. 
    } 
    return 0; 
} 

void squareNum() // function to square the inputted number. 
{ 
    system("cls"); 
    cout << "Square Calcualtions" << endl; 
    cout << "******************" << endl; 
    cout << endl; 
    cout << "Enter the number you want to square." << endl; 
    cin >> number; 
    cout << "You have chosen: " << number << endl; 
    cout << "Press ENTER to calculate." << endl; 
    cin.ignore().get(); 
    system("cls"); 
    squaredNumber = number * number;  // Simple maths to find the square number 
    cout << "You have squared " << number << "." << endl; 
    cout << "The result was " << squaredNumber << "." << endl; 
    cout << "Press ENTER to continue." << endl; 
    cin.get(); 
    useAgainQuery(); 
    return; 
} 

void sqrtNum() 
{ 
    system("cls"); 
    cout << "Square Calcualtions" << endl; 
    cout << "******************" << endl; 
    cout << endl; 
    cout << "Enter the number you would like the square root of." << endl; 
    cin >> number; 
    cout << "You have chosen: " << number << "." << endl; 
    cout << "Press ENTER to calculate." << endl; 
    cin.ignore().get(); 
    system("cls"); 
    sqrtResult = sqrt(number); 
    cout << "You have found the square root of " << number << "." << endl; 
    cout << "The result was: " << sqrtResult << "." << endl; 
    cout << "Press ENTER to continue." << endl; 
    cin.get(); 
    useAgainQuery(); 
    return; 
} 

void useAgainQuery() 
{ 
    system("cls"); 
    cout << "Square Calcualtions" << endl; 
    cout << "******************" << endl; 
    cout << endl; 
    cout << "Would you like to make another calculation?" << endl; 
    cout << "Y for Yes and N for No." << endl; 
    cout << endl; 
    cin >> useAgain; 
    if (useAgain == 'Y' || 'y') 
    { 
    retry2:      // Establishing somewhere to send user if their input is invalid. 
     system("cls"); 
     cout << "Square Calcualtions" << endl; 
     cout << "******************" << endl; 
     cout << endl; 
     cout << "Do you want to square a number or find the square root of a number?" << endl; 
     cout << "Please select 1 or 2 respectively." << endl; 
     cout << endl; 
     cin >> input; 
     if (input == '1') 
     { 
      cout << "Please press ENTER to continue." << endl; 
      cin.ignore().get(); 
      squareNum();    // If the input is 1, run the void to square a number. 
     } 
     else if (input == '2') 
     { 
      cout << "Please press ENTER to continue." << endl; 
      cin.ignore().get(); 
      sqrtNum();     // If the input is 2, run the void to sqrt a number. 
     } 
     else if (input != '1' || '2') 
     { 
      system("cls"); 
      cout << "Square Calcualtions" << endl; 
      cout << "******************" << endl; 
      cout << endl; 
      cout << "Your selection was invalid, please enter 1 or 2." << endl; 
      cin.ignore().get(); 
      goto retry2; 
     } 
    } 
    else if (useAgain != 'Y' || 'y') 
     return; 
    return; 
} 

所以,是的,當我去通過並詢問「你想再玩一次」,它會一遍又一遍地通過它。不管我按什麼鍵,但它循環。任何幫助將不勝感激!

+0

我建議用'do-while'或'while'循環替換'goto retry'。 –

+0

@ThomasMatthews我會如何去做這件事?我聽說goto是一個不好用的,但我如何使用while循環? –

+0

你在學習中是否覆蓋了'''','while'和'do-while'循環?我喜歡:'bool user_error = true; while(user_error){/*...*/};'。如果沒有用戶錯誤(在循環中),將變量設置爲false。如果沒有用戶錯誤,有些人使用'while(true)',然後使用'break'語句。 –

回答

3

這裏更改您的條件:

if (useAgain == 'Y' || 'y') 

if (useAgain == 'Y' || useAgain=='y') 

另外,更改此:

else if (useAgain != 'Y' || 'y') 
{ 
    return; 
} 

這樣:

else if (useAgain != 'Y' && useAgain!='y') 
{ 
    return; 
} 
+0

謝謝!這工作完美。出於好奇,你爲什麼要用&&而不是重複上面的過程? –

+0

@IainAnderson:這兩個條件都滿足了,也就是說,只有'useAgain'既不是'y'也不是'Y'時它才應該是真的。 –

1
  1. 請勿使用goto,請改用(1)。
  2. 你最後的if語句是錯誤的,它需要

    if (input != '1' && input != '2')

+0

謝謝,添加&&修復它。 (1)如何工作?我將如何實現它是一個更好的替代goto? –

+0

它會一直循環,直到您在某些條件下使用「break」語句,比如說輸入'q'來退出應用程序。 – Diceyus

1

也許嘗試創建一個控制整個主迴路中的布爾變量,就像這樣:

#include <InsertLibrariesHere> 
int main(){ 
    bool running=true; 
    while(running){ 
     //calculations here 
     //continue(Y/N)? 
     if (input == N || input == n){running = false;} 
    } 
} 
1

你可以簡化你的比較字母:

if (std::toupper(useAgain) == 'Y') 

if (std::tolower(useAgain) == 'y') 

你也可以轉換的情況下,你輸入後:

cin >> useAgain; 
useAgain = std::toupper(useAgain); 
if (useAgain == 'Y') 

還有std::transform是你需要一個std::string變換到所有下級或全部大寫。

相關問題