Help with program

Hi,

Why doesn't this program work? The 'if' block works but the 'else' block doesn't. I think it doesn't recognise that O_s is not a float. How do I overcome this?

[code]
#include
#include

int main()
{
// Declaring the variables
float n_1 = 9999.0, n_2 = 9999.0, n = 9999.0;
char O_b;
char O_s;

std::cout << ": ";

// Enter the calculation
std::cin >> n_1 >> O_b >> n_2;

// Checks if calculation is basic or scientific
if (n_1 != 9999.0)
{
switch (O_b)
{
case '+':
std::cout << n_1 + n_2;
break;

case '-':
std::cout << n_1 - n_2;
break;

case '*':
std::cout << n_1 * n_2;
break;

case '/':
if (n_2 !=0)
{
std::cout << n_1 / n_2;
}
else
{
std::cout << "Division by zero error!";
}
break;

default:
std::cout << "Invalid calculation!";
break;
}
}
else
{
std::cout << "Please enter your scientific calculation:
";
std::cin >> O_s >> n;

switch (O_s)
{
case 's':
std::cout << "You have entered a sine calc";
std::cout << sin(n);
break;

case 'c':
std::cout << "You have entered a cosine calc";
std::cout << cos(n);
break;

case 'l':
std::cout << "You have entered a log calc";
std::cout << log(n);
break;

case 'e':
std::cout << "You have entered a exp calc";
std::cout << exp(n);
break;

default:
std::cout << "Invalid calc";
break;
}

}

return 0;
}
[/code]

Thanks in advance.

Comments

  • : Hi,
    :
    : Why doesn't this program work? The 'if' block works but the 'else' block doesn't. I think it doesn't recognise that O_s is not a float. How do I overcome this?
    :
    : [code]
    : #include
    : #include
    :
    : int main()
    : {
    : // Declaring the variables
    : float n_1 = 9999.0, n_2 = 9999.0, n = 9999.0;
    : char O_b;
    : char O_s;
    :
    : std::cout << ": ";
    :
    : // Enter the calculation
    : std::cin >> n_1 >> O_b >> n_2;
    :
    : // Checks if calculation is basic or scientific
    : if (n_1 != 9999.0)
    : {
    : switch (O_b)
    : {
    : case '+':
    : std::cout << n_1 + n_2;
    : break;
    :
    : case '-':
    : std::cout << n_1 - n_2;
    : break;
    :
    : case '*':
    : std::cout << n_1 * n_2;
    : break;
    :
    : case '/':
    : if (n_2 !=0)
    : {
    : std::cout << n_1 / n_2;
    : }
    : else
    : {
    : std::cout << "Division by zero error!";
    : }
    : break;
    :
    : default:
    : std::cout << "Invalid calculation!";
    : break;
    : }
    : }
    : else
    : {
    : std::cout << "Please enter your scientific calculation:
    ";
    : std::cin >> O_s >> n;
    :
    : switch (O_s)
    : {
    : case 's':
    : std::cout << "You have entered a sine calc";
    : std::cout << sin(n);
    : break;
    :
    : case 'c':
    : std::cout << "You have entered a cosine calc";
    : std::cout << cos(n);
    : break;
    :
    : case 'l':
    : std::cout << "You have entered a log calc";
    : std::cout << log(n);
    : break;
    :
    : case 'e':
    : std::cout << "You have entered a exp calc";
    : std::cout << exp(n);
    : break;
    :
    : default:
    : std::cout << "Invalid calc";
    : break;
    : }
    :
    : }
    :
    : return 0;
    : }
    : [/code]
    :
    : Thanks in advance.
    :
    :

    Just a quick observation, but shouldn't:
    if (n_1 != 9999.0)
    be:
    if (atof(n_1) != 9999.0) ...?


  • : : Hi,
    : :
    : : Why doesn't this program work? The 'if' block works but the 'else' block doesn't. I think it doesn't recognise that O_s is not a float. How do I overcome this?
    : :
    : : [code]
    : : #include
    : : #include
    : :
    : : int main()
    : : {
    : : // Declaring the variables
    : : float n_1 = 9999.0, n_2 = 9999.0, n = 9999.0;
    : : char O_b;
    : : char O_s;
    : :
    : : std::cout << ": ";
    : :
    : : // Enter the calculation
    : : std::cin >> n_1 >> O_b >> n_2;
    : :
    : : // Checks if calculation is basic or scientific
    : : if (n_1 != 9999.0)
    : : {
    : : switch (O_b)
    : : {
    : : case '+':
    : : std::cout << n_1 + n_2;
    : : break;
    : :
    : : case '-':
    : : std::cout << n_1 - n_2;
    : : break;
    : :
    : : case '*':
    : : std::cout << n_1 * n_2;
    : : break;
    : :
    : : case '/':
    : : if (n_2 !=0)
    : : {
    : : std::cout << n_1 / n_2;
    : : }
    : : else
    : : {
    : : std::cout << "Division by zero error!";
    : : }
    : : break;
    : :
    : : default:
    : : std::cout << "Invalid calculation!";
    : : break;
    : : }
    : : }
    : : else
    : : {
    : : std::cout << "Please enter your scientific calculation:
    ";
    : : std::cin >> O_s >> n;
    : :
    : : switch (O_s)
    : : {
    : : case 's':
    : : std::cout << "You have entered a sine calc";
    : : std::cout << sin(n);
    : : break;
    : :
    : : case 'c':
    : : std::cout << "You have entered a cosine calc";
    : : std::cout << cos(n);
    : : break;
    : :
    : : case 'l':
    : : std::cout << "You have entered a log calc";
    : : std::cout << log(n);
    : : break;
    : :
    : : case 'e':
    : : std::cout << "You have entered a exp calc";
    : : std::cout << exp(n);
    : : break;
    : :
    : : default:
    : : std::cout << "Invalid calc";
    : : break;
    : : }
    : :
    : : }
    : :
    : : return 0;
    : : }
    : : [/code]
    : :
    : : Thanks in advance.
    : :
    : :
    :
    : Just a quick observation, but shouldn't:
    : if (n_1 != 9999.0)
    : be:
    : if (atof(n_1) != 9999.0) ...?
    :

    Hi,

    But atof converts a string to a float. I have already declared n_1 to be a float so I don't think that will work.
  • [blue]Your problem is getting all input as values.
    Try to get a string, then extract values and operations from that string.[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories