-
Just starting out!
hi there, I have started to go through the tourtorials so far i have got up to the case section and decided to try and make a simple form of calculator i suspose you could call it. To see if i could still remember the stuff from the earlier tutorials. so far i have written the coding below but when i try to compile i get this error
" line 26 error: expected initializer before "int"
Code:
#include <iostream>
using namespace std;
int input;
int mult ( int x, int y);
int main()
{
int x;
int y;
int main();
{
cout<<"enter two numbers (sperated by a space): ";
cin>> x >> y;
cin.ignore ();
cout<<"the answer is "<< mult ( x, y ) <<"\n";
cin.get();
}
int mult ( int x, int y )
int input;
cout<<"1. multiply\n";
cout<<"2. devide\n";
cout<<"3. subtract\n";
cout<<"4. add\n";
cout<<"Selection: ";
cin>>input;
switch ( input ) {
case 1:
{
return x * y;
}
break;
case 2:
{
return x / y;
}
break;
case 3:
{
return x - y;
}
break;
case 4:
{
return x + y;
}
break;
default:
cout<<"Error, bad input, quitting\n";
break;
}
}
the code is a combonation of the tutorial and what i made my self. as far as i know i could be a millons miles off the actual code i need. any help would be greatly accepted so that i know how to avoid the error in the future as well.
-
>int main()
>{
> int x;
> int y;
>
> int main();
>{
You've got main() defined twice.
>int mult ( int x, int y )
You left out the opening brace for this function.
-
And there's no need to declare variable input as a global on the line which follows using namespace std;
-
Get rid of
i think that
Code:
int mult ( int x, int y )
is also wrong...
I think you need
Code:
int mult ( int x, int y ){ 'code }
-
I think you also need to define mult function
Code:
int mult ( int x, int y ){ 'code to do what ever function will do }
because you got prototype, but it is not correctly defined anywhere
-
I'm just going to fix formatting issues here and let you deal with the code as needs be. Compare to your original code so you don't make these same mistakes again.
Code:
#include <iostream>
using namespace std;
int input;
int mult ( int x, int y);
int main()
{
int x;
int y;
// You had an extra declaration of main here. Too much copy and paste, I think.
cout<<"enter two numbers (seperated by a space): ";
cin>> x >> y;
cin.ignore ();
cout<<"the answer is "<< mult ( x, y ) <<"\n";
cin.get();
}
int mult ( int x, int y )
{
int input;
cout<<"1. multiply\n";
cout<<"2. devide\n";
cout<<"3. subtract\n";
cout<<"4. add\n";
cout<<"Selection: ";
cin>>input;
switch ( input ) { // The brackets in every one of your case statements...
case 1: // ... didn't do anything (good or bad) in this case...
return x * y; // ... so I removed them. In switch statements it's the
break; // ... break statement that's important.
case 2:
return x / y;
break;
case 3:
return x - y;
break;
case 4:
return x + y;
break;
default:
cout<<"Error, bad input, quitting\n";
} // of course you do need brackets around the whole case statement.
}
There are still alot of issues with the code, but this should get you to compiling it.