Thread: building a function

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    40

    building a function

    Code:
    flag = 1;
    while(flag == 1)
    {
        printf("What is the student's Assignment 1 mark? :");
        scanf("%d",&a1);
    
    
        if(a1>100||a1<0)
    {
        printf("Invalid mark. Please enter again\n");
        flag = 1;
    }
    else
    {
            flag = 0;
    }
    }
    
    
    flag = 1;
    while(flag == 1)
    {
        printf("What is the student's Assignment 2 mark? :");
        scanf("%d",&a2);
    
    
        if(a2>100||a2<0)
    {
        printf("Invalid mark. Please enter again\n");
        flag = 1;
    }
    else
    {
            flag = 0;
    }
    }
    
    
    flag = 1;
    while(flag == 1)
    {
    
    
        printf("What is the student's Assignment 3 mark? :");
        scanf("%d",&a3);
    
    
        if(a3>100||a3<0)
    {
        printf("Invalid mark. Please enter again\n");
        flag = 1;
    }
    else
    {
            flag = 0;
    }
    }
    I'm trying to find a way to express this thing into a function because it's repeating almost same thing over and over again.
    Could anyone give me any suggestions?

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    nvm i got it

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good instincts, Toshiro. When code has to be repeated over and over in a program, it's time to re-do it.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You could try something like:

    Code:
    # define MAXGRADES 3
    	
    //...
    
    	void ShowNextGrade(int thisGrade)
    	{
    		int gradeVal = 101;
            int flag = 1;
    
    		if(thisGrade == 0)
    		{
    			printf("What is the student's Assignment 1 mark? :");
    		}
    		//else if...etc
    		
    		while(flag == 1)
    		{
    			scanf("%d",&gradeVal);
    
    			if( gradeVal > 100 || gradeVal < 0 ) 
    			{
    				printf("Invalid mark. Please enter again\n");
    			}
    			else
    			{
    				flag = 0;
    			}
    
    		}
    	}
    
    	//...
    
    	int gradeNum = 0;
    
    	while(gradeNum < MAXGRADES)
    	{	
    		ShowNextGrade(gradeNum);
    		gradeNum++;
    	}
    Last edited by rogster001; 03-15-2013 at 02:08 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with building
    By Darkinyuasha1 in forum Windows Programming
    Replies: 1
    Last Post: 02-10-2013, 03:00 AM
  2. Problem with my building function for my roguelike
    By Thorbenn in forum Game Programming
    Replies: 2
    Last Post: 05-27-2012, 09:56 PM
  3. building a DLL
    By sass in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2006, 05:25 AM
  4. building/using dll's
    By bluehead in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 07:03 AM
  5. building pc..
    By Shadow in forum Tech Board
    Replies: 13
    Last Post: 01-08-2003, 07:53 PM