No errors - No results either

EDIT: SOLVED!!
Disregard this post guys. I made a simple mistake. I will leave the post and add my solution to the bottom of the post, just in case any other noobs make the same mistake.
----------------------------------------------------------------

Hey guys. Absolute noob here. I spent a couple weeks reading a book on C++ in High School and got bogged down by some other stuff since, and haven't touched it in years. I started back up with it a few days ago, and have a new-found passion for it.I've been trying to push the limits of what I know before trying to overload myself with all the lessons. How else to do that than make a calculator like every other noob?

On to the question:
I already know how to build a basic calculator, where it asks you for the operation and then the two numbers, but I wanted to build something (a little) more advanced, and use my own brain (not any tutorials) to do it. I intended to just troubleshoot any errors as they cam along. Problem is, I'm not getting any errors. It's compiling just fine, but it's not yielding any results. Therefore, I'm not really sure where to start troubleshooting.


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
#include <iostream>
using namespace std;

float input, exinput, answer;
char sign;
void eq();

int main()
{
	cout << "standard calculator" << endl;
	cin >> input;
	cout << endl;
	cin >> sign;
	cout << endl;
	cin >> exinput;
	cout << endl;
	
	void eq();
	
	cin >> exinput;
	cout << endl;
	
	void eq();
	
	return 0;
	
}

void eq()
{
	if (sign == '+')
	{
		answer = input + exinput;
	}
	else if (sign == '-')
	{
		answer = input - exinput;
	}
	else if (sign == '*')
	{
		answer = input * exinput;
	}
	else if (sign == '/')
	{
		answer = input / exinput;
	}
	else
	{
		cout << "incorrect math symbol - try again";
	}
	
	cout << "= " << answer << endl;
	input = answer;
}


My intention is to eventually set up a loop, where it keeps letting you add, subtract, multiply, and divide, until a certain input is detected causing it to return 0. Before setting up the loop, however, I wanted to test it to see if worked before going any further.

I enter the first input, the sign, then the second input (exinput) and it doesn't yield any results. It's almost like it isn't calling on the 'eq' function. Did I use the function wrong? Was the IF statement where I messed up? Is it both? Or is it neither?
------------------------------------------------------

SOLUTION:

I made a simple mistake where I specified 'void' when calling on the function inside of main. This was unnecessary, as, all I needed to do was name the function to call on it.

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
#include <iostream>
using namespace std;

float input, exinput, answer;
string sign;
void eq();

int main()
{
	cout << "standard calculator" << endl;
	cin >> input;
	cout << endl;
	cin >> sign;
	cout << endl;
	cin >> exinput;
	cout << endl;
	
	eq(); //HERE! is where I removed the 'void'
	
	cin >> exinput;
	cout << endl;
	
	eq(); //HERE TOO
	
	return 0;
	
}

void eq()
{
	if (sign == "+")
	{
		answer = input + exinput;
	}
	else if (sign == "-")
	{
		answer = input - exinput;
	}
	else if (sign == "*")
	{
		answer = input * exinput;
	}
	else if (sign == "/")
	{
		answer = input / exinput;
	}
	else
	{
		cout << "incorrect math symbol - try again";
	}
	
	cout << "= " << answer << endl;
	input = answer;
}


Before now, I only ever mimiced the use of 'void', when it came to making functions for main to call upon. I now understand that 'void' prevents that section of code from running, which is also why it is used for functions that will be called upon from inside main, so that it will only run when called upon. By putting void in front of the call inside of main, I was telling it to NOT run it.
Last edited on
By putting void in front of the call inside of main, I was telling it to NOT run it.

By putting a return type in front of an identifier followed by parentheses, you were declaring rather than calling a function. Whether the return type was void or another type is immaterial, the result would've been the same.

Topic archived. No new replies allowed.