943,816 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4241
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 12th, 2004
0

Uber-N00B question

Expand Post »
First off I'd like to say hailz to you all! I'm your newest newbie trying to get a good grasp of programming C/C++/C#. Unfortunetly, I've got a loooong ways to go. With your help, I know I can make it.

My first question is rather tricky.... well... for me...

I'm using Mac OS X 10.2.6 on Project Builder, just doing Foundation C, but when I use scanf, it never seems to work quite right...

http://uploader.unrealboards.com/upl...canf probs.jpg

So my question is: why in the world is it asking for the scanf values before displaying my printf functions? How do I correct this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SeijuroHiko is offline Offline
7 posts
since Jun 2004
Jun 12th, 2004
0

Re: Uber-N00B question

get a PC!!!
Reputation Points: 13
Solved Threads: 0
Posting Whiz in Training
marceta is offline Offline
217 posts
since May 2004
Jun 12th, 2004
0

Re: Uber-N00B question

:rolleyes:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SeijuroHiko is offline Offline
7 posts
since Jun 2004
Jun 12th, 2004
0

Re: Uber-N00B question

Please only offer helpful suggestions. I'm confused - when you run the program, the 2 and 5 are printed to the top of the screen automatically? Or did you enter them, or?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jun 12th, 2004
0

Re: Uber-N00B question

he is saying that he entered them and it asks for them when he starts instead of after the print out. let me try my hand with the code for a bit and ill see if i can help out. (not likely but ill try )
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 12th, 2004
0

Re: Uber-N00B question

C++ Syntax (Toggle Plain Text)
  1. /////////////////////////////////////////////////////
  2. ///program that takes one value a and places/////////
  3. ///it to the power of another value b, defining//////
  4. ///the power function by c///////////////////////////
  5. /////////////////////////////////////////////////////
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. int main()
  10. {
  11. int a; // defining the integer a which will be to the power of b
  12. int b; // defining the integer b which will be the integer being raised to some power
  13. int c; // defines the power function used to put a to the power of b
  14.  
  15.  
  16. printf("Welcome\nThis Program takes user-defined numbers\n(a and b respectivly), and puts 'a' to the power of 'b'\nPlease enter your value of 'a' below:\n");
  17. scanf("%i", &a);
  18. printf("\n\nGood, now enter your value for 'b':\n");
  19. scanf("%i", &b);
  20. printf("\n\nCOMPUTING");
  21. c = pow(a,b);
  22. printf("\n\nThe value of %i^%i is %i", a, b, c);
  23. return 0;
  24. }

ok so heres what i wrote and it works perfectly fine, you have to have some kind of pause or break between the scanf and c=pow or else it will give you 1^12345678..... and so and so forth. also you should comment your work more so its easier to read, its a good habbit to get into, and it will pay off in the long run.

try not setting your variables to anything IE dont set int a to anything if you know that your going to define it later in the code.

also, its good practice to space your code out so that it is readable.

IE x = "some value"
Not
x="somevalue"

its easier to read the first one over the second one.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 12th, 2004
0

Re: Uber-N00B question

Well, I plugged in your code, Killer_Typo, and it's still doing the exact same thing... it won't come up with anything until the 2 variables are defined by the user. I tried using breakpoints, but my newbie butt doesn't even know what those are for, so of course it doesn't work... how do you implement a break or a pause?

Sorry the code is sloppy, I just cranked it out real quick having fun and testing what *little* I know. I wasn't planing on having to post it online to get it fixed...

Man this compiler is weird! Talk about non-linear thinking!

When my programs go beyond 25-30 lines, I'll start noting the code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SeijuroHiko is offline Offline
7 posts
since Jun 2004
Jun 13th, 2004
0

Re: Uber-N00B question

i didnt have any break points, the code up there is exactly what i compiled and ran, and it worked great. are you running this from the command prompt? or macs version of it at least? i use windows so im not sure what your problem with it is. maybe someone with a mac can help you better.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 13th, 2004
0

Re: Uber-N00B question

Hey I think you guys are barking up the wrong tree.

scanf() works with options you sepecify.By options I mean all the stuff you specify in the " ".So it also means you can have multiple options too.

C++ Syntax (Toggle Plain Text)
  1. some options
  2. -------------
  3. %s string (char *)
  4. %d int
  5. %f float

you can also use scanf() like this ie how to specify multiple options

C++ Syntax (Toggle Plain Text)
  1. scanf("%d,%f",int_val,float_val);

Helps???
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 13th, 2004
0

Re: Uber-N00B question

Ah, yes, scanf. Trouble is almost guaranteed until you decide to quit using it.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Advantages of C++ SAFEARRAY: Explain
Next Thread in C++ Forum Timeline: NOOB question , graphical tiles





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC