需要解决方案 C++ 循环和条件
Need a solution C++ Loops and conditions
我希望程序在有人从选项中输入超出范围的数字时显示错误....
#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 的总和,依此类推....
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;
只是一个简短的建议。您可以将输入类型从 char ch 更改为 int ch。一个字符是一个字符。如果你输入 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');
我希望程序在有人从选项中输入超出范围的数字时显示错误....
#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 的总和,依此类推....
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;
只是一个简短的建议。您可以将输入类型从 char ch 更改为 int ch。一个字符是一个字符。如果你输入 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');