I'm just learning C++, coming out of python3 and QBASIC, and am having a very hard time reading the compiler errors and understanding them, making it difficult to debug.
The problem I am having is that I keep pulling the compilation-error:
error: expected unqualified-id
This occurs on the 10th and 18th line.
I am trying to compile this program using linuxs' g++:
g++ proto.cpp -o prototype
The code for the program is below.
#include <iostream>
#include <string>
using namespace std;
//Declaring Functions
//Trouble Function
int mult ( double x, double y );
{
return x * y;
}
//Trouble Function
int dive ( double x, double y );
{
if ( y == 0 )
{
cout<<"Error, cannot divide by zero.\n";
return;
}
else
{
return x / y;
}
}
//This error doesn't occur beyond this point.
int plus ( double x, double y );
{
return x + y;
}
int min ( double x, double y );
{
return x - y;
}
//End of global declarations.
//I would have made them local functions if not
//for an entirely set of unrelated problems.
int main()
{
cout<<"Please enter two numbers.\n"<<"\n";
int num1;
int num2;
cin>>num1;
cin>>num2;
string returnz = "<unknown>";
while ( returnz != "no" )
{
cout<<"What would you like to do with the numbers>\n";
cout<<'\n'<<"Enter ( mult ) to multiply, ( min ) to subtract, ( plus ) to add, and ( dive ) to divide.\n";
getline( cin, returnz, '\n' );
if ( returnz == "mult" )
{
double result = mult ( num1, num2 );
cout<<num1<<" * "<<num2<<" = "<<result<<"\n";
continue;
}
else if ( returnz == "dive" )
{
double rest = dive ( num1, num2 );
cout<<num1<<" / "<<num2<<" = "<<rest<<"\n";
continue;
}
else if ( returnz == "plus" )
{
double res = plus ( num1, num2 );
cout<<num1<<" + "<<num2<<" = "<<res<<"\n";
continue;
}
else if ( returnz == "min" )
{
double re = min ( num1, num2 );
cout<<num1<<" - "<<num2<<" = "<<re<<"\n";
continue;
}
else
{
break;
}
}
}
The goal is to allow the user to enter a couple numbers and then give them the option to use specified operators on the number.
Note, I am new to this language so it is probably riddled with syntax errors and inconsistencies. The question though is, why does the unqualified-id get pulled for these two, (but not the others), what does it mean, and how would one go about fixing this.
I ask here because I'm trying to learn this independently, therefore I have no instructor or peers to turn too. Advice as to how I could have done this in a more effective code is also welcome and would be much appreciated.
Thank you.
Errors as they appear in terminal:
proto.cpp:10:1: error: expected unqualified-id before ‘{’ token
{
^
proto.cpp:18:1: error: expected unqualified-id before ‘{’ token
{
^
As stated by @user657267 get rid of the semicolons when declaring a function and its implementation. If you were to have
int some_function(int a, int b);
above the main and the implementation of that function below the main
int some_function(int a, int b) {
//something happens here
return a;
}
That would be ok. The implementation can also go above the main and then you don't have to write the first line defining the function. The reason the definition or implementation has to be above the main is c, or c++ for that matter, won't be able to see the function otherwise which will throw and error as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With