0

I'm learning programming and just made a very crappy calculator program in C++. The program works correctly up until line: std::getline (std::cin, operation);

It never asks for user input and the program stops running. What's the reason for this?

(Sorry about the abhorrent design in advance)

Below is code:

#include <iostream>
#include <cmath>
#include <string>

float value1;
float value2; 

float addition = value1 + value2;
float subtraction = value1 - value2;
float multiplication = value1 * value2;
float division = value1 / value2;
float exponentiation = pow (value1,value2);
float root = pow (value1,(1/value2));

std::string operation;

int main(){
std::cout << "This is a calculator, please enter two numbers.. one number at a time: " << std::endl;

std::cin >> value1;
std::cin >> value2; 
std::cout << "Great, your two numbers are: " << value1 << " and " << value2 << std::endl;

std::cout << "Which operation would you like to perform on number 1 -> number 2? Addition = '+', subtraction = '-', multiplication = '*', division = '/', exponentiation = 'pow', Nth root = 'root': ";

std::getline (std::cin, operation);
    if (operation == "+") {
    std::cout << "Your two numbers add to: "<< addition << "!"; 
    }
    else if (operation == "-") {
    std::cout << "Your two numbers subtract to: " << subtraction << "!";
    }
    else if (operation == "*") {
    std::cout << "Your two numbers multiply to: " << multiplication << "!"; 
    }
    else if (operation == "/") {
    std::cout << "Your two numbers divide to: " << division << "!";
    }
    else if (operation == "pow") {
    std::cout << "Your two numbers exponentiate to: " << exponentiation << "!";
    }
    else if (operation == "root") {
    std::cout << "Your two numbers root to: " << root << "!";
    }
return 0;
}

273K
  • 29,503
  • 10
  • 41
  • 64
BDO
  • 21
  • 4

0 Answers0