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... :sad:

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...

[IMG]http://uploader.unrealboards.com/uploads22/scanf probs.jpg[/IMG]

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?

get a PC!!!

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?

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 :))

/////////////////////////////////////////////////////
///program that takes one value a and places/////////
///it to the power of another value b, defining//////
///the power function by c///////////////////////////
/////////////////////////////////////////////////////
 
#include <stdio.h>
#include <math.h>
int main()
{
	  int a;   // defining the integer a which will be to the power of b
	  int b;   // defining the integer b which will be the integer being raised to some power
	  int c;   // defines the power function used to put a to the power of b
 
 
	  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");
	  scanf("%i", &a);
	  printf("\n\nGood, now enter your value for 'b':\n");
		 scanf("%i", &b);
	  printf("\n\nCOMPUTING");
		  c = pow(a,b);
	  printf("\n\nThe value of %i^%i is %i", a, b, c);
		 return 0;
}

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.

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.

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.

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.

some options
-------------
%s       string (char *)
%d       int
%f       float

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

scanf("%d,%f",int_val,float_val);

Helps???

Ah, yes, scanf. Trouble is almost guaranteed until you decide to quit using it.

Dave, I wish I knew that going into this... *sigh*

FireNet- This is what I came up with:

///////////////////////////////////////////////////////////////////////
/////////////////////  Simple Application here:  //////////////////////
///////  Allz we're doing is asking the user for 2 variables,  ////////
////////  a and b, put a to the power of b, and return that to ////////
///////////  c for display. Code should be self-explanitory.  /////////
///////////////////////////////////////////////////////////////////////


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    int a;
    int b;
    int c; 
    
    printf("CAN YOU FEEL THE POWER???\nJust enter 2 variables (a and b respectively)\nThis program will calculate a^b and return the result.\nEnter your to variables below:\n"); //He feels the pwer! ^_^
    
    scanf("%d, %d", &a, &b); //Hope this works...
    
    c = pow(a, b); // The one line of actual math...
    
    printf("%i^%i=%i", a, b, c);  //Display the values as a^b=c
    
    return 0;
}

It's still doing the same thing, only now, when I enter '5', it goes right through the math somehow getting '4' for 'b'. When I enter '7' for 'a', '4' for 'b' is still there...

Knowing me, I probably implemented the code wrong...

One of the recommendations, if you followed the link I previously posted, was to use fgets and sscanf. For example,

#include <stdio.h>
#include <math.h>

int main(void) 
{
   double a, b, c; 
   char buffer [ BUFSIZ ];

   puts("CAN YOU FEEL THE POWER???\n"
		"Just enter 2 variables (a and b respectively)\n"
		"This program will calculate a^b and return the result.\n"
		"Enter your to variables below:");
   fflush(stdout);

   if ( fgets(buffer, sizeof buffer, stdin) )
   {
	  if ( sscanf(buffer, "%lf%lf", &a, &b) == 2 )
	  {
		 c = pow(a, b);
		 printf("%g ^ %g = %g\n", a, b, c);
	  }
   }

   return 0;
}

/* mu output
CAN YOU FEEL THE POWER???
Just enter 2 variables (a and b respectively)
This program will calculate a^b and return the result.
Enter your to variables below:
5 4
5 ^ 4 = 625
*/

Apologies, Dave. I didn't realize that was a link.

I'll plug that in later tonight (IMPORT RACES!!! ^_^ ) and let ya know how it works... looks good from here!

Thanks for the help, Dave.

OK! that's it... I give up... I'd never use scanf/sscanf anyways, so screw it.

Dave, you got pretty close with your idea.

[IMG]http://uploader.unrealboards.com/uploads22/scanf sucks.tiff[/IMG]

I tried about 3 or 4 variations of this code, but none got me really any closer...

Thanks for your help guys, but I'm giving up... I'm better off wasting my time on real-world code implementation...

try doing this

int a,b,c,d;

scanf("%d%d%d",a,b,c);

d = a+b+c;

printf("The answer is:%d",d)

NEVER EVER GIVE UP

Works just fine...

[IMG]http://uploader.unrealboards.com/uploads22/scanf blows nut.tiff[/IMG]

What my n00b logic is telling me is that main() wants to define the variables ASAP, overriding the printf() function...

Would defining the double variables in a header possibly overcome this?

:confused:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.