Super Noob question about c++ initializer

i am stuck on how to fix this code, it was my first real attempt at trying to make a program without some sample code. and i got stuck on the darn compiler lol

the error was untitled.cxx:7:1: error: expected initializer before ‘int’

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
#include <iostream>

using namespace std;

void exponent()

int main()
{
	int base;
	int answer;
	cin>>base;
	if(base<=0)
	{
		cout<<"sorry that was an invalid input, please input a positive number..."<<endl;
	}
	return 0;
}

void exponent(int base, int answer)
{
	cout<<"I am going to square the number you are going to input!"
	cout<<" "<<endl;
	cout<<"What is your base number?"<<endl;
	cin>>base>>endl;
	cout<<"Are you sure you want "<<base<<" for a base number?"<<endl;
	cout<<" "<<endl;
	cout<<"Just so you know, you typed "<<base<<" for a base number."<<endl;
	
	base * base = answer;
	
	cout<<"Your input to the second power is "<<answer<<" isn't it!!!"<<endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
// ...
using namespace std;

void exponent() ; // the terminator ie. ; was missing here

int main() // this is line 7 - 7:1: error: expected initializer before ‘int’
// 7:1 indicates error at line 7 col 1
{
// .. 

idk, I tried putting a ; at the end of the exponent function, but i get a ton of more errors, do i need to put anything in the parenthesis such as...


void exponent( //insert important missing info// )


what is an initializer?

and thanks for the post dude :)
I mean, it seemed to have worked fine with floats in this example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

void add(float, float);
void sub(float, float);
void mult(float, float);
void divd(float, float);

int main()
{
	cout<<"Please input a simple math problem: ";
	float a;
	float b;
	char oper;
	cin>>a>>oper>>b;
	
	switch(oper)
	{
		case '+':
		add(a, b);

/*END OF SAMPLE CODE*/


I didn't see any special initializer before int main()
> what is an initializer?

An initializer is something that specifies an initial value for the identifier being defined.

In int a_number = 99 * 6 ; = 99 * 6 is the initializer.


> do i need to put anything in the parenthesis such as...

Yes.

1
2
3
4
5
6
7
8
9
10
11
12
void exponent(int base, int answer) ; // declaration

int main()
{
    // ....
}

void exponent(int base, int answer) // no ; here - the definition  follows
{ // definition
	cout<<"I am going to square the number you are going to input!"
       // ... other statements
}






Last edited on
thank you very much for the definition of initializer, it makes so much sense lol, but i don't expect you guys to compile the code or anything but if i but a ; after
 
void exponent(int base, int answer)

then i end up with a ton of iostream errors.

any idea?

Thanks for the contributions JLBorges
wait, i can't believe i didn't see this,

http://www.cplusplus.com/doc/tutorial/functions/

I can just make it int instead of void and call it like in that example. The original intention of the program was to make it without sample code but i think i understand what i need and hey, i learned something...

//close the thread lol//
> then i end up with a ton of iostream errors.

1
2
3
4
5
6
void exponent(int base, int answer)
{
        // ....
	cin>>base>>endl;// *** the error is on this line
        // ....
}


Now try to figure out why that is an error.

Hint: what does << endl; do?
http://www.cplusplus.com/reference/iostream/manipulators/endl/
oh. my. god...


why can't the compiler just say take away the >>endl; and if you want to use endl; you gotta use it when you're "couting" and not "cinning?" lol thanks a ton!
> why can't the compiler just say take away the >>endl;

It would hane told you that. Something which means:
at line xxx col yyy in your program there is no match for operator>> ..
.

Tip: Look at the line number and column number in the diagnostic. The error would be there or thereabouts.
Topic archived. No new replies allowed.