Royal
Royal

Reputation: 41

C++ basic calculator. Beginner

Can someone please explain to me where is the error in my code? It is a simple calculator. The end result is nonsense numbers for some reason and when it compiles it doesn't even consider going trough "izteiksmju kalkulators". Thank you very much for help!

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

   using namespace std;

   int main()
   {

     string o,funkcija;
     double x,y,l,result;
     double Pi = 3.14159265359;
     char operators;
     cout << "Velaties izmantot aritmetikas vai funkciju kalkulatoru?" << endl;
     getline(cin,o);

     if (o == "funkciju kalkulators" || "funkciju" || "oo")
     {
        cout << "ievadiet funkciju:" << endl;
        cin >> l;
        if (funkcija == "cos")  result = (x*Pi/180);
        if (funkcija == "sin")  result = (x*Pi/180);
        if (funkcija == "tan")  result = (x*Pi/180);
        if (funkcija == "exp")  result = exp(x);
        if (funkcija == "log")  result = log(x); 
        if (funkcija == "sqrt") result = sqrt(x);
        cout << endl;
        cout << " " << result;
    }

    else if (o == "Izteiksmju kalkulators" || "izteiksmi" || "aa")
    {
        cout << "Ievadiet izteiksmi:" << endl;
        cin >> x;
        cin >> operators;
        cin >> y;
        if (operators == '+')  result = x+y;
        if (operators == '-')  result = x-y;
        if (operators == '*')  result = x*y;
        if (operators == '/')  result = x/y;
        cout << "Rezultats:" << " " << result << endl;
   }

   else
   {
        cout << "error" << endl;
   }

    return 0;
}

Upvotes: 0

Views: 255

Answers (3)

BufferOverflow
BufferOverflow

Reputation: 543

Here was there many errors.

if (o == "funkciju kalkulators" || "funkciju" || "oo")

What is this? This will in all cases be true, it should be written like this:

if (o == "funkciju kalkulators" || o == "funkciju" || o == "oo")

And same with:

else if (o == "Izteiksmju kalkulators" || "izteiksmi" || "aa")

Should be:

else if (o == "Izteiksmju kalkulators" || o == "izteiksmi" || o == "aa")

if (funkcija == "cos")  result = (x*Pi/180);
if (funkcija == "sin")  result = (x*Pi/180);
if (funkcija == "tan")  result = (x*Pi/180);
if (funkcija == "exp")  result = exp(x);
if (funkcija == "log")  result = log(x); 
if (funkcija == "sqrt") result = sqrt(x);

Where is funkcija and x initialized?


And where is l used?

cin >> l;

Upvotes: 2

Ryan Haining
Ryan Haining

Reputation: 36892

This comparison

o == "Izteiksmju kalkulators" || "izteiksmi" || "aa"

Doesn't mean what you think, it will evaluate the booleanness of the later strings and the whole thing is always true, instead you probable mean

o == "Izteiksmju kalkulators" || o == "izteiksmi" || o == "aa"

the same goes for your similar if expressions

Upvotes: 3

maayanwis
maayanwis

Reputation: 5

you seem to forgot to initialize x,y,l,result till you do, they are garbage. so, result = sqrt(x) when x is garbage... will give you garbage.

also, when you use || you need to write the entire condition for all three statements.

Upvotes: 0

Related Questions