Calculator Problem

I have no idea what is going on. Any help would be appreciated.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>

double mult(int a,int b){
a=a*b;
return a;
}

double add(int a, int b){
a=a+b;
return a;
}

double sub(int a, int b){
a=a-b;
return a;
}
double root(int a, int b){
a=sqrt(a);
return a;
}

double raise2power(int a, int b){
a=pow(a,b);
return a;
}

double divide(int a, int b) {
a=a/b;
return a;
}

int x =1;
int main() {
  
  if (x==1) {}
int a=0;
int b=0;
int x=0;
}
  if (x==0) {
  std::cout<<"A's value is "<<a<<".\n";
  }
  std::cout<<"Welcome to Charlie's Amazing Calculator!\n";
 
  std::string op;

  std::cout<<"Enter the value of the first number!\n";
  std::cin>>a;
  std::cout<<"Declare the operation!\nAddition,"; 
  std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nFactoral, !\nTerminate, #\n";
  std::cin>>op;
  std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
if (op=='+']){
add(a,b); }
if (op=="-") {
sub(a,b); }
if (op=="*") {
mult(a,b); }
if (op=="/") {
div(a,b); }
if (op=="^") {
raise2power(a,b); }
if (op=="~") {
root(a,b); }
if (op=="!") {
std::cout<<"not yet!"; }
}


