【发布时间】: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