Skip to main content
GameDev.net gamedev.net
🔒 Locked

my c++ exercise

Started by iammfa Apr 4, 2009 at 4:43 PM 8 replies 1.6k views
Original Post
iammfa
iammfa
Hello, the following is my exercise which i trying learn in c++ tutorial, here is the exercise:

write a program that asks the user to input a real number n. Compute 3 n and output the result. Your program output should be formatted as follows:

Enter a real number: 7.12
7.12^3 = 360.944
Press any key to continue

here is my code:

#include <iostream>
#include <string>
using namespace std;

float main()
{
float n,a;
cout << "Enter a real number:";
cin >> n;
a=n^3;
cout << a << endl;
}



the compiler always giving me an error...! where is my wrong regards MFA
banana_get
banana_get
Your main function isn't returning a value. And it's suppose to be int main() because it returns an integer.
rip-off
rip-off
The operator "^" doesn't do what you think it does. It is a bitwise XOR operator. That may mean nothing to you yet, which isn't a problem. Try the function std::pow(), which is available in the include file .

Also, main() must be defined to return an "int". You don't actually need to use the return keyword explicitly in main.
iammfa
iammfa

when i change (float n,a) to (int n,a), the program run without errors, but when run the program and input the real number 7.12, the result = 4 ... !!!!!!
the exercise told me that the result must be (7.12^3 = 360.944)

this my new code:

#include <iostream>using namespace std;float main(){int n,a;cout << "Enter a real number:";cin >> n;a=n^3;cout << a << endl;}
GraphicsBas
GraphicsBas
The problem is that an integer is a natural number. You should use the floats instead. Also as already pointed out by someone else, the ^ operator is a XOR operator and not a power operator. The error you were getting probably states that you can't use the XOR operator on a float.

GBS
GBS
iammfa
iammfa
please, can you tell me the full code to study it
M2tM
M2tM
We've told you twice.

Here it is a third time:

do not use ^ to do a power operation, it is not a power operation, it will not do what you are expecting here. ^ performs an exclusive or (XOR) which is a bitwise operation.

use std::pow

Re-read rip-off's post, he outlined it well.

#include <iostream>#include <cmath>int main(){   float InputNumber, PowerResult;   std::cout << "Enter a real number: ";   std::cin >> InputNumber;   PowerResult = std::pow(InputNumber, 3.0f);   std::cout << std::endl << "Number to the power of 3: " << PowerResult << std::endl;}
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
iammfa
iammfa
worked well..
many thanks for helping me.

greeting
MFA
Joshuad
Joshuad
I think the thing that you weren't understanding was your return value of main(). As they had suggested, you cannot use float on main(), it returns either an int or it will return NULL (or void). In the case of your program, you will want it to return an int, because for the program to exit correctly, it will be returning 0. 0 will return saying that there were no errors, anything other than 0 will return that it was unsuccessful in its operation. This isn't true on all functions, but for main it is.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.