More C++ Project Problems

Hi! If you saw my post C++ Project Poblems you’ll know that I’m trying to code a C++ calculator. I need help with this code:

#include <iostream>
#include <cmath>

int main() {
  int x;
  int remainder;
  int answer;
  int y;
  char input;
  
  std::cout << "Please input one of the following:\n";
  std::cout << "M, A, D, S, or K for a key on how to use this calculator\n";
  std::cout << "Input: ";
  std::cin >> input;
  std::cout << "\n";

  if(input == 'K') {
  std::cout << "Welcome to our useful calculator key!\n";
  std::cout << "M = Multiplication, A = Addition,\n";
  std::cout << "D = Division, S = Subtraction\n";
  std::cout << "please make your input: ";
  std::cin >> input;
  }

  else if(input == 'M') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x * y;
  std::cout << x << " X " << y << " = " << answer << "\n";

  std::cout << "Please make another input: ";
  std::cin >> input;
  std::cout << "\n"; 
  }

  else if(input == 'A') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x + y;
  std::cout << x << " + " << y << " = " << answer << "\n";

  std::cout << "Please make another input: ";
  std::cin >> input;
  std::cout << "\n";  
  }

  else if(input == 'D') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x / y;
  remainder = x % y;
  std::cout << x << " ÷ " << y << " = " << round(answer) << " R: " << remainder << "\n";

  std::cout << "Please make another input: ";
  std::cin >> input;
  std::cout << "\n";
  }

  else if(input == 'S') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x - y;
  std::cout << x << " - " << y << " = " << answer << "\n";

  std::cout << "Please make another input: ";
  std::cin >> input;
  std::cout << "\n";
  }

  else {
 std::cout << "INVALID INPUT please input a Valid option: ";
 std::cin >> input;
  }
}

Now here is the problem:
After a maths question is solved it asks you to:
"Please make another input: "
But when I make a VALID input it doesn’t run the code in the if statements.
any ideas?

You’d probably need to put a loop (that would contain the if and else-if stuff) so that the stuff repeats.
Don’t forget to include a way to exit the loop [or the function] if you do that.

1 Like

Thanks! You were right about the loop! But Just to be clear
in this NEW code:

#include <iostream>
#include <cmath>

int main() {
  int x;
  int remainder;
  int answer;
  int y;
  int score = 0;
  char input;
  bool use = true;
  char yes_no;

  std::cout << "Would you like to use this calculator? Y/N?\n";
  std::cout << ">";
  std::cin >> yes_no;
  std::cout << "\n";

  if(yes_no == 'Y') {
  while(use == true) {
  std::cout << "Please input one of the following:\n";
  std::cout << "M, A, D, S, T, ., or K for a key on how to use this calculator\n";
  std::cout << "Input: ";
  std::cin >> input;
  std::cout << "\n";

  if(input == 'K') {
  std::cout << "Welcome to our useful calculator key!\n";
  std::cout << "M = Multiplication, A = Addition,\n";
  std::cout << "D = Division, S = Subtraction, Also:\n";
  std::cout << "If you want to calculate decimals input: . or:\n";
  std::cout << "If you want to convert temperatures: T\n";
  }

  else if(input == 'M') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x * y;
  std::cout << x << " X " << y << " = " << answer << "\n";

  }

  else if(input == 'A') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x + y;
  std::cout << x << " + " << y << " = " << answer << "\n";

  }

  else if(input == 'D') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x / y;
  remainder = x % y;
  std::cout << x << " ÷ " << y << " = " << round(answer) << " R: " << remainder << "\n";

  }

  else if(input == 'S') {
  std::cout << "Please Input A Number: ";
  std::cin >> x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> y;
  std::cout << "\n";

  answer = x - y;
  std::cout << x << " - " << y << " = " << answer << "\n";

  }