Here is the error message
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
exit status 1
main.cpp:41:3: error: expected unqualified-id before 'if'
   if (x==0) {
   ^~
main.cpp:44:8: error: 'cout' in namespace 'std' does not name a type
   std::cout<<"Welcome to Charlie's Amazing Calculator!\n";
        ^~~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:61:18: note: 'std::cout' declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
main.cpp:48:8: error: 'cout' in namespace 'std' does not name a type
   std::cout<<"Enter the value of the first number!\n";
        ^~~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:61:18: note: 'std::cout' declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
main.cpp:49:8: error: 'cin' in namespace 'std' does not name a type
   std::cin>>a;
        ^~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:60:18: note: 'std::cin' declared here
   extern istream cin;  /// Linked to standard input
                  ^~~
main.cpp:50:8: error: 'cout' in namespace 'std' does not name a type
   std::cout<<"Declare the operation!\nAddition,";
        ^~~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:61:18: note: 'std::cout' declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
main.cpp:51:8: error: 'cout' in namespace 'std' does not name a type
   std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nFactoral, !\nTerminate, #\n";
        ^~~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:61:18: note: 'std::cout' declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
main.cpp:52:8: error: 'cin' in namespace 'std' does not name a type
   std::cin>>op;
        ^~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:60:18: note: 'std::cin' declared here
   extern istream cin;  /// Linked to standard input
                  ^~~
main.cpp:53:8: error: 'cout' in namespace 'std' does not name a type
   std::cout<<"Enter the magnitude of the operation!\n";
        ^~~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:61:18: note: 'std::cout' declared here
   extern ostream cout;  /// Linked to standard output
                  ^~~~
main.cpp:54:8: error: 'cin' in namespace 'std' does not name a type
   std::cin>>b;
        ^~~
In file included from main.cpp:1:
/usr/local/include/c++/8.1.0/iostream:60:18: note: 'std::cin' declared here
   extern istream cin;  /// Linked to standard input
                  ^~~
main.cpp:55:1: error: expected unqualified-id before 'if'
 if (op=='+']){
 ^~
main.cpp:57:1: error: expected unqualified-id before 'if'
 if (op=="-") {
 ^~
main.cpp:59:1: error: expected unqualified-id before 'if'
 if (op=="*") {
 ^~
main.cpp:61:1: error: expected unqualified-id before 'if'
 if (op=="/") {
 ^~
main.cpp:63:1: error: expected unqualified-id before 'if'
 if (op=="^") {
 ^~
main.cpp:65:1: error: expected unqualified-id before 'if'
 if (op=="~") {
 ^~
main.cpp:67:1: error: expected unqualified-id before 'if'
 if (op=="!") {
 ^~
main.cpp:69:1: error: expected declaration before '}' token
 }
 ^
   
 
Here's your entire main function:

1
2
3
4
5
6
7
int main() {
  
  if (x==1) {}
int a=0;
int b=0;
int x=0;
}


Everything after that (line 41 onwards) is outside of any function. This is bad news.
Last edited on
After a quick glance it appears that you have mismatched braces {}.

If you were using a meaningful indent style this problem would probably be much easier to spot.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cmath>

double mult(int a,int b)
{
    a=a*b;
    return a;
}

double add(int a, int b)
{
    a=a+b;
    return a;
}

double sub(int a, int b)
{
    a=a-b;
    return a;
}
double root(int a, int b)
{
    a=sqrt(a);
    return a;
}

double raise2power(int a, int b)
{
    a=pow(a,b);
    return a;
}

double divide(int a, int b)
{
    a=a/b;
    return a;
}

int x =1;
int main()
{

    if (x==1) {}
    int a=0;
    int b=0;
    int x=0;
}
if (x==0)
{
    std::cout<<"A's value is "<<a<<".\n";
}
std::cout<<"Welcome to Charlie's Amazing Calculator!\n";

std::string op;

std::cout<<"Enter the value of the first number!\n";
std::cin>>a;
std::cout<<"Declare the operation!\nAddition,";
std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nFactoral, !\nTerminate, #\n";
std::cin>>op;
std::cout<<"Enter the magnitude of the operation!\n";
std::cin>>b;
if (op=='+'])
{
    add(a,b);
}
if (op=="-")
{
sub(a,b);
}
if (op=="*")
{
mult(a,b);
}
if (op=="/")
{
div(a,b);
}
if (op=="^")
{
raise2power(a,b);
}
if (op=="~")
{
root(a,b);
}
if (op=="!")
{
std::cout<<"not yet!";
}
}


Do you see how everything after line 48 is right up against the margin? This is showing you where the problems start.

Thanks, silly mistake. Still doesn't work though.
While I'm at it, how do I pass variables in-between functions?

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>

double mult(int a,int b){
a=a*b;
return a;
}

double add(int a, int b){
a=a+b;
return a;
}

double sub(int a, int b){
a=a-b;
return a;
}
double root(int a, int b){
a=sqrt(a);
return a;
}

double raise2power(int a, int b){
a=pow(a,b);
return a;
}

double divide(int a, int b) {
a=a/b;
return a;
}

int x=1;
int main() {
  
if (x==1) {
int a=0;
int b=0;
int x=0;
}
  if (x==0) {
  std::cout<<"A's value is "<<a<<".\n";
  }
  std::cout<<"Welcome to Charlie's Amazing Calculator!\n";
 
  std::string op;

  std::cout<<"Enter the value of the first number!\n";
  std::cin>>a;
  std::cout<<"Declare the operation!\nAddition,"; 
  std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nFactoral, !\nTerminate, #\n";
  std::cin>>op;
  std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
if (op=='+']){
add(a,b); }
if (op=="-") {
sub(a,b); }
if (op=="*") {
mult(a,b); }
if (op=="/") {
div(a,b); }
if (op=="^") {
raise2power(a,b); }
if (op=="~") {
root(a,b); }
if (op=="!") {
std::cout<<"not yet!"; }
}
Last edited on
many problems,

you use std::string but you should be using char to test for the operator.

Also you call div but there is not function named div,you named it divide,

your main function ends before the majority of your code ends.

also when testing for a char use single ' '


fix them problems and your code should work.

how do I pass variables in-between functions?


you are already doing it,example is your mult() funtion.
Last edited on
Thanks that helped a lot adam!
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>

double mult(double a, double b){
a=a*b;
return a;
}

double add(double a, double b){
a=a+b;
return a;
}

double sub(double a, double b){
a=a-b;
return a;
}
double root(double a, double b){
a=sqrt(a);
return a;
}

double raise2power(double a, double b){
a=pow(a,b);
return a;
}

double div(double a, double b) {
a=a/b;
return a;
}

int x=1;
int main() {
  std::cout<<"Welcome to Charlie's Amazing Calculator!\n";  
    if (x==0) {   std::cout<<"A's value is "<<a<<".\n";
    }
    if (x==1) {
    double a=0;
    double b=0;
    double x=0;
    } 
  std::char op;
  std::cout<<"Enter the value of the first number!\n";
  std::cin>>a;
  std::cout<<"Declare the operation!\nAddition,"; 
  std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nFactoral, !\nTerminate, #\n";
  std::cin>>op;
  std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
    if (op=='+']){
    add(a,b); }
    if (op=='-') {
    sub(a,b); }
    if (op=='*') {
    mult(a,b); }
    if (op=='/') {
    div(a,b); }
    if (op=='^') {
    raise2power(a,b); }
    if (op=='~') {
    root(a,b); }
    if (op=='!') {
    std::cout<<"not yet!"; 
    }
}


Still errors though!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   
exit status 1
main.cpp: In function 'int main()':
main.cpp:36:47: error: 'a' was not declared in this scope
     if (x==0) {   std::cout<<"A's value is "<<a<<".\n";
                                               ^
main.cpp:43:8: error: expected unqualified-id before 'char'
   std::char op;
        ^~~~
main.cpp:45:13: error: 'a' was not declared in this scope
   std::cin>>a;
             ^
main.cpp:48:13: error: 'op' was not declared in this scope
   std::cin>>op;
             ^~
main.cpp:50:13: error: 'b' was not declared in this scope
   std::cin>>b;
             ^
main.cpp:51:16: error: expected ')' before ']' token
     if (op=='+']){
        ~       ^
                )
main.cpp:51:16: error: expected primary-expression before ']' token
   
Last edited on
if (op=='+'])

remove the ] before the )

