Can someone please tell me why this shows some weird results?
it compiles, but it doesn't do what it is supposed to, and I am really confused as to why. It is a really basic calculator code I have been working on.
Line 13: C++ does not support an implicit left hand side.
Line 13: w is defined as a single char. You can't compare against multi-character literals.
Line 13: Single quotes define single character literals. If you want to compare strings, use double quotes. w should also be a string. Note that if you're going to use cin with multiple words, you should use getline(). cin stops at the first space.
Line 14,16: Avoid the use of goto This is a bad practice to get into.
Line 27: arithmetic calculator falls through to function calculator.
Line 32-37: Improper use of single quotes again.
Did the compiler not warn you about these problems?
If not, you need to up the warning level you're using.
GCC with -Wall -Wextra -pedantic
.../main.cpp:13:12: warning: character constant too long for its type [enabled by default]
if (w == 'funkciju kalkulators' || 'funkciju')
^
.../main.cpp:13:38: warning: character constant too long for its type [enabled by default]
if (w == 'funkciju kalkulators' || 'funkciju')
^
.../main.cpp:32:19: warning: multi-character character constant [-Wmultichar]
if (funkcija == 'cos') result = (x*Pi/180);
^
W:/Source/Explore/cplusplus_gcc/temp_test/main.cpp:33:19: warning: multi-character character constant [-Wmultichar]
if (funkcija == 'sin') result = (x*Pi/180);
^
etc
I guess you're probably using GCC as MSVC won't even build your code, let alone run it. (Or Clang??)
1>------ Build started: Project: temp_test, Configuration: Debug Win32 ------
1> main.cpp
1>main.cpp(10): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>main.cpp(13): error C2015: too many characters in constant
1>main.cpp(13): error C2015: too many characters in constant
1>main.cpp(13): warning C4127: conditional expression is constant
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Other problems...
Line 10 - pi needs to be a double rather than int
Line 33-35 - looks like something's missing??
And if you stick with goto, you're missing one? (But should switch to a different solution.)
#include <iostream>
#include <cmath>
#include <string>
usingnamespace 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;
}
elseif (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;
}
I am really confused why it only goes to function calculator no matter what. And it even doesnt execute it properly, just prints out some nonsense numbers