Howdy, Stranger!

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

Categories

Need some assistance with this piece of code

Geeth AsokanGeeth Asokan Member Posts: 127
Hi,

I am trying to create a piece of code where the user inputs a calculation, for example, 1+1, it should go through the [b]if - else[/b] statement and execute the answer.....and it does. But this does not work when I enter a scientific calculation like s1 (sin1).

Anyway, here is the code:

[code]
#include
#include
#include

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

cout << ": ";

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

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

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

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

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

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

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

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

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

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

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

}

return 0;
}
[/code]

Thanks for your help.

Comments

  • Geeth AsokanGeeth Asokan Member Posts: 127
    : Hi,
    :
    : I am trying to create a piece of code where the user inputs a calculation, for example, 1+1, it should go through the [b]if - else[/b] statement and execute the answer.....and it does. But this does not work when I enter a scientific calculation like s1 (sin1).
    :
    : Anyway, here is the code:
    :
    : [code]
    : #include
    : #include
    : #include
    :
    : int main()
    : {
    : // Declaring the variables
    : float n_1, n_2, n;
    : char O_b;
    : char O_s;
    :
    : cout << ": ";
    :
    : // Enter the calculation
    : cin >> n_1 >> O_b >> n_2;
    :
    : // Checks if calculation is basic or scientific
    : if (!isdigit(n_1))
    : {
    : switch (O_b)
    : {
    : case '+':
    : cout << n_1 + n_2;
    : break;
    :
    : case '-':
    : cout << n_1 - n_2;
    : break;
    :
    : case '*':
    : cout << n_1 * n_2;
    : break;
    :
    : case '/':
    : if (n_2 !=0)
    : {
    : cout << n_1 / n_2;
    : }
    : else
    : {
    : cout << "Division by zero error!";
    : }
    : break;
    :
    : default:
    : cout << "Invalid calculation!";
    : break;
    : }
    : }
    : else
    : {
    : cout << "Please enter your scientific calculation:
    ";
    : cin >> O_s >> n;
    :
    : switch (O_s)
    : {
    : case 's':
    : cout << "You have entered a sine calc";
    : cout << sin(n);
    : break;
    :
    : case 'c':
    : cout << "You have entered a cosine calc";
    : cout << cos(n);
    : break;
    :
    : case 'l':
    : cout << "You have entered a log calc";
    : cout << log(n);
    : break;
    :
    : case 'e':
    : cout << "You have entered a exp calc";
    : cout << exp(n);
    : break;
    :
    : default:
    : cout << "Invalid calc";
    : break;
    : }
    :
    : }
    :
    : return 0;
    : }
    : [/code]
    :
    : Thanks for your help.
    :

    The [b]if[/b] block works but the [b]else[/b] block doesn't work properly.

  • DariusDarius Member Posts: 1,666
    [b][red]This message was edited by Darius at 2002-10-29 14:9:29[/red][/b][hr]
    : : Hi,
    : :
    : : I am trying to create a piece of code where the user inputs a calculation, for example, 1+1, it should go through the [b]if - else[/b] statement and execute the answer.....and it does. But this does not work when I enter a scientific calculation like s1 (sin1).
    : :
    : : Anyway, here is the code:
    : :
    : : [code][blue]
    : : #include
    : : #include
    : : #include

    using namespace std;
    [/blue]
    : :
    : : int main()
    : : {
    : : // Declaring the variables
    : : float n_1, n_2, n;
    : : char O_b;
    : : char O_s;
    : :
    : : cout << ": ";
    : :
    : : // Enter the calculation
    : : cin >> n_1 >> O_b >> n_2;
    : :
    : : // Checks if calculation is basic or scientific
    : : if (!isdigit(n_1))
    : : {
    : : switch (O_b)
    : : {
    : : case '+':
    : : cout << n_1 + n_2;
    : : break;
    : :
    : : case '-':
    : : cout << n_1 - n_2;
    : : break;
    : :
    : : case '*':
    : : cout << n_1 * n_2;
    : : break;
    : :
    : : case '/':
    : : if (n_2 !=0)
    : : {
    : : cout << n_1 / n_2;
    : : }
    : : else
    : : {
    : : cout << "Division by zero error!";
    : : }
    : : break;
    : :
    : : default:
    : : cout << "Invalid calculation!";
    : : break;
    : : }
    : : }
    : : else
    : : {
    : : cout << "Please enter your scientific calculation:
    ";
    : : cin >> O_s >> n;
    : :
    : : switch (O_s)
    : : {
    : : case 's':
    : : cout << "You have entered a sine calc";
    : : cout << sin(n);
    : : break;
    : :
    : : case 'c':
    : : cout << "You have entered a cosine calc";
    : : cout << cos(n);
    : : break;
    : :
    : : case 'l':
    : : cout << "You have entered a log calc";
    : : cout << log(n);
    : : break;
    : :
    : : case 'e':
    : : cout << "You have entered a exp calc";
    : : cout << exp(n);
    : : break;
    : :
    : : default:
    : : cout << "Invalid calc";
    : : break;
    : : }
    : :
    : : }
    : :
    : : return 0;
    : : }
    : : [/code]
    : :
    : : Thanks for your help.
    : :
    :
    : The [b]if[/b] block works but the [b]else[/b] block doesn't work properly.
    :
    :


    What is 's' as a float? That's your problem.

    Here's a possible solution, peek at the value and make sure it is a digit before you read it in, that way you know you'll get at least some number or no number (it's not perfect, e.g. '-' but you could make it so).

    Also, in the long run you may want to look at what a recursive descent parser is.


    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion



  • Geeth AsokanGeeth Asokan Member Posts: 127
    : [b][red]This message was edited by Darius at 2002-10-29 14:9:29[/red][/b][hr]
    : : : Hi,
    : : :
    : : : I am trying to create a piece of code where the user inputs a calculation, for example, 1+1, it should go through the [b]if - else[/b] statement and execute the answer.....and it does. But this does not work when I enter a scientific calculation like s1 (sin1).
    : : :
    : : : Anyway, here is the code:
    : : :
    : : : [code][blue]
    : : : #include
    : : : #include
    : : : #include
    :
    : using namespace std;
    : [/blue]
    : : :
    : : : int main()
    : : : {
    : : : // Declaring the variables
    : : : float n_1, n_2, n;
    : : : char O_b;
    : : : char O_s;
    : : :
    : : : cout << ": ";
    : : :
    : : : // Enter the calculation
    : : : cin >> n_1 >> O_b >> n_2;
    : : :
    : : : // Checks if calculation is basic or scientific
    : : : if (!isdigit(n_1))
    : : : {
    : : : switch (O_b)
    : : : {
    : : : case '+':
    : : : cout << n_1 + n_2;
    : : : break;
    : : :
    : : : case '-':
    : : : cout << n_1 - n_2;
    : : : break;
    : : :
    : : : case '*':
    : : : cout << n_1 * n_2;
    : : : break;
    : : :
    : : : case '/':
    : : : if (n_2 !=0)
    : : : {
    : : : cout << n_1 / n_2;
    : : : }
    : : : else
    : : : {
    : : : cout << "Division by zero error!";
    : : : }
    : : : break;
    : : :
    : : : default:
    : : : cout << "Invalid calculation!";
    : : : break;
    : : : }
    : : : }
    : : : else
    : : : {
    : : : cout << "Please enter your scientific calculation:
    ";
    : : : cin >> O_s >> n;
    : : :
    : : : switch (O_s)
    : : : {
    : : : case 's':
    : : : cout << "You have entered a sine calc";
    : : : cout << sin(n);
    : : : break;
    : : :
    : : : case 'c':
    : : : cout << "You have entered a cosine calc";
    : : : cout << cos(n);
    : : : break;
    : : :
    : : : case 'l':
    : : : cout << "You have entered a log calc";
    : : : cout << log(n);
    : : : break;
    : : :
    : : : case 'e':
    : : : cout << "You have entered a exp calc";
    : : : cout << exp(n);
    : : : break;
    : : :
    : : : default:
    : : : cout << "Invalid calc";
    : : : break;
    : : : }
    : : :
    : : : }
    : : :
    : : : return 0;
    : : : }
    : : : [/code]
    : : :
    : : : Thanks for your help.
    : : :
    : :
    : : The [b]if[/b] block works but the [b]else[/b] block doesn't work properly.
    : :
    : :
    :
    :
    : What is 's' as a float? That's your problem.
    :
    : Here's a possible solution, peek at the value and make sure it is a digit before you read it in, that way you know you'll get at least some number or no number (it's not perfect, e.g. '-' but you could make it so).
    :
    : Also, in the long run you may want to look at what a recursive descent parser is.
    :
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :
    :
    :

    Hi,

    Thanks for your help. I am very grateful. But, what do you mean by peeking at the value?

    Thanks again for your help

  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ________ < http://forcoder.org /> free ebooks and video tutorials about || Ruby, C#, PHP, Delphi, R, Perl, PL/SQL, Visual Basic, Assembly, MATLAB, Scratch, C, JavaScript, Java, Python, C++, Visual Basic .NET, Objective-C, Go, Swift Hack, Erlang, Julia, LabVIEW, COBOL, Lisp, FoxPro, SAS, Alice, Transact-SQL, Apex, Kotlin, Logo, Bash, Ada, Rust, Clojure, D, Awk, Scheme, ML, VBScript, Fortran, ABAP, Crystal, Prolog, Lua, Dart, F#, Scala || _

Sign In or Register to comment.