looks fine, and if it runs and works then you're sorted. just as a matter of interest, what compiler are you using? system isn't normally in stdio.h. its not a big deal, just curiosity
looks fine, and if it runs and works then you're sorted. just as a matter of interest, what compiler are you using? system isn't normally in stdio.h. its not a big deal, just curiosity
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
Some fixes.Code:#include <stdio.h> #include <math.h> int main(void) { char string[256]; long int a,b; printf( "Please enter your name:\t " ); fgets ( string, 256, stdin ); printf("Hello, %s\n", string); printf("You will enter two numbers, and I will raise the first number\n"); printf("to the power of the second number!\n\n"); printf("Enter the first number.\t"); scanf("%ld", &a); printf("Enter the second number, which will be the exponent.\t"); scanf("%ld", &b); printf("Your total is %ld\n", (long int)pow(a,b)); return 0; }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I am using the bloodshed dev whatever compiler. And the reason I have the beginning wrong is because once again, my teacher wants us to use that format. And for the return 0, my compiler will not pause the program unless I use that code.
Edit:
I'm sorry, it will pause the program, but my teacher wants us to "Press any key to end the program." The way I did it before (using getchar( )) would only end the program if I pressed Enter. The code I use now seems to be the easiest way to do that.
Fine. I'm in the habit of not perpetuating bad habits. It's habit forming and bad.Originally Posted by kabuatama
Pause before the return?Originally Posted by kabuatama
Fine. When you get out of the class, remember to ditch this bad habit.Originally Posted by kabuatama
-----
Learn wrong. [school and bad books]
Unlearn. [real life]
Relearn correctly. [real life again]
I wish they'd skip the first one or two steps and just teach correctly.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
> my teacher wants us to use that format
Who's paying who?
As Dave has pointed out, if you think learning is hard, wait until you have to unlearn it.
Your compiler is based on gcc, so you should be able to add these compiler options to get a lot more information about your code.
Code:$ gcc -W -Wall -ansi -pedantic -O2 foo.c -lm foo.c:7: warning: return type of ‘main’ is not ‘int’ foo.c: In function ‘main’: foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’ foo.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’ foo.c:29: warning: implicit declaration of function ‘system’
If so I would think that your teacher wants you to compute a to the power of b by yourself using long ints. like thisOriginally Posted by kabuatama
checking for b < 1 needs to be added.Code:res = a; for ( ; b > 1; --b ) res *= a; printf("Your total is %ld\n", res );
Kurt
is the int necessary? I am really sorry for asking all these questions, but it is difficult just going by the book, and I thank everyone for taking the time to help me.The following are all exactly the same:put the int in for safety - standards and all that.
Take your pick. I use just plain long.Code:long int x; int long x; long x; signed long x; signed int long x; signed long int x; long signed int x; long int signed x; int long signed x; int signed long x;
It's the same with shorts. You can have the int keyword before the short, after, or absent.
And the order of keywords doesn't matter. So you can do things like this:
->Code:extern const long long int ll;
Code:long const int extern long ll;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
This is indicating that you need to include <stdlib.h> for system()'s prototype.Code:foo.c:29: warning: implicit declaration of function ‘system’
In Dev-C++: Options->Compiler options (or something similar), and add
into the "Extra compiler options" textbox.Code:-W -Wall -ansi -pedantic -O2
Note that older versions of Dev-C++ find several thousand warnings in <stdio.h> and other standard header files. If that happens, use just this:
Code:-W -Wall -O2
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I guess it's worth noting that when you use type 'long' with pow you can get inaccurate results. For example pow(5, 3) is likely to be displayed as 124. But hey, at least you know the general reason of why that could happen.
p.s. actually when casting the result to long.
Regards,
Brian
Last edited by Br5an; 01-28-2006 at 01:22 PM.
Only if you don't include <math.h>.For example pow(5, 3) is likely to be displayed as 124.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.