Thread: scanf question

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    scanf question

    alright my program needs to prompt the user how many circles between 1 and 10 they would like to calculate circumference and area of. then the user enters a number between 1 and 10 or the program prints an error message.
    however, if a correct number is entered, it then begins to prompt the user to enter the radius for each circle and stores them in an array to do the required calculations.

    lets say you want to use 4 circles.
    the problem is that its printing the prompt message:

    "Enter the radius for circle #1:"

    "Enter the radius for circle #2"

    "Enter the radius for circle #3:"

    "Enter the radius for circle #4:"

    *then down here it allows you to enter the radius'*

    i need it to prompt like this:

    "Enter the radius for circle #1:"
    (scans your entry)
    "Enter the radius for circle #2:"
    (scans your entry)
    "Enter the radius for circle #3:"
    (scans your entry)
    "Enter the radius for circle #4:"
    (scans your entry)

    any ideas?
    *please keep in mind im working on this and its not nearly completed yet, i just like to compile and test along the way so i dont get a million errors at the end to deal with.

    Code:
    #include <stdio.h>
    
    main ()
    {
    	int circleno;
    	int imputvalues[10], i;
    	int j;
    	printf("Please enter the number of circles (max = 10):\n");
    	scanf("%i", &circleno);
    	if (circleno < 1 || circleno > 10)
    		printf("Error -> %i circles is too many. Please try again:\n", circleno);
    		else
    		printf("You have selected to enter %i circles.\n\n", circleno);
    		for ( j = 1; j <= circleno; ++j )
    			printf("Enter the radius for circle #%i:\n\n", j);
    			scanf("%i", &imputvalues[i]);
    		
    		
    	return 0;
    }
    i appreciate the help guys and gals!

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Well, something to consider is that you will want that code in a loop of sorts, such as . . .

    (pseudocode, not real C!)
    Code:
    first = 1
    do {
        if(!first) explain_error()
        print_prompt()
        get_data()
        first = 0 
    } while(invalid_input)
    Or so.

    Does that make any sense?

    Note that this applies to your # of circles data entry as well as the circle radius entry.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 08:48 AM
  4. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 10:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM