【问题标题】:Need a solution C++ Loops and conditions需要一个解决方案 C++ 循环和条件
【发布时间】:2015-11-16 08:12:03
【问题描述】:

当有人从选项中输入超出范围的数字时,我希望程序显示错误......

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <process.h>
#define PI 3.14159265359
using namespace std;

int main()
{
     int a, b, c, d, e, f;
     long double loga, logarithm, sine, cosine,tan;
     char ch, ch1;
     cout << "\nMy Friend Calci:-";
     cout << "\n1. Addition (1st + 2nd)";
     cout << "\n2. Subtraction (1st - 2nd)";
     cout << "\n3. Multiplication (1st * 2nd)";
     cout << "\n4. Division (1st/2nd)";
     cout << "\n5. Logarithm";
     cout << "\n6. Natural Sine";
     cout << "\n7. Natural Cosine";
     cout << "\n8. Natural Tangent";
     cout << "\n9. Exit";
     do
     {
         cin >> ch;
         if (ch == '1' || ch == '2' || ch == '3' || ch == '4')
         {
          cout << "\nEnter the first and the second numbers respectively:-";
          cin >> a >> b;
         }
         else if (ch == '5' || ch == '6' || ch == '7' || ch == '8')
         {
          cout << "\nEnter the angle (in Radians) or the number you want to calculate Log/Sine/Cosine of:-";
           cin >> loga;
         }
        switch (ch)
        {
        case '1':c = a + b;
            cout << "Sum =" << c;
            break;
        case '2':d = a - b;
            cout << "Difference =" << d;
            break;
        case '3':e = a*b;
            cout << "Product =" << e;
            break;
        case '4':f = a / b;
            cout << "Quotient =" << f;
            break;
        case '5':logarithm = log(loga);
            cout << "log(%d) =" << logarithm;
            break;
        case '6':sine = sin(loga*PI / 180);
            cout << "sin(%d) =" << sine;
            break;
        default:cout << "Wrong Choice My Friend... Now DIE!!!!!";
            cout << "\nOr enter a valid one";
            break;
        }
    cout << "\nWant to Enter MORE (y/n) ?????";
    cin >> ch1;
    if (ch1 == 'y' || ch1 == 'Y')
        cout << "Then enter a choice:";
} while (ch1 == 'y' || ch1 == 'Y');
return 0;
}

当有人输入 1 到 9 的任何其他数值时,我需要程序显示错误消息,同时显示 1 到 4 和 5 到 8 的两条不同消息,如代码所示,如​​果可能的话然后至少没有。循环......(并不是说已经有更少了......呵呵......)

更新!!!!!! 经过一些调整,如果我输入 156,现在发生的事情是 dat,它将 1 作为输入并要求输入,如果我输入 1,那么它会给我 56 和 1 的总和,依此类推....

【问题讨论】:

    标签: c++ loops switch-statement conditional-statements


    【解决方案1】:
    do
         {
             cin >> ch;
    

    添加:

             if (ch < '1' || ch > '9')
             {
                 cout << "Unknown option " << ch << "! Please retry: " << endl;
                 ch1 = 'y';
                 continue;
             }
    
             if (ch == '1' || ch == '2' || ch == '3' || ch == '4')
             {
              cout << "\nEnter the first and the second numbers respectively:-";
              cin >> a >> b;
             }
    

    或者如果你想保留当前格式,那么而不是

        default:cout << "Wrong Choice My Friend... Now DIE!!!!!";
            cout << "\nOr enter a valid one";
            break;
    

    这样写:

    default:cout << "Wrong Choice My Friend... Now DIE!!!!!";
        cout << "\nOr enter a valid one ";
        ch1 = 'y';
        continue;
    

    【讨论】:

    • 但是我应该如何处理开关的默认部分......无论如何感谢帮助
    • @RaviSharmaRs 请注意,'7'、'8' 和 '9' 缺少 case。在这种情况下,default 选项是无用的。您可以删除它或抛出异常,因为您永远不应该到达那里。
    • @RaviSharmaRs 我使用default 选项编辑了我的答案。
    【解决方案2】:

    只是一个快速的建议。您可以将输入类型从 char ch 更改为 int ch。 char 是一个字符。如果输入 10,则只取“1”。如果输入 22。它只需要 '2'。输入的其余部分用于该功能。例如,如果您输入 22。选择选项“2”。 a = 2。 b = 等待用户。 此外,您没有提供正确的退出功能。您可以添加到您的开关条件。

    int a, b, c, d, e, f;
    long double loga, logarithm, sine, cosine, tan;
    char ch1;
    int ch; // int ch instead of char ch
    cout << "\nMy Friend Calci:-";
    cout << "\n1. Addition (1st + 2nd)";
    cout << "\n2. Subtraction (1st - 2nd)";
    cout << "\n3. Multiplication (1st * 2nd)";
    cout << "\n4. Division (1st/2nd)";
    cout << "\n5. Logarithm";
    cout << "\n6. Natural Sine";
    cout << "\n7. Natural Cosine";
    cout << "\n8. Natural Tangent";
    cout << "\n9. Exit";
    do
    {
        cin >> ch;
        if (ch == 1 || ch == 2 || ch == 3 || ch == 4)
        {
            cout << "\nEnter the first and the second numbers respectively:-";
            cin >> a >> b;
        }
        else if (ch == 5 || ch == 6 ) // remove 7 and 8 because there is not matching function in switch statement. Pointless to accept user input.
        {
            cout << "\nEnter the angle (in Radians) or the number you want to calculate Log/Sine/Cosine of:-";
            cin >> loga;
        }
        switch (ch)
        {
        case 1:c = a + b; // change to int instead of char input for all cases
            cout << "Sum =" << c;
            break;
        case 2:d = a - b;
            cout << "Difference =" << d;
            break;
        case 3:e = a*b;
            cout << "Product =" << e;
            break;
        case 4:f = a / b;
            cout << "Quotient =" << f;
            break;
        case 5:logarithm = log(loga);
            cout << "log(%d) =" << logarithm;
            break;
        case 6:sine = sin(loga*PI / 180);
            cout << "sin(%d) =" << sine;
            break;
        case 9: exit(0); // exit your program 
            break;
        default:cout << "Wrong Choice My Friend... Now DIE!!!!!";
            cout << "\nOr enter a valid one";
            break;
        }
        cout << "\nWant to Enter MORE (y/n) ?????";
        cin >> ch1;
        if (ch1 == 'y' || ch1 == 'Y')
            cout << "Then enter a choice:";
    } while (ch1 == 'y' || ch1 == 'Y');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      相关资源
      最近更新 更多