else if(input == '.') {
char decimal_input;
float decimal_x;
float decimal_y;
float decimal_answer;

std::cout << "Welcome to our useful decimal calculator!\n";
std::cout << "Please input: M, A, D, or S.\n";
std::cout << "Input: ";
std::cin >> decimal_input;

if(decimal_input == 'M') {

std::cout << "Please Input A Number: ";
std::cin >> decimal_x;
std::cout << "\n";

std::cout << "Please Input Another Number: ";
std::cin >> decimal_y;
std::cout << "\n";

decimal_answer = decimal_x * decimal_y;
std::cout << decimal_x << " X " << decimal_y << " = " << decimal_answer << "\n";


}

else if(decimal_input == 'A') {

std::cout << "Please Input A Number: ";
  std::cin >> decimal_x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> decimal_y;
  std::cout << "\n";

  decimal_answer = decimal_x + decimal_y;
  std::cout << decimal_x << " + " << decimal_y << " = " << decimal_answer << "\n";

}

else if(decimal_input == 'D') {
  std::cout << "Please Input A Number: ";
  std::cin >> decimal_x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> decimal_y;
  std::cout << "\n";

  decimal_answer = decimal_x / decimal_y;
  std::cout << decimal_x << " ÷ " << decimal_y << " = " << decimal_answer << "\n";
}

else if(decimal_input == 'S') {
  std::cout << "Please Input A Number: ";
  std::cin >> decimal_x;
  std::cout << "\n";

  std::cout << "Please Input Another Number: ";
  std::cin >> decimal_y;
  std::cout << "\n";

  decimal_answer = decimal_x - decimal_y;
  std::cout << decimal_x << " - " << decimal_y << " = " << decimal_answer << "\n";
}

else {
std::cout << "INVALID INPUT please input a valid option.\n";
}
}

else if(input == 'T') {
char temp_input;
float temp_x;
float temp_k;
float temp_c;
float temp_f = 9.0 / 5;
float temp_f1;
float temp_f2;
float temp_f3;
float temp_f4 = 5 / 9;

std::cout << "Welcome to our useful temperature converter!\n";
std::cout << "Please input the unit of measurement for your temperature (K = Kelvin, F = Fahrenheit, C = Celsius)\n";
std::cout << ">";
std::cin >> temp_input;
std::cout << "\n";

if(temp_input == 'K') {
std::cout << "Please input the temperature in Kelvin.\n";
std::cout << ">";
std::cin >> temp_x;
std::cout << "\n";

temp_c = temp_x - 273.15;

temp_f1 = temp_x - 273.15;
temp_f2 = temp_f1 * temp_f;
temp_f3 = temp_f2 + 32;

std::cout << "Your Input: Kelvin: " << temp_x << "\n";
std::cout << "Celsius: " << temp_c << "\n";
std::cout << "Fahrenheit: " << temp_f3 << "\n";
}

else if(temp_input == 'C') {
std::cout << "Please input the temperature in Celsius.\n";
std::cout << ">";
std::cin >> temp_x;
std::cout << "\n";

temp_k = temp_x + 273.15;

temp_f1 = temp_x * temp_f;
temp_f2 = temp_f1 + 32;

std::cout << "Your Input: Celsius: " << temp_x << "\n";
std::cout << "Kelvin: " << temp_k << "\n";
std::cout << "Fahrenheit: " << temp_f2 << "\n";

}

else if(temp_input == 'F') {
std::cout << "Please input the temperature in Fahrenheit.\n";
std::cout << ">";
std::cin >> temp_x;
std::cout << "\n";

temp_f1 = temp_x - 32;
temp_f2 = temp_f1 * temp_f4;
temp_k = temp_f2 + 273.15;

temp_c = temp_f1 * temp_f4;

std::cout << "Your Input: Fahrenheit: " << temp_x << "\n";
std::cout << "Kelvin: " << temp_k << "\n";
std::cout << "Celsius: " << temp_c <<  "\n";
}

else {

}
}

else {
 std::cout << "INVALID INPUT please input a Valid option\n";
  }
  }
  }

else if(yes_no == 'N') {
std::cout << "Okay! type ./a.out if you change your mind.\n";
}

else {
std::cout << "INVALID\n";
}
}

To end the loop all i need do is:
Take input after each question is answered asking the user if they want to leave?
And if they do want to: make using = false?

Yes.
Changing using to be false would work to exit the loop.
Or doing return 0 would also exit the program.

1 Like

Ok thanks a lot buddy!