Thread: Help with While Loop and array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Unhappy Help with While Loop and array

    I have to make a program for a voting machine for a presidential election as part of a school assignment. my question is that i'm using a while loop and numerous arrays. In the while loop , the data i type in gets overwritten every time the while loop executes in the loop. How to i switch the data location from for example i[0] to i[1] when the loop runs for the second time.

    If anyone could also tell me how to do proportional representation too, could you let me know.


    Thanks

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    We have no way of knowing what's going wrong in your code unless you show us your code.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Code:
    while(i!=5)
        {
           
               
               
                    printf("2011 Presidential Election \n");
                
                    system("pause");
                   
                    printf("\nPlease Enter a vote for candidate1 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&mg[0]);
                    
                    printf("Please Enter a vote for candidate2 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&da[0]);
                    
                    printf("Please Enter a vote for candidate3 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&no[0]);
                    
                    printf("Please Enter a vote for candidate4 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&gal[0]);
                    
                    printf("Please Enter a vote for candidate5 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&hig[0]);
                    
                    printf("Please Enter a vote for candidate6 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&dav[0]);
                    
                    printf("Please Enter a vote for candidate7 \n");
                    printf("Enter 1 for first choice \n");
                    printf("Enter 2 for 2nd choice \n");
                    printf("Enter 3 for 3rd choice \n");
                    printf("Enter 0 for no vote \n");
                            
                    scanf("%d",&mit[0]);
                    
                    
                    
                    printf("\n Thank you for voting!! \n");
                    
                   
                    
                    system("pause");
                  
                    (i++);
                    mg[0]+= 1;
                    
                   system("cls");    
                  
                  
                  
                   
        }    
            printf("%d,%d,%d,%d", mg[0]);
    ok here's the part I am talking about, what do i do to get mg[0] to change to mg[1] on the loop and so on to mg[2] and mg[3]

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use a variable instead of hardcoding the array index:

    Code:
    	int ray[5] = { 4, 3, 2, 1, 0 };
    	int i = 0;
    	while (ray[i]) {
    		printf("%d\n", ray[i]);
    		i++;
    	}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Thanks MK27, much appreciated. got it working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop with array
    By camel-man in forum C Programming
    Replies: 9
    Last Post: 04-25-2011, 07:21 PM
  2. While loop with array
    By Lego_TeCh in forum C Programming
    Replies: 3
    Last Post: 08-15-2009, 09:42 AM
  3. array and loop help
    By hockey1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 09:47 PM
  4. Help Array and Loop
    By Mike1982 in forum C Programming
    Replies: 5
    Last Post: 10-17-2007, 02:41 PM
  5. Need help on for loop and array
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2003, 10:17 AM