Need help with my beginner calculator code

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	double x,y,l,result;
	char operators,funkcija,funkcCal,aritmCal,w;
	int Pi = 3.14159265359;
      cout << "Velaties izmantot aritmetikas vai funkciju kalkulatoru?" << endl;
      cin >> w;
  if (w == 'funkciju kalkulators' || 'funkciju')
  goto funkcCal; 
  else 
  goto aritmCal;

  aritmCal:
        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 << endl;
        cout << "Rezultats:" << " " << result << endl;
  
  funkcCal:
  	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;
  
  return 0;
}


Sorry the text is in Latvian, I will put the translations below.

Velaties izmantot aritmetikas vai funkciju kalkulatoru? == Do you wish to use arithmetical or function calculator?

funkciju kalkulators == Function calculator

Ievadiet izteiksmi: == Input the problem:

ievadiet funkciju: == Input the function:
Last edited 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.

Thank you very much!
it compiles, ...

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.)

Andy
Last edited on
what should I use instead of goto?
This is how far I got on that code, but it still doesnt work like it should, not by a long shot!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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;
}


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
Last edited on
Line 15, line 28: You did not correct the implied left hand side.

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

The two underlined expressions always evaluate to true (they are pointers), therefore the if statement always evaluates as true.

It should be:
 
if (o == "funkciju kalkulators" || o == "funkciju" || o == "oo")


omg I didn't even consider that. Thank you very much!
Instead of goto, use separate functions and just call the functions.
Topic archived. No new replies allowed.