it should just be char, char is not in the std:: namespace it is a predefined variable of the language.
Last edited on
Okay.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cmath>

double mult(double a, double b){
double sum=a*b;
return sum;
}

double add(double a, double b){
double sum=a+b;
return sum;
}

double sub(double a, double b){
double sum=a-b;
return sum;
}
double root(double a, double b){
double sum=sqrt(a);
return sum;
}

double pow(double a, double b){
double sum=pow(a,b);
return sum;
}

double div(double a, double b) {
double sum=a/b;
return sum;
}

void main2(double a, double b){
char op;
double sum;
    std::cout<<"A's value is "<<a<<".\n";
  std::cout<<"Declare the operation!\n";
  std::cin>>op;
  std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
    if (op=='+'){
      std::cout<<"add\n";
    add(a,b); }
    if (op=='-') {
      std::cout<<"sub\n";
    sub(a,b); }
    if (op=='*') {
      std::cout<<"mult\n";
    mult(a,b); }
    if (op=='/') {
      std::cout<<"div\n";
    div(a,b); }
    if (op=='^') {
      std::cout<<"pow\n";
    pow(a,b); }
    if (op=='~') {
    root(a,b); }
    if (op=='!') {
    std::cout<<"not yet!"; 
    }
    main2(a,0);
}
int main() {
    double a;
    std::cout<<"Welcome to Charlie's Amazing Calculator!\n\n";  
    std::cout<<"Here is a operation key!\nAddition,";
     std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nFactoral, !\nTerminate, #\n\n";
    std::cout<<"Enter the value of the first number!\n\n";
    std::cin>>a;
  main2(a,0);
}


It gives no errors but it... doesnt work. at all.
Last edited on
How do I take the returned sum and make it equal a and then run main2 again?
Here is how it would be done without functions.
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
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cmath>

