I know how to do rationals, my problem is with the actually code. I've been messing around with everyone's suggestions and ideas. Still no luck.
Printable View
I know how to do rationals, my problem is with the actually code. I've been messing around with everyone's suggestions and ideas. Still no luck.
Besides the problems you are having with the mechanics of compiling and running programs, you need to review some basics on how to declare, define, and call functions. Maybe the following will help.
Here is an example of a very simple function:
All functions require a return type. In the display() function above, the return type is 'void', which stands for 'nada', 'nothing', 'zip'. That means, the function doesn't return anything. Functions also can have things called 'parameters'. The parameter in the function above is 'int n'. That means the function is expecting any integer to be sent to it. The integer value that is sent to the function will be stored in the variable 'n'.Code:void display(int n)
{
cout<<n<<endl;
}
Here is what the function looks like as part of a program:
In main(), the function is called by this line:Code:#include <iostream> //contains cout
using namespace std;
//makes it so you don't have to write 'std::cout'
//instead you can just write 'cout'
//function:
void display(int n)
{
cout<<n<<endl;
}
int main()
{
int num = 10;
display(num);
return 0;
}
display(num);
That instructs the program to send the value contained in the variable 'num' to the function named display(), and then execute the code in the function. Since num is equal to 10, 10 is sent to the display() function. The value 10 is then stored in the variable 'n', which is the function parameter. Then, the code in the function is executed. After the last line in the function is executed, execution returns to the place in main() where the function was called, and then the statements thereafter in main() are executed.
Here is another example of a function:
This time the return type is double(a double is a number with a decimal point like 2.5). The return type has to be specified as 'double' because the function returns a double value in this line:Code:double multiplyBy3(int x)
{
double temp = x * 3.0;
return temp;
}
return temp;
The multiplyBy3() function also has an int parameter, so it must be sent an int.
Adding the multiplyBy3() function to the previous program looks like this:
The function multiplyBy3() is called in main() by this line:Code:#include <iostream> //contains cout
using namespace std;
//makes it so you don't have to write 'std::cout'
//instead you can just write 'cout'
double multiplyBy3(int x)
{
double temp = x * 3.0;
return temp;
}
void display(int n)
{
cout<<n<<endl;
}
int main()
{
int num = 10;
display(num);
int number = 5;
double result = multiplyBy3(number);
cout<<result<<endl;
return 0;
}
double result = multiplyBy3(number);
The function is sent the int value stored in number. Since number is equal to 5, 5 is sent to the multiplyBy3 function, and then 5 is stored in the variable 'x', which is the function parameter. Then, the code in the function is executed. When the return statment is executed:
return temp;
the function gets the value in temp, and sends it back to main(). Since temp equals 15.0, 15.0 is sent back to main(). When a function returns a value, the function call is replaced by the return value. So, this line in main():
double result = multiplyBy3(number);
becomes:
double result = 15.0;
Then, the line:
cout<<result<<endl;
displays the return value. Note that you cannot do this to display 'result':
display(result);
The display() function requires an int to be sent to it, not a double, and result is type double. Therefore, you would get an error if you tried that: the types have to match.
My copy paste technique is fine thank you very much... I'm using g++... I've been messing around with that program and all it ever does it output 0. It's like it never gets to int main()Quote:
Originally Posted by 7stud
because it doesn't output:
0
0
it outputs just
0
Also with the *= I'm trying to multiply a numerator that is part of the rational class by a normal intger for example 8
So what I'm trying to do is this:
third(rational class) *= 8;
Perhaps you're calling some old program?
I fixed my problem, thanks everyone for your patience and help.