Answer the question
In order to leave comments, you need to log in
How to prematurely terminate a program in c++?
How to terminate the program if the condition is met? Apologies in advance for th...bad code, and lack of comments.
the code:
#include <iostream>
using namespace std;
int main() {
int num1, num2;
string act;
cout << "Calculator version 0.0.1" << endl;
while (1 == 1) {
cout << "Write your first number (or exit to exit programm): " << endl;
cin >> num1;
cout << "Write your second number: " << endl;
cin >> num2;
cout << "Enter your action (Examples: + / - / * / : / other): " << endl;
cin >> act;
if (act == "+") {
cout << "Answer = " << num1 + num2 << endl;
}
if (act == "-") {
cout << "Answer = " << num1 - num2 << endl;
}
if (act == "*") {
cout << "Answer = " << num1 * num2 << endl;
}
if (act == ":") {
cout << "Answer = " << num1 / num2 << endl;
}
if (act == "other") {
string act2;
cout << "Other operations (Example: remainder of the division (RoD)): " << endl;
cin >> act2;
if (act2 == "RoD") {
cout << "Answer = " << num1 % num2 << endl;
}
if (act == "exit") {
return 0; //место, где я хочу чтобы программа заканчивалась, если условие выполнено
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Look carefully where the block ends, if (act == "other")
if (act == "exit")
got inside it, and since. act
cannot take on two different values at the same time, the exit condition is never met.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question