void main2(double a, double b){
char op;
double sum;
    std::cout<<"A's value is "<<a<<".\n";
  std::cout<<"Declare the operation!\n";
  std::cin>>op;
    if (op=='+'){
      std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
    a=a+b;
    main2(a,0);
    }
    if (op=='-') {
      std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
      a=a-b;
      main2(a,0);
    }
    if (op=='*') {
      std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
     a=a*b; 
     main2(a,0);
    }
    if (op=='/') {
      std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
      a=a/b;
      main2(a,0);
    }
    if (op=='^') {
      std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b;
      a=pow(a,b);
      main2(a,0);
    }
    if (op=='~') {
      a=sqrt(a);
      main2(a,0);
     }
    if (op=='!') {
      a=0;
    main2(a,0);
    }
}
int main() {
    double a;
    std::cout<<"Welcome to Charlie's Amazing Calculator!\n\n";  
    std::cout<<"Here is a operation key!\nAddition,";
     std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nReset A, !\nTerminate, #\n\n";
    std::cout<<"Enter the value of the first number! (A)\n\n";
    std::cin>>a;
  main2(a,0);
}
How do I take the returned sum and make it equal a and then run main2 again?



well your functions such as mult,sub and add are returning doubles so all you would have to do for example is set

a = to add(5,5)

the function add will be called and the result returned by add will be stored in the variable a,

also naming conventions are important it is better that you have moved the calculations out of main but main2 is not a good name,maybe rename it calculation

run main2 again?


you need a while loop,also have a bool to check if quit = true,if quit = true,break out of the loop


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

int main() {

    
    double a;
    bool quit = false;

    while(!quit){

    std::cout<<"Welcome to Charlie's Amazing Calculator!\n\n";  
    std::cout<<"Here is a operation key!\nAddition,";
     std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, \nPower, ^\nSquare Root, ~\nReset A, !\nTerminate, #\n\n";
    std::cout<<"Enter the value of the first number! (A)\n\n";
    std::cin>>a;
    if(a == '!')
      quit = true;

    main2(a,0);
    

   }
}



that is just an example,the only problem with my example is that main2(a,0) will still be run even if you enter '!' so a better idea in this case is check for '!' before main2 is called and break out of the loop if a = '!'

Last edited on
Thank you! Works perfectly now!

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cmath>

double add(double a, double b){
double sum=a+b;
return sum; }

double sub(double a, double b){
double sum=a-b;
return sum; }

double mult(double a, double b){
double sum=a*b;
return sum; }

double div(double a,double b) {
double sum=a/b;
return sum; }

double pow(double a, double b){
double sum=pow(a,b);
return sum; }

double root(double a){
double sum=sqrt(a);
return sum; }

double new_a(double a) {
std::cout<<"Enter A's new value!\n";
std::cin>>a; 
return(a); }

void Calc(double a){
double b;
char op;
std::cout<<"A's value is "<<a<<".\n";
std::cout<<"Declare the operation!\n";
std::cin>>op;
  if(op=='+'||op=='-'||op=='/'||op=='*'||op=='^') {
  std::cout<<"Enter the magnitude of the operation!\n";
  std::cin>>b; }

  if (op=='+'){
  a=add(a,b); 
  Calc(a); }

  if (op=='-') {
  a=sub(a,b); 
  Calc(a); }

  if (op=='*') {
  a=mult(a,b);
  Calc(a); }

  if (op=='/') {
  a=div(a,b);
  Calc(a); }

  if (op=='^') {
  a=pow(a,b);
  Calc(a); }

  if (op=='~') {
  a=root(a);
  Calc(a); }

  if (op=='!') {
  a=new_a(a);
  Calc(a); }

  if (op=='#') {
  std::cout<<"Shutting down...\n";  }

  if (op!='+'||op!='-'||op!='*'||op!='/'||op!='^'||op!='~'||op!='!'||op!='#') {
  std::cout<<"That is not an allowed input!\n";
  Calc(a); } }

int main() {
double a;
std::cout<<"Welcome to Charlie's Amazing Calculator!\n\n";  
std::cout<<"Here is a operation key!\nAddition, ";
std::cout<<"+\nSubtraction, -\nMultiplication, *\nDivision, /\nPower, ^\nSquare Root, ~\nReset A, !\nTerminate, #\n\n";
std::cout<<"Enter the value of the first number! (A)\n";
std::cin>>a;
Calc(a); }
Last edited on
Topic archived. No new replies allowed.