Hi and welcome to cplusplus :+)
With your code, the
^
doesn't do what you think it does. There is the
pow
function for which you need to include the math header file.When you are programming, always look at the reference material for the operators and functions etc that you want to use, rather than assuming you know how they work.
The reference material has example code in it. There is also a tutorial at the top left of this page.
Some other things that will help a bit:
It is good practice to always declare and initialise one variable per line. Initialise each variable to something, even if you use
scanf
on the next line, or assign a value to it on the next line. This might seem a little crazy, but quite often the biggest source of errors is uninitialised variables. Also, you can put a comment to explain what each variable is used for.
scanf
returns an int for the number of successfully read values, so you should always make use of this, to see that it worked. One has to be careful - try to think of what can go wrong, and write your code to catch errors and do something about them before they become a problem.
If you are going to calculate a series, you will need a loop. A
for
loop is good when you know how many terms you want to calculate. A
while
loop is good when you want to keep going until a term is less than a certain value, say.
Line 14 of your code isn't doing what you think it is either. You need to use
printf
to show the value.
You can return a value from a function, if you write one.
At the moment you are returning a value to the operating system. A value of zero indicates that the program ran successfully, other values means that it didn't. This is useful because one can use the success of one program to determine whether to run another program, as in this on the command line:
ProgA && ProgB
ProgB only runs if ProgA was successful.
I hope this helps a bit, don't be afraid to ask questions - there are plenty who can help.