Thread: do while loop guide

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    2

    do while loop guide

    Hi guys, i am new here and need some help for my code as there is some error on the do while loop

    Code:
    #include<stdio.h>
    
    void main(void)
    {
        char selection;
        extern int year2010[],year2011[],year2012[],year2013[],year2014[];
        int totalPopulation, ratioOfResident, pop2010, pop2011,pop2012,pop2013,pop2014,choice,selection1;
        selection1 = 0;
        do{
        printf("MENU\n");
        printf("-----\n");
        printf("1-Population of 2010\n");
        printf("2-Population of 2011\n");
        printf("3-Population of 2012\n");
        printf("4-Population of 2013\n");
        printf("5-Population of 2014\n");
        printf("Please select 1,2,3,4 or 5:");
        scanf("%c",&selection);
        }while(selection <1 || selection >5);
    
    
        switch(selection)
    
    
        {
            case'1':
                do{
                printf("1-Number of citizen in thousands\n");
                printf("2-Number of PR(resident) in thousands\n");
                printf("3-Number of non-resident in thousands\n");
                printf("4-Land area(sq.km.)\n");
                printf("Please select 1,2,3,4: \n");
                scanf("%d",&choice);
                if(choice == 1)
                     printf("Number of citizen(resident) is %d.\n",year2010[0]);
                else if(choice == 2)
                     printf("Number of PR(resident) in thousands is %d.\n",year2010[1]);
                else if(choice == 3)
                    printf("Number of non-resident in thousands is %d.\n",year2010[2]);
                else if(choice == 4)
                    printf("Land area(sq.km.) is %d.\n",year2010[3]);
                else
                    printf("You have select a wrong choice.\n");
                }while(choice<1 || choice>4);
                       break;
    
    
            case'2':
                do{
                printf("1-Number of citizen in thousands\n");
                printf("2-Number of PR(resident) in thousands\n");
                printf("3-Number of non-resident in thousands\n");
                printf("4-Land area(sq.km.)\n");
                printf("Please select 1,2,3,4: \n");
                scanf("%d",&choice);
                if(choice == 1)
                     printf("Number of citizen(resident) is %d.\n",year2011[0]);
                else if(choice == 2)
                     printf("Number of PR(resident) in thousands is %d.\n",year2011[1]);
                else if(choice == 3)
                    printf("Number of non-resident in thousands is %d.\n",year2011[2]);
                else if(choice == 4)
                    printf("Land area(sq.km.) is %d.\n",year2011[3]);
                else
                    printf("You have select a wrong choice.\n");
                }while(choice<1||choice>5);
                       break;
    
    
            case'3':
                printf("1-Number of citizen in thousands\n");
                printf("2-Number of PR(resident) in thousands\n");
                printf("3-Number of non-resident in thousands\n");
                printf("4-Land area(sq.km.)\n");
                printf("Please select 1,2,3,4: \n");
                scanf("%d",&choice);
                if(choice == 1)
                     printf("Number of citizen(resident) is %d.\n",year2012[0]);
                else if(choice == 2)
                     printf("Number of PR(resident) in thousands is %d.\n",year2012[1]);
                else if(choice == 3)
                    printf("Number of non-resident in thousands is %d.\n",year2012[2]);
                else if(choice == 4)
                    printf("Land area(sq.km.) is %d.\n",year2012[3]);
                else
                    printf("You have select a wrong choice.\n");
                       break;
             
            case'4':
                printf("1-Number of citizen in thousands\n");
                printf("2-Number of PR(resident) in thousands\n");
                printf("3-Number of non-resident in thousands\n");
                printf("4-Land area(sq.km.)\n");
                printf("Please select 1,2,3,4: \n");
                scanf("%d",&choice);
                if(choice == 1)
                     printf("Number of citizen(resident) is %d.\n",year2013[0]);
                else if(choice == 2)
                     printf("Number of PR(resident) in thousands is %d.\n",year2013[1]);
                else if(choice == 3)
                    printf("Number of non-resident in thousands is %d.\n",year2013[2]);
                else if(choice == 4)
                    printf("Land area(sq.km.) is %d.\n",year2013[3]);
                else
                    printf("You have select a wrong choice.\n");
                       break;
    
    
            case'5':
                printf("1-Number of citizen in thousands\n");
                printf("2-Number of PR(resident) in thousands\n");
                printf("3-Number of non-resident in thousands\n");
                printf("4-Land area(sq.km.)\n");
                printf("Please select 1,2,3,4: \n");
                scanf("%d",&choice);
                if(choice == 1)
                     printf("Number of citizen(resident) is %d.\n",year2014[0]);
                else if(choice == 2)
                     printf("Number of PR(resident) in thousands is %d.\n",year2014[1]);
                else if(choice == 3)
                    printf("Number of non-resident in thousands is %d.\n",year2014[2]);
                else if(choice == 4)
                    printf("Land area(sq.km.) is %d.\n",year2014[3]);
                else
                    printf("You have select a wrong choice.\n");
                       break;
    
    
    
    
            default:printf("You have selected a wrong choice\n");
                
    
    
        }//switch
        
            
    
    
    }// main
    when i build it, it will be successful but when i select any choice the output will be like this.


    MENU
    -----
    1-Population of 2010
    2-Population of 2011
    3-Population of 2012
    4-Population of 2013
    5-Population of 2014
    Please select 1,2,3,4 or 5:0
    MENU
    -----
    1-Population of 2010
    2-Population of 2011
    3-Population of 2012
    4-Population of 2013
    5-Population of 2014
    Please select 1,2,3,4 or 5:MENU
    -----
    1-Population of 2010
    2-Population of 2011
    3-Population of 2012
    4-Population of 2013
    5-Population of 2014
    Please select 1,2,3,4 or 5:

    //please give me some guidance on what is wrong.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    What compiler are you using? Because this is what I got when I tried to compile your code:

    Code:
    /*
    main.c|3|warning: return type of 'main' is not 'int'|
    main.c||In function 'main':|
    main.c|7|warning: unused variable 'pop2014'|
    main.c|7|warning: unused variable 'pop2013'|
    main.c|7|warning: unused variable 'pop2012'|
    main.c|7|warning: unused variable 'pop2011'|
    main.c|7|warning: unused variable 'pop2010'|
    main.c|7|warning: unused variable 'ratioOfResident'|
    main.c|7|warning: unused variable 'totalPopulation'|
    obj\Debug\main.o||In function `main':|
    main.c|35|undefined reference to `year2010'|
    main.c|37|undefined reference to `year2010'|
    main.c|39|undefined reference to `year2010'|
    main.c|41|undefined reference to `year2010'|
    main.c|57|undefined reference to `year2011'|
    main.c|59|undefined reference to `year2011'|
    main.c|61|undefined reference to `year2011'|
    main.c|63|undefined reference to `year2011'|
    main.c|78|undefined reference to `year2012'|
    main.c|80|undefined reference to `year2012'|
    main.c|82|undefined reference to `year2012'|
    main.c|84|undefined reference to `year2012'|
    main.c|97|undefined reference to `year2013'|
    main.c|99|undefined reference to `year2013'|
    main.c|101|undefined reference to `year2013'|
    main.c|103|undefined reference to `year2013'|
    main.c|117|undefined reference to `year2014'|
    main.c|119|undefined reference to `year2014'|
    main.c|121|undefined reference to `year2014'|
    main.c|123|undefined reference to `year2014'|
    ||=== Build finished: 20 errors, 8 warnings ===|
    */
    Code:
    void main(void)
    "main()" returns int, not void: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    Code:
    extern int year2010[],year2011[],year2012[],year2013[],year2014[];
    You need to provide sizes to your arrays.
    And why are they declared as extern?

    Code:
        do{
        printf("MENU\n");
        printf("-----\n");
        printf("1-Population of 2010\n");
        printf("2-Population of 2011\n");
        printf("3-Population of 2012\n");
        printf("4-Population of 2013\n");
        printf("5-Population of 2014\n");
        printf("Please select 1,2,3,4 or 5:");
        scanf("%c",&selection);
        }while(selection <1 || selection >5);
    You're reading the input as a character, not an integer. You handle this in the case statements (e.g. '1', '2'), but not in the above "while()".

    And that hiccup you're seeing is the result of "scanf()" reading a newline left over in the buffer: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    2
    im using microsoft visual studio 2010.

    Code:
     int year2010[]={3230,541,1305,700};	int year2011[]={3257,532,1394,702};
    	int year2012[]={3285,533,1494,705};
    	int year2013[]={3313,531,1554,708};
    	int year2014[]={3343,527,1598,718};
    this are the extern int

    Code:
    #include<stdio.h>
    
    void main(void)
    {
    	char selection;
    	extern int year2010[],year2011[],year2012[],year2013[],year2014[];
    	int totalPopulation, ratioOfResident, pop2010, pop2011,pop2012,pop2013,pop2014,choice,selection1;
    	selection1 = 0;
        do{
    	printf("MENU\n");
    	printf("-----\n");
    	printf("1-Population of 2010\n");
    	printf("2-Population of 2011\n");
    	printf("3-Population of 2012\n");
    	printf("4-Population of 2013\n");
    	printf("5-Population of 2014\n");
    	printf("Please select 1,2,3,4 or 5:");
    	scanf(" %c",&selection);
    	}while(selection <1 || selection >5);
    
    
    	switch(selection)
    
    
    	{
    	    case'1':
    			do{
    			printf("1-Number of citizen in thousands\n");
    			printf("2-Number of PR(resident) in thousands\n");
    			printf("3-Number of non-resident in thousands\n");
    			printf("4-Land area(sq.km.)\n");
    			printf("Please select 1,2,3,4: \n");
    			scanf("%d",&choice);
    			if(choice == 1)
    				 printf("Number of citizen(resident) is %d.\n",year2010[0]);
    			else if(choice == 2)
    				 printf("Number of PR(resident) in thousands is %d.\n",year2010[1]);
    			else if(choice == 3)
    				printf("Number of non-resident in thousands is %d.\n",year2010[2]);
    			else if(choice == 4)
    				printf("Land area(sq.km.) is %d.\n",year2010[3]);
    			else
    				printf("You have select a wrong choice.\n");
    			}while(choice<1 || choice>4);
    			       break;
    
    
    		case'2':
    			do{
    			printf("1-Number of citizen in thousands\n");
    			printf("2-Number of PR(resident) in thousands\n");
    			printf("3-Number of non-resident in thousands\n");
    			printf("4-Land area(sq.km.)\n");
    			printf("Please select 1,2,3,4: \n");
    			scanf("%d",&choice);
    			if(choice == 1)
    				 printf("Number of citizen(resident) is %d.\n",year2011[0]);
    			else if(choice == 2)
    				 printf("Number of PR(resident) in thousands is %d.\n",year2011[1]);
    			else if(choice == 3)
    				printf("Number of non-resident in thousands is %d.\n",year2011[2]);
    			else if(choice == 4)
    				printf("Land area(sq.km.) is %d.\n",year2011[3]);
    			else
    				printf("You have select a wrong choice.\n");
    			}while(choice<1||choice>5);
    			       break;
    
    
    		case'3':
    			printf("1-Number of citizen in thousands\n");
    			printf("2-Number of PR(resident) in thousands\n");
    			printf("3-Number of non-resident in thousands\n");
    			printf("4-Land area(sq.km.)\n");
    			printf("Please select 1,2,3,4: \n");
    			scanf("%d",&choice);
    			if(choice == 1)
    				 printf("Number of citizen(resident) is %d.\n",year2012[0]);
    			else if(choice == 2)
    				 printf("Number of PR(resident) in thousands is %d.\n",year2012[1]);
    			else if(choice == 3)
    				printf("Number of non-resident in thousands is %d.\n",year2012[2]);
    			else if(choice == 4)
    				printf("Land area(sq.km.) is %d.\n",year2012[3]);
    			else
    				printf("You have select a wrong choice.\n");
    				   break;
             
    		case'4':
                printf("1-Number of citizen in thousands\n");
    			printf("2-Number of PR(resident) in thousands\n");
    			printf("3-Number of non-resident in thousands\n");
    			printf("4-Land area(sq.km.)\n");
    			printf("Please select 1,2,3,4: \n");
    			scanf("%d",&choice);
    			if(choice == 1)
    				 printf("Number of citizen(resident) is %d.\n",year2013[0]);
    			else if(choice == 2)
    				 printf("Number of PR(resident) in thousands is %d.\n",year2013[1]);
    			else if(choice == 3)
    				printf("Number of non-resident in thousands is %d.\n",year2013[2]);
    			else if(choice == 4)
    				printf("Land area(sq.km.) is %d.\n",year2013[3]);
    			else
    				printf("You have select a wrong choice.\n");
    				   break;
    
    
    		case'5':
    		    printf("1-Number of citizen in thousands\n");
    			printf("2-Number of PR(resident) in thousands\n");
    			printf("3-Number of non-resident in thousands\n");
    			printf("4-Land area(sq.km.)\n");
    			printf("Please select 1,2,3,4: \n");
    			scanf("%d",&choice);
    			if(choice == 1)
    				 printf("Number of citizen(resident) is %d.\n",year2014[0]);
    			else if(choice == 2)
    				 printf("Number of PR(resident) in thousands is %d.\n",year2014[1]);
    			else if(choice == 3)
    				printf("Number of non-resident in thousands is %d.\n",year2014[2]);
    			else if(choice == 4)
    				printf("Land area(sq.km.) is %d.\n",year2014[3]);
    			else
    				printf("You have select a wrong choice.\n");
    				   break;
    
    
    
    
    		default:printf("You have selected a wrong choice\n");
    			
    
    
    	}//switch
    	
    		
    
    
    }// main
    The previous problem managed to go away but now whenever i select a option this happened.

    MENU
    -----
    1-Population of 2010
    2-Population of 2011
    3-Population of 2012
    4-Population of 2013
    5-Population of 2014
    Please select 1,2,3,4 or 5:1
    MENU
    -----
    1-Population of 2010
    2-Population of 2011
    3-Population of 2012
    4-Population of 2013
    5-Population of 2014
    Please select 1,2,3,4 or 5:2
    MENU
    -----
    1-Population of 2010
    2-Population of 2011
    3-Population of 2012
    4-Population of 2013
    5-Population of 2014
    Please select 1,2,3,4 or 5:3
    MENU

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,548
    Looking at this snippet of code:
    Code:
    do {
        printf("1-Number of citizen in thousands\n");
        printf("2-Number of PR(resident) in thousands\n");
        printf("3-Number of non-resident in thousands\n");
        printf("4-Land area(sq.km.)\n");
        printf("Please select 1,2,3,4: \n");
        scanf("%d", &choice);
        if (choice == 1)
            printf("Number of citizen(resident) is %d.\n", year2010[0]);
        else if (choice == 2)
            printf("Number of PR(resident) in thousands is %d.\n", year2010[1]);
        else if (choice == 3)
            printf("Number of non-resident in thousands is %d.\n", year2010[2]);
        else if (choice == 4)
            printf("Land area(sq.km.) is %d.\n", year2010[3]);
        else
            printf("You have select a wrong choice.\n");
    } while (choice < 1 || choice > 4);
    You are basically repeating it for year2011, year 2012, 2013, and year 2014. Rather, you should keep in mind the Don't Repeat Yourself (DRY) principle. For example, you could write a function:
    Code:
    void showPopulation(int year[]) {
        int choice = 0;
        do {
            printf("1-Number of citizen in thousands\n");
            printf("2-Number of PR(resident) in thousands\n");
            printf("3-Number of non-resident in thousands\n");
            printf("4-Land area(sq.km.)\n");
            printf("Please select 1,2,3,4: \n");
            scanf("%d", &choice);
            if (choice == 1)
                printf("Number of citizen(resident) is %d.\n", year[0]);
            else if (choice == 2)
                printf("Number of PR(resident) in thousands is %d.\n", year[1]);
            else if (choice == 3)
                printf("Number of non-resident in thousands is %d.\n", year[2]);
            else if (choice == 4)
                printf("Land area(sq.km.) is %d.\n", year[3]);
            else
                printf("You have select a wrong choice.\n");
        } while (choice < 1 || choice > 4);
    }
    Now, you can simplify your switch:
    Code:
    switch (selection) {
    case'1':
        showPopulation(year2010);
        break;
    case'2':
        showPopulation(year2011);
        break;
    /* etc */
    default:
        printf("You have selected a wrong choice\n");
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. guide anyone ?..
    By dev cpp in forum C Programming
    Replies: 2
    Last Post: 03-20-2015, 05:39 AM
  2. A guide to start...plz
    By Blacky Ducky in forum Game Programming
    Replies: 6
    Last Post: 02-19-2011, 06:57 PM
  3. Someone guide me here..
    By xeddiex in forum Windows Programming
    Replies: 3
    Last Post: 04-04-2005, 12:12 AM
  4. [guide] Socks
    By BianConiglio in forum C Programming
    Replies: 5
    Last Post: 05-21-2004, 09:25 AM
  5. would somebody be able to guide me
    By simpleYetPowerful in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-14-2001, 09:40 PM