Thread: newbie_here!!help??

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    16

    Talking newbie_here!!help??

    Hey, I learning to program in c just started like two or three weeks ago. I was writing this program from one exercise I came in contact with and the code does what it is suppose to do, but i get this number at the end of each prompt i try several things even setting the varibles to zero but instead of the numbers appearing , zero appears. The codes is this:

    #include <stdio.h>

    int main ()
    {
    /*declares variables*/
    int begin_num;
    int bn;
    int stop_num;
    int increment_num;

    /*prompt the user for input*/
    printf ("\n Please enter a number to begin counting from:%d",bn);
    scanf ("%d",&bn);
    printf ("\n Please enter a number to stop counting:%d",stop_num);
    scanf ("%d",&stop_num);
    printf ("\n Please enter the increment number:%d",increment_num);
    scanf ("%d",&increment_num);

    /*the for_loop*/
    for (begin_num =bn;begin_num <=stop_num;begin_num+=increment_num)
    printf ("\n%d",begin_num);
    }


    This is what i get:

    Please enter a number to begin counting from:1074048096

    Please enter a number to stop counting:134513258

    Please enter the Increment number:-1073743960

    Can anyone help me by explaining why this happen or how to fix this.
    Thanx

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Code:
     printf ("\n Please enter a number to begin counting from:%d",bn);
    bn has a garbage value in it. You need to give a variable a value before printing it.

    You do this with all the other variables too, your printing them and then getting a value for them. Should be the other way around

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    You have the control code %d in your printf statements, which is a placeholder for an int. As Sourcecode just said, you need to initialize your variables too... try this... *feeling generous*

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	/*declares variables*/
    	int begin_num=0;
    	int bn=0;
    	int stop_num=0;
    	int increment_num=0;
    
    	/*prompt the user for input*/
    	printf ("Please enter a number to begin counting from:");
    	scanf ("%d",&bn);
    	printf ("\n Please enter a number to stop counting:");
    	scanf ("%d",&stop_num);
    	printf ("\n Please enter the increment number:");
    	scanf ("%d",&increment_num);
    
    	/*the for_loop*/
    	for (begin_num =bn;begin_num <=stop_num;begin_num+=increment_num)
    		printf ("\n%d",begin_num);
    }
    oh, and use code tags...
    Away.

Popular pages Recent additions subscribe to a feed