2012-04-04 16 views
0

これは非常に曖昧ですが、C++の新機能です。最初のプロジェクトとして電卓を作っていて、やりたいことは、「y」と答えた場合は、スクリプトを最初からやり直してください...基本的には。スクリプトを再実行する方法は?

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

using namespace std; 
int main() 
    { 
     cout << "Hello and Welcome to the Test Calculator!\n"; 
     signed char choice; 
     char resp; 
     cout << "Choose your problem:\n a)Addition\n b)Subtraction\n c)Multiplication\n d)Division\n e)Square Root\n f)Hypotenuse\n"; 
     scanf ("%c", &choice); 
     switch (choice) 
     { 
      case 'a': 
      { 
       int a; 
       int b; 
       cout << "Addition\n"; 
       cout << "Please enter a number:\n"; 
       cin >> a; 
       cout << "Please enter your second number:\n"; 
       cin >> b; 
       cin.ignore(); 
       int result = a + b; 
       cout << "Calculating...\n"; 
       cout << "Your total is:\n"<<" "<<result; 
       cin.get(); 
       break; 
      } 
      case 'b': 
      { 
       int c; 
       int d; 
       cout << "Subtraction\n"; 
       cout << "Please enter a number:\n"; 
       cin >> c; 
       cout << "Please enter your second number:\n"; 
       cin >> d; 
       cin.ignore(); 
       int result2 = c - d; 
       cout << "Calculating...\n"; 
       cout << "Your total is:\n"<<" "<<result2; 
       cin.get(); 
       break; 
      } 
      case 'c': 
      { 
       int e; 
       int f; 
       cout << "Multiplication\n"; 
       cout << "Please enter a number:\n"; 
       cin >> e; 
       cout << "Please enter your second number:\n"; 
       cin >> f; 
       cin.ignore(); 
       int result3 = e * f; 
       cout << "Calculating...\n"; 
       cout << "Your total is:\n"<<" "<<result3; 
       cin.get(); 
       break; 
      } 
      case 'd': 
      { 
       int g; 
       int h; 
       cout << "Division\n"; 
       cout << "Please enter a number:\n"; 
       cin >> g; 
       cout << "Please enter your second number:\n"; 
       cin >> h; 
       cin.ignore(); 
       int result4 = g/h; 
       cout << "Calculating...\n"; 
       cout << "Your total is:\n"<<" "<<result4; 
       cin.get(); 
       break; 
      } 
      case 'e': 
      { 
       int x; 
       #define square ((x)*(x)) 
       cout << "Square Root\n"; 
       cout << "Please enter a number:\n"; 
       cin >> x; 
       cin.ignore(); 
       cout << "Calculating...\n"; 
       cout << "Your total is:\n"<<" "<<square; 
       cin.get(); 
       break; 
      } 
      case 'f': 
      { 
       int i; 
       int j; 
       cout << "Hypotenuse\n"; 
       cout << "Enter your smaller side:\n"; 
       cin >> i; 
       cout << "Please enter the longer side:\n"; 
       cin >> j; 
       cin.get(); 
       int hypotenuse = ((i*i)+(j*j)); 
       cout << "Calculating...\n"; 
       cout << "The hypotenuse is the square root of:\n"<<" "<<hypotenuse; 
       cin.ignore(); 
       cout << "Would you like to do another problem?\n y)Yes\n n)No\n"; 
       cin >> resp; //this is where im trying to test this at 
      } 
      default: 
      { 
       cout << " \n"; 
       cout << "Error: Undefined response\n"; 
       cout << "Contact the source programmer for details\n"; 
      } 
    } 
    } 

答えて

0

あなたはそれを見つけるまでcharは==「n」は、それが継続するかどうかを確認し、whileループにスイッチを入れた場合。

while(choice != 'n') 
{ 
    switch (choice) 
    { 
     case 'a': 
     { 
      int a; 
      int b; 
      cout << "Addition\n"; 
      cout << "Please enter a number:\n"; 
      cin >> a; 
      cout << "Please enter your second number:\n"; 
      cin >> b; 
      cin.ignore(); 
      int result = a + b; 
      cout << "Calculating...\n"; 
      cout << "Your total is:\n"<<" "<<result; 
      cin.get(); 
      break; 
     } 
     case 'b': 
     { 
      int c; 
      int d; 
      cout << "Subtraction\n"; 
      cout << "Please enter a number:\n"; 
      cin >> c; 
      cout << "Please enter your second number:\n"; 
      cin >> d; 
      cin.ignore(); 
      int result2 = c - d; 
      cout << "Calculating...\n"; 
      cout << "Your total is:\n"<<" "<<result2; 
      cin.get(); 
      break; 
     } 
     case 'c': 
     { 
      int e; 
      int f; 
      cout << "Multiplication\n"; 
      cout << "Please enter a number:\n"; 
      cin >> e; 
      cout << "Please enter your second number:\n"; 
      cin >> f; 
      cin.ignore(); 
      int result3 = e * f; 
      cout << "Calculating...\n"; 
      cout << "Your total is:\n"<<" "<<result3; 
      cin.get(); 
      break; 
     } 
     case 'd': 
     { 
      int g; 
      int h; 
      cout << "Division\n"; 
      cout << "Please enter a number:\n"; 
      cin >> g; 
      cout << "Please enter your second number:\n"; 
      cin >> h; 
      cin.ignore(); 
      int result4 = g/h; 
      cout << "Calculating...\n"; 
      cout << "Your total is:\n"<<" "<<result4; 
      cin.get(); 
      break; 
     } 
     case 'e': 
     { 
      int x; 
      #define square ((x)*(x)) 
      cout << "Square Root\n"; 
      cout << "Please enter a number:\n"; 
      cin >> x; 
      cin.ignore(); 
      cout << "Calculating...\n"; 
      cout << "Your total is:\n"<<" "<<square; 
      cin.get(); 
      break; 
     } 
     case 'f': 
     { 
      int i; 
      int j; 
      cout << "Hypotenuse\n"; 
      cout << "Enter your smaller side:\n"; 
      cin >> i; 
      cout << "Please enter the longer side:\n"; 
      cin >> j; 
      cin.get(); 
      int hypotenuse = ((i*i)+(j*j)); 
      cout << "Calculating...\n"; 
      cout << "The hypotenuse is the square root of:\n"<<" "<<hypotenuse; 
      cin.ignore(); 
      cout << "Would you like to do another problem?\n y)Yes\n n)No\n"; 
      cin >> choice; //this is where im trying to test this at 
     } 
    } 
} 
0

別のオプションを追加して電卓を続けることができます。


while(doContinue == true){ 

    switch{.....} 

} 

つまり、コードの切り替え部分をwhileループで囲みます。最初にdoContinueをtrueに設定し、最後にユーザー入力に応じてdoContinueを変更します。

関連する問題