在 y/n 响应后保存 while 循环信息
Save while loop information after y/n response
我的代码有问题,但我不知道如何解决它。
我在C++中使用 Dev-C++ 制作了一个计算器。我做了一个while
循环,这样用户就不必重新启动程序就可以再次使用它。我正在尝试添加先前计算的答案在下一次计算中使用的功能,但代码被跳过了。
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string username;
float num1, num2, answer;
string berekening;
string again;
float oldanswer;
string oldanswerq;
again = "y";
cout << "Hello who are you?" << endl;
cout << "" << endl;
cin >> username;
cout << "" << endl;
cout << "well hello " << username << endl;
cout << "" << endl;
while (again == "y"){
oldanswer = answer;
if (oldanswer == 0) {
cout << "what is the first number you wanna put in " << username << endl;
cout << "" << endl;
cin >> num1;
}
else {
cout << "do you wanna use your old answer? y/n" << endl;
cout << "" << endl;
cin >> oldanswerq;
}
cout << "" << endl;
cout << "+, -, x or ÷(u can use / instead of ÷" << endl;
cout << "" << endl;
cin >> berekening;
cout << "" << endl;
cout << "and what is the second number " << username << endl;
cout << "" << endl;
cin >> num2;
cout << "" << endl;
if (berekening == "+"){
answer = num1 + num2;
}
else if (berekening == "-"){
answer = num1 - num2;
}
else if (berekening == "x"){
answer = num1 * num2;
}
else if (berekening == "/"){
answer = num1 / num2;
}
else if (berekening == "÷"){
answer = num1 / num2;
}
cout << username << ", you choosed " << berekening << " what i did was: " << num1 << berekening << num2 << "=" << answer << endl;
cout << "" << endl;
cout << username << ", do you wanna go again? y/n" << endl;
cout << "" << endl;
cin >> again;
cout << "" << endl;
}
}
我是C++新手,欢迎提出改进建议。
您将oldanswer
设置为等于循环外的newanswer
。它应该在循环内完成。
在比较float
时,您也不应该使用==
,因为它们很少完全等于某物(小数只能精确到计算机上的一定数量)。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 为什么我不能在 while 循环中创建线程?
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 退出简单while循环时出现问题
- 如何在c++中的while循环中保存用户输入
- 在 y/n 响应后保存 while 循环信息