Thread: Printing asterisks using user input

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    17

    Printing asterisks using user input

    Hello. I'm in the process of writing a program that does the following:

    1. Ask user for input of five numbers, each between 1 and 30
    2. Check if those numbers are between 1 and 30 (if not, tell them)
    3. Use functions for the while loop and checking if the number is between 1 and 30 (two seperate functions)
    4. Take that number, print number of asterisks (each in a new line)

    I've written the program for the most part, but can't figure out how to use functions when needed. Can someone help me out? Thanks.

    Code:
    #include <stdio.h>
    int main()
    {
    	int num=0,num1=0,num2=0,num3=0,num4=0;
    	printf("Please enter five numbers that are between 1 and 30: ");
    	scanf("%d",&num);
    	scanf("%d",&num1);
    	scanf("%d",&num2);
    	scanf("%d",&num3);
    	scanf("%d",&num4);
    	if(num > 30)
    		{
    			printf("One or more of the numbers you entered was greater than 30. Try again: ");
    			scanf("%d",&num);	
    		}
    	if(num < 1 || num==1)
    		{
    			printf("One of the numbers you entered was less than 1 or 1. Try again: ");
    			scanf("%d",&num);	
    		}
    	if(num1 > 30)
    		{
    			printf("The number you entered was greater than 30. Try again: ");
    			scanf("%d",&num1);	
    		}
    	if(num1 < 1 || num1==1)
    		{
    			printf("One of the numbers you entered was less than 1 or 1. Try again: ");
    			scanf("%d",&num1);	
    		}
    	if(num2 > 30)
    		{
    			printf("The number you entered was greater than 30. Try again: ");
    			scanf("%d",&num2);	
    		}
    	if(num2 < 1 || num2==1)
    		{
    			printf("One of the numbers you entered was less than 1 or 1. Try again: ");
    			scanf("%d",&num2);	
    		}
    	if(num3 > 30)
    		{
    			printf("The number you entered was greater than 30. Try again: ");
    			scanf("%d",&num3);	
    		}
    	if(num3 < 1 || num3==1)
    		{
    			printf("One of the numbers you entered was less than 1 or 1. Try again: ");
    			scanf("%d",&num3);	
    		}
    	if(num4 > 30)
    		{
    			printf("The number you entered was greater than 30. Try again: ");
    			scanf("%d",&num4);	
    		}
    	if(num4 < 1 || num4==1)
    		{
    			printf("One of the numbers you entered was less than 1 or 1. Try again: ");
    			scanf("%d",&num4);	
    		}
    
    	while(num < 30 || num > 1)
    		{
    			for(;num>0;num--)
    				{
    					printf("*");
    				}
    			printf("\n");
    		}
    	while(num1 < 30 || num1 > 1)
    		{
    			for(;num1>0;num1--)
    				{
    					printf("*");
    				}
    			printf("\n");
    		}
    	while(num2 < 30 || num2 > 1)
    		{
    			for(;num2>0;num2--)
    				{
    					printf("*");
    				}
    			printf("\n");
    		}
    	while(num3 < 30 || num3 > 1)
    		{
    			for(;num3>0;num3--)
    				{
    					printf("*");
    				}
    			printf("\n");
    		}
    	while(num4 < 30 || num4 > 1)
    		{
    			for(;num4>0;num4--)
    				{
    					printf("*");
    				}
    			printf("\n");
    		}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could replace all those if checks with a function that returns only when a number between 1 and 30 was entered, and then use it something like this:
    Code:
    num1 = get1to30();
    Then make a function called 'get1to30' that has the user keep retrying until a valid number is entered. That's just a simple way to do it. You could get a bit more complex, and pass 'low' and 'high' to it, and make it work for any valid range:
    Code:
    num1 = getrangednumber( 1, 30 );
    Basically you make functions be small tools to handle a job that you may need to do multiple times. It saves you from writing the same big block of code over and over.


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

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    17
    Quote Originally Posted by quzah View Post
    You could replace all those if checks with a function that returns only when a number between 1 and 30 was entered, and then use it something like this:
    Code:
    num1 = get1to30();
    Then make a function called 'get1to30' that has the user keep retrying until a valid number is entered. That's just a simple way to do it. You could get a bit more complex, and pass 'low' and 'high' to it, and make it work for any valid range:
    Code:
    num1 = getrangednumber( 1, 30 );
    Basically you make functions be small tools to handle a job that you may need to do multiple times. It saves you from writing the same big block of code over and over.


    Quzah.
    I can write a function for one user input relatively easily - it's just when it gets to five. I'm not sure where to even start with it.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... something to think about...

    1) can your num variables be made into an array?

    2) Can you process arrays in loops?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by celticpride View Post
    I can write a function for one user input relatively easily - it's just when it gets to five. I'm not sure where to even start with it.
    You aren't understanding me. You have one function that reads one value, and you use the same tool five times.

    Go out and look at the lug nuts on a tire on your car. How many are there? Doesn't matter. You don't get that many different wrenches to take them off. You use one wrench that many different times. You use it once for each nut.

    Your user input function is the wrench. Use it for each nut.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. displaying text below user input (cin)
    By MilkyJoe in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2009, 03:00 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM