Thread: New C Programmer -- Need help here!

  1. #1
    Frustrated C Programmer
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    5

    New C Programmer -- Need help here!

    Hey everyone. First post on these boards! I recently got into very basic C-programming because I'm taking a mandatory course in college for C-programming. The professor has assigned a few simple programs for us to create with the limited knowledge of C-programming that we have so far. Anyways, on to what brought me here...

    I've had a couple minor problems with a couple of the programs he's assigned. The first one here, all the code looks perfect. It looks like it should run perfectly. But then when I run it, it asks for the first input, waits to take it in (and takes it in), waits for the second input, then skips right over that and moves on to asking the third user input and waiting for that. What's up with that? Please look at this code and tell me what's wrong with it, because I just can't see it.

    Code:
    // Description: Calculates how many pictures you can store on a CD.
    
    #include <stdio.h>
    #define MB 1048576
    #define PX 3
    
    int main()
    {
    	// Delcares necessary variables
    	int cd_space, pic_length, pic_width;
    	int pic_size, number_pics;
    	
    	// Asks user for input
    	printf("How many megabytes of free space is on the CD? ");
    	scanf("&d", &cd_space);
    	printf("What is the length of each picture in pixels? ");
    	scanf("%d", &pic_length);
    	printf("What is the width of each picture in pixels? ");
    	scanf("%d", &pic_width);
    	
    	// Calculates how many whole pictures can be placed on the CD
    	pic_size = pic_width * pic_length * PX;
    	number_pics = (cd_space * MB) / pic_size;
    	
    	printf("You can store %d pictures on your CD.", number_pics);
    	
    	return 0;
    }



    Second program here, I'm getting different numbers in the results than my professor's practice run of the program. Can you please look at this one and tell me if all the math and everything looks correct, and it might be my professor's program that was wrong?

    Code:
    // Description: Calculates the number of jelly beans in a container.
    
    #include <stdio.h>
    #define PI 3.14159265
    
    int main()
    {
    	// Declare necessary variables
    	double jb_radius, conta_radius, conta_height, percent_air;
    	double jb_volume, conta_volume, conta_volume_noair, jb_number;
    	
    	// Request user input
    	printf("What is the radius of a single jelly bean? ");
    	scanf("%lf", &jb_radius);
    	printf("What is the radius of the container? ");
    	scanf("%lf", &conta_radius);
    	printf("What is the height of the container? ");
    	scanf("%lf", &conta_height);
    	printf("What percentage of the container is filled with air? ");
    	scanf("%lf", &percent_air);
    	
    	// Calculate volumes and number of jelly beans
    	conta_volume = PI * conta_radius * conta_radius * conta_height;
    	conta_volume_noair = conta_volume - (conta_volume * (percent_air * .01));
    	jb_volume = 4 / 3 * PI * jb_radius * jb_radius * jb_radius;
    	jb_number = conta_volume_noair / jb_volume;
    	
    	// Tell user how many jelly beans are in the jar
    	printf("There are %.0lf jelly beans in the jar.", jb_number);
    	
    	return 0;
    }



    Thanks to any and everyone who helps!


    - Randy

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) The newline (you pressing enter) is left in the input stream when you use scanf in the manner in which you're using it. As such, the next call to scanf gets that (the newline) as its input. Consider reading the FAQ entry (located at the top of the page) on better methods of reading input from the user.

    2) Add a few lines in to print out everything you read in, to be sure it's what you expect it to be. You could be suffering from the same problem as above. Double check to make sure you're not using any variables before you initalize them.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    Wif ref. to 1st program, declare a char array of 1 and use gets to store '\n' in it.
    In ur first scanf statement, should be scanf("%d", &cd_space);
    after the scanf statements, use gets to store '\n'.
    Eg: scanf("%d", &cd_space);
    gets(dump);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's a horrible idea.

    1) Why on earth would you use an array of one, ever? There's no point in this at all, unless you're just trying to get around the need to use the address-of operator. Which is really a stupid way to do it.
    2) There's a FAQ on why using gets is bad. Using gets at all is a horrible idea, not mentioning the fact that even if you were going to use it, using it with a one byte buffer is one of the worst ideas I've ever heard.
    3) Never take advice from some one who starts their post with the word, if you call it that, Wif.

    Just read the FAQ instead on better methods of reading input.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    [QUOTE=xRandyx]Hey everyone. First post on these boards! I recently got into very basic C-programming because I'm taking a mandatory course in college for C-programming. The professor has assigned a few simple programs for us to create with the limited knowledge of C-programming that we have so far. Anyways, on to what brought me here...

    I've had a couple minor problems with a couple of the programs he's assigned. The first one here, all the code looks perfect. It looks like it should run perfectly. But then when I run it, it asks for the first input, waits to take it in (and takes it in), waits for the second input, then skips right over that and moves on to asking the third user input and waiting for that. What's up with that? Please look at this code and tell me what's wrong with it, because I just can't see it.

    Code:
    // Description: Calculates how many pictures you can store on a CD.
    
    #include <stdio.h>
    #define MB 1048576
    #define PX 3
    
    int main()
    {
    	// Delcares necessary variables
    	int cd_space, pic_length, pic_width;
    	int pic_size, number_pics;
    	
    	// Asks user for input
    	printf("How many megabytes of free space is on the CD? ");
    	scanf("&d", &cd_space);   // I think should be scanf("%d", &cd_space);
    
    
    	printf("What is the length of each picture in pixels? ");
    	scanf("%d", &pic_length);
    	printf("What is the width of each picture in pixels? ");
    	scanf("%d", &pic_width);
    	
    	// Calculates how many whole pictures can be placed on the CD
    	pic_size = pic_width * pic_length * PX;
    	number_pics = (cd_space * MB) / pic_size;
    	
    	printf("You can store %d pictures on your CD.", number_pics);
    	
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    I thank Quzah for the feedback to my previous reply...

    I have misunstood that the problem is caused by '\n' left in stdin, therefore i use gets to get '\n' into dump...

    Is '\n' considered 1 char?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. It's what's known as an escaped character. There are a number of them. \' to denote a single quote, \" for a double quote, \n for a newline, \r for a carraige return, etc.

    For better and safer methods of getting input, let me again suggest the FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    Code:
    // Description: Calculates the number of jelly beans in a container.
    
    #include <stdio.h>
    #define PI 3.14159265
    
    int main()
    {
    	// Declare necessary variables
    	double jb_radius, conta_radius, conta_height, percent_air;
    	double jb_volume, conta_volume, conta_volume_noair, jb_number;
    	
    	// Request user input
    	printf("What is the radius of a single jelly bean? ");
    	scanf("%lf", &jb_radius);
    	printf("What is the radius of the container? ");
    	scanf("%lf", &conta_radius);
    	printf("What is the height of the container? ");
    	scanf("%lf", &conta_height);
    	printf("What percentage of the container is filled with air? ");
    	scanf("%lf", &percent_air);
    	
    	// Calculate volumes and number of jelly beans
    	conta_volume = PI * conta_radius * conta_radius * conta_height;
    	conta_volume_noair = conta_volume - (conta_volume * (percent_air * .01));
    	jb_volume = 4 / 3.0 * PI * jb_radius * jb_radius * jb_radius;
    	jb_number = conta_volume_noair / jb_volume;
    	
    	// Tell user how many jelly beans are in the jar
    	printf("There are %.0lf jelly beans in the jar.", jb_number);
    	
    	return 0;
    }
    Pls take note tat a interger / interger will give a interger value and the calculatation above requires floating numbers.

    4 / 3 will be 1...

  9. #9
    Frustrated C Programmer
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    5
    Thanks everyone! I knew it had to be some small mistake that I couldn't notice with my blurry monitor. Now all my programs are working fine!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. When are you REALLY a programmer?
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 09:08 PM
  3. Senior 3D Programmer - Moab Studios™ LLC
    By moab in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-30-2005, 10:19 PM
  4. Me as a programmer?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-31-2002, 06:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM