有没有一个变量,可以保存字母和小数
Is there a variable, that can hold letters and decimals c++?
我正在制作一个程序,如果给出一些信息,可以计算三角形,但如果我尝试使用"字母"或","然后,程序出现故障,程序需要重新启动。我目前使用双精度来保持数字和小数,但双精度的规则之一是你需要使用"。"小数,但我已经击中了","所以我想知道是否有办法解决它。我还想知道这个人是否可以选择在同一个盒子里同时写数字和字母。
#include <iostream>
#include <windows.h>
#include <math.h>
#define PI 3.14159265359
using namespace std;
void SetColor(int ForgC);
int failure = 0;
int wait;
double a, b, c, A, B, C = 90;
int main()
{
SetColor(10);
{
info:
while (GetAsyncKeyState(VK_RETURN))
{
}
system("cls");
cout << "nttttFind triangle infon" << endl;
SetColor(11);
cout << "ttttNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "tIf you use decimal numbers then use "." not ","!";
SetColor(10);
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "A = ";
cin >> A;
cout << "B = ";
cin >> B;
while (GetAsyncKeyState(VK_RETURN))
{
}
}
{
calculate:
if (a == 0 && b == 0 && c == 0 && A == 0 && B == 0)
{
cout << "There need to be at least two numbers!n" << endl;
system("pause");
failure = 1;
} // Failsafe
else if (c != 0)
{
if (a >= c || b >= c)
{
cout << "a or b cannot be greater than c!n" << endl;
system("pause");
failure = 1;
}
} // Failsafe 2
else if (A >= 90 || B >= 90)
{
cout << "A or B cannot be equal to, or greater than 90!n" << endl;
system("pause");
failure = 1;
;
} // Failsafe 3
if (a != 0 && c != 0)
{
b = sqrt(c * c - a * a);
A = asin(a / c) * 180 / PI;
B = 90 - A;
}
else if (b != 0 && c != 0)
{
a = sqrt(c * c - b * b);
A = acos(b / c) * 180 / PI;
B = 90 - A;
}
else if (a != 0 && b != 0)
{
c = sqrt(a * a + b * b);
A = atan(a / b) * 180 / PI;
B = 90 - A;
}
else if (c != 0 && A != 0)
{
a = c * sin(A);
b = c * cos(A);
B = 90 - A;
}
else if (c != 0 && B != 0)
{
A = 90 - B;
a = c * sin(A);
b = c * cos(A);
}
else if (a != 0 && B != 0)
{
b = a * tan(B);
c = sqrt(a * a + b * b);
A = 90 - B;
}
else if (b != 0 && A != 0)
{
a = b * tan(A);
c = sqrt(a * a + b * b);
B = 90 - A;
}
else if (a != 0 && A != 0)
{
b = a / tan(A);
c = sqrt(a * a + b * b);
B = 90 - A;
}
else if (b != 0 && B != 0)
{
a = b / tan(B);
c = sqrt(a * a + b * b);
A = 90 - B;
}
if (failure == 1)
{
goto failsafe;
}
goto answer;
}
{
failsafe:
failure = 0;
system("cls");
cout << "nttttFind triangle infon" << endl;
SetColor(11);
cout << "ttttNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "tIf you use decimal numbers then use "." not ","!";
SetColor(10);
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "A = ";
cin >> A;
cout << "B = ";
cin >> B;
while (GetAsyncKeyState(VK_RETURN))
{
}
goto calculate;
}
answer:
while (1)
{
system("cls");
wait = 1;
cout << "nttttFind triangle infon" << endl;
SetColor(11);
cout << "ttttNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "tIf you use decimal numbers then use "." not ","!";
SetColor(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "nDo you want to write new information? (";
SetColor(9);
cout << "Y";
SetColor(10);
cout << "/N)";
while (wait == 1)
{
if (GetAsyncKeyState(VK_RIGHT))
{
wait = 0;
}
else if (GetAsyncKeyState(VK_RETURN))
{
goto info;
}
}
system("cls");
wait = 1;
cout << "nttttFind triangle infon" << endl;
SetColor(11);
cout << "ttttNote: Use 0 if you don't have the number!" << endl;
SetColor(10);
cout << "What information do you have?";
SetColor(11);
cout << "tIf you use decimal numbers then use "." not ","!";
SetColor(10);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "nDo you want to write new information? (Y/";
SetColor(9);
cout << "N";
SetColor(10);
cout << ")";
while (wait == 1)
{
if (GetAsyncKeyState(VK_LEFT))
{
wait = 0;
}
else if (GetAsyncKeyState(VK_RETURN))
{
return 0;
}
}
}
}
void SetColor(int ForgC)
{
WORD wColor;
// We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
// We use csbi for the wAttributes word.
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
// Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
您需要添加错误处理。
如果输入失败,您需要决定要做什么。例如,你可以打印错误信息,清除流状态并重新开始:
std::cin.exceptions(std::ios::failbit);
try
{
std::cout << "a = ";
std::cin >> a;
std::cout << "b = ";
std::cin >> b;
std::cout << "c = ";
std::cin >> c;
std::cout << "A = ";
std::cin >> A;
std::cout << "B = ";
std::cin >> B;
} catch(std::exception const& e)
{
std::cerr << "Error in input.n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
goto info;
}
我衷心建议不使用goto程序流(使用循环)。你也可以使用非异常错误检查:
std::cout << "a b c A B = ";
if (!(std::cin >> a >> b >> c >> A >> B))
{
if (std::cin.eof())
{
std::cerr << "Goodbyen";
exit(1);
}
std::cerr << "Error in input.n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
goto info;
}
p。对于numeric_limits特征,包括
#include <limits>
除了文本输入通常棘手的错误处理,特别是在控制台上,以及有趣的gotos:为了更改数字格式(以及其他格式),您可能需要在cin(以及为了一致性,在cout)上"灌输一个区域设置"。我从来没有这样做过,但这个网站有看起来合理和简单的例子:http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_101.html
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- C++将浮点指针值舍入为小数位数
- 从给定的 I 和 D 序列中形成最小数
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 哈希文件递归并保存到矢量Cryptopp中
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 无法通过头文件保存变量的值
- 我们可以将数据永久保存为数据结构吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- C++ 如何获取兰登数以及如何保存带有 ONY 两位小数的双精度
- 有没有一个变量,可以保存字母和小数