'main' identifier not found.

#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int firstadd,secondadd,initialsubamount, deductor, firstmult, secondmult, quotient, sum, product, difference;
double dividend, divisor;
string answer3;
int exit2;
int loop;
int exit()
{
cout<< "would you like to end the program?\n";
cin>> answer3;
if (answer3=="yes" || answer3=="yep" || answer3=="okay")
{
cout<< "Okay Goodbye";
}
else
{
cout<< "okay program will not terminate\n";
loop=main(); // ERROR IS RIGHT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
return 0;
}
int main()
{
string answer;
string answer2;
string and;
cout<<"This is a calculator to add, subtract, multiply, or divide two numbers. Would you like to add subtract multiply or divide?\n";
cin>> answer;
if (answer=="Subtract" || answer=="subtract")
{
cout<<"Okay you would like to " << answer << "\n" << "Enter the inital amount then the amount you want to subtract from the initial amount\n";
cin>> initialsubamount>> and >>deductor;
int difference=initialsubamount - deductor;
cout<< difference <<endl;
}
else if (answer=="multiply"||answer=="Multiply")
{
cout<<"okay you would like to " << answer <<"\n" << "enter a number then the number you would like to multiply it by\n";
cin>> firstmult >> and >>secondmult;
signed product=firstmult * secondmult;
cout<< product <<endl;
}
else if (answer=="divide" || answer=="Divide")
{
cout<<"okay you would like to " << answer <<"\n" << "enter the inital value then the amount you want to divide from it\n";
cin>> dividend>>and>>divisor;
double quotient = (dividend / divisor);
cout<< quotient <<endl;
}
else if (answer=="add" || answer=="Add")
{
cout<<"okay you would like to add\n" << "enter two numbers that you want to add\n";
cin>> firstadd >> and >> secondadd;
signed sum=firstadd + secondadd;
cout<< sum << " is your answer"<<endl;
}
else
{
exit2=exit();
return 0;
}
cout<<"would you like to do another calculation?\n";
cin>>answer2;
if (answer2=="yes")
{
cout<<"okay the calculator will rerun\n";
loop=main();
}
else
{
exit2=exit();
return 0;
}
}
Last edited on
Holy code tags batman. Be surprised that someone actually responded, because looking at something that didn't use code tags is brutal.

The primary mistake you are making is that you cannot call main(). main() is called once and only once by the [operating system?] as the entry point to your program.

The second mistake is that on line 25, you call a function without declaring it above the first usage in the file. You'd need to declare that function beforehand. It's main() so this wouldn't work anyways, but in general that's a problem.

Your final mistake is on line 77 where you try and call main from main() giving it a recursive flavor. main() cannot be called. Use an infinite loop loop instead and break if a condition is true.

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
#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int firstadd,secondadd,initialsubamount, deductor, firstmult, secondmult, quotient, sum, product, difference; 
double dividend, divisor;
string answer3;
int exit2;
int loop;

int exit()
{
	cout<< "would you like to end the program?\n";
	cin>> answer3;
	if (answer3=="yes" || answer3=="yep" || answer3=="okay")
	{
		cout<< "Okay Goodbye";
	}
	else 
	{	
		cout<< "okay program will not terminate\n";
		loop=main(); // ERROR IS RIGHT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	}
	return 0;
}
int main()
{	
	string answer;
	string answer2;
	string and;
	cout<<"This is a calculator to add, subtract, multiply, or divide two numbers. Would you like to add subtract multiply or divide?\n";
	cin>> answer;

	if (answer=="Subtract" || answer=="subtract")
	{ 
		cout<<"Okay you would like to " << answer << "\n" << "Enter the inital amount then the amount you want to subtract from the initial amount\n";
		cin>> initialsubamount>> and >>deductor;
		int difference=initialsubamount - deductor;
		cout<< difference <<endl;
	}	
	else if (answer=="multiply"||answer=="Multiply")
	{
		cout<<"okay you would like to " << answer <<"\n" << "enter a number then the number you would like to multiply it by\n";
		cin>> firstmult >> and >>secondmult;
		signed product=firstmult * secondmult;
		cout<< product <<endl;
	}
	else if (answer=="divide" || answer=="Divide")
	{
		cout<<"okay you would like to " << answer <<"\n" << "enter the inital value then the amount you want to divide from it\n";
		cin>> dividend>>and>>divisor;
		double quotient = (dividend / divisor);
		cout<< quotient <<endl;
	}
	else if (answer=="add" || answer=="Add")
	{
		cout<<"okay you would like to add\n" << "enter two numbers that you want to add\n";
		cin>> firstadd >> and >> secondadd; 
		signed sum=firstadd + secondadd;
		cout<< sum << " is your answer"<<endl;
	}
	else
	{
		exit2=exit();
		return 0;
	}

	cout<<"would you like to do another calculation?\n";
	cin>>answer2;
	
	if (answer2=="yes")
	{
		cout<<"okay the calculator will rerun\n";
		loop=main();
	}
	else
	{
		exit2=exit();
		return 0;
	}
}
man ur code so hard to read ...

basically
loop=main(); // u cannnot do this..... dont even know what u are trying to do here.


you already have

int main(){

}


clean ur stuff up... like this

#includes

int if, you, have , alot, of, variables; // declare variables
int use,another,line;

void myFunction(); // prototype your fucntions above main
void anotherFunction() // prototyping another function if i have one.

int main() // call your functions here..
{
myFunction();

}

void myFunction // this is what your function does
{
1+3+4;
}

void anotherFunction // defining what another function does.
{
your code here blah blah

}
Last edited on
You can't call main. Main is the entry point. Only the OS can call main. If you need to loop to the beginning, then enclose the body within a for/next, while or do/while.
+1 Roberts for suggesting the use of loops.

Wazzak
okay I have no Idea what nooblet said I dont even know what void is. and yeah im new to calling functions. you guys said you cant call main, but it works when I call it at the end.
but it works when I call it at the end.


It's horribly broken. It doesn't go back to the beginning - it creates a whole new stack frame (which you can think of as a section of memory that "is" the function you've just called) for the new function, main, and then when it gets to the end again and you call main again, it does it again, so now you've got THREE main functions set up in memory, each one with their own values, and then again and again and again and again..... until eventually you run out of space on the stack and the program crashes.

Of course, let's say that at the end of one of those main functions, you don't repeat calling you main and the function does actually return. Does the program end? No! It goes back to the previous main, and carries on from there, and then has to unwind that, and then the next one, and the next and the next and the next... oh, what a mess :( All for want of a proper loop.
Last edited on
haha alright I just wanted to call functions in the midst of it. They were new and fun :). I will go back to tutorials :O
Topic archived. No new replies allowed.