Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. While loop not working on C.

While loop not working on C.

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Superman_

    One more thing that I would like to add is that you should initialize variable i to 0.

    «_Superman_»  _I love work. It gives me something to do between weekends.

    _Microsoft MVP (Visual C++) (October 2009 - September 2013)

    Polymorphism in C

    T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #7

    thank you

    1 Reply Last reply
    0
    • T Tarun Jha

      In this program i have tried to store all the input in the array. The while should continue till the user input 0, but the loop exits only after one input.

      #include
      #include

      int main(){

      int num\[50\], i, j;
      int count = 0;
      
      printf("Enter your integer: \\n");
      
      do{
      	scanf(" %d", &num\[i\]);
      	i++;
      	count++;
      	
      }while(num\[i\]!=0);
      
      for(j=1; j<= count; j++){
      	
      	if(num\[1\] > num\[j\]){
      		num\[1\] = num\[j\];
      	}
      }
      
      printf("%d is the greatest of them all !", num\[0\]);
      
      return 0;
      

      }

      T Offline
      T Offline
      Tarun Jha
      wrote on last edited by
      #8

      THE WORKING CODE

      Quote:

      #include #include int main(){ int num[50], i=0, j; int count = 0; printf("Enter your integer: \n"); do{ scanf(" %d", &num[i]); i++; count++; }while(num[i-1]!=0); for(j=1; j<= count-1; j++){ if(num[0] < num[j]){ num[0] = num[j]; } } printf("%d is the greatest of them all !", num[0]); return 0; }

      D G 2 Replies Last reply
      0
      • T Tarun Jha

        THE WORKING CODE

        Quote:

        #include #include int main(){ int num[50], i=0, j; int count = 0; printf("Enter your integer: \n"); do{ scanf(" %d", &num[i]); i++; count++; }while(num[i-1]!=0); for(j=1; j<= count-1; j++){ if(num[0] < num[j]){ num[0] = num[j]; } } printf("%d is the greatest of them all !", num[0]); return 0; }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #9

        Suggestions: 1) The variable count is not necessary. You can use i instead. 2) The separate for() loop is not necessary. You can do the min/max checking in the while() loop.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • T Tarun Jha

          In this program i have tried to store all the input in the array. The while should continue till the user input 0, but the loop exits only after one input.

          #include
          #include

          int main(){

          int num\[50\], i, j;
          int count = 0;
          
          printf("Enter your integer: \\n");
          
          do{
          	scanf(" %d", &num\[i\]);
          	i++;
          	count++;
          	
          }while(num\[i\]!=0);
          
          for(j=1; j<= count; j++){
          	
          	if(num\[1\] > num\[j\]){
          		num\[1\] = num\[j\];
          	}
          }
          
          printf("%d is the greatest of them all !", num\[0\]);
          
          return 0;
          

          }

          M Offline
          M Offline
          Manish K Agarwal
          wrote on last edited by
          #10

          here you go

          #include
          #include

          int main(){

          int num\[50\], i = -1, j;
          int count = 0;
          
          printf("Enter your integer: \\n");
          
          do{
          	scanf(" %d", &num\[i+1\]);
          	i++;
          	count++;
          	
          }while(num\[i\]!=0);
          
          for(j=1; j<= count; j++){
          	
          	if(num\[1\] > num\[j\]){
          		num\[1\] = num\[j\];
          	}
          }
          
          printf("%d is the greatest of them all !", num\[0\]);
          
          return 0;
          

          }

          T D 2 Replies Last reply
          0
          • T Tarun Jha

            In this program i have tried to store all the input in the array. The while should continue till the user input 0, but the loop exits only after one input.

            #include
            #include

            int main(){

            int num\[50\], i, j;
            int count = 0;
            
            printf("Enter your integer: \\n");
            
            do{
            	scanf(" %d", &num\[i\]);
            	i++;
            	count++;
            	
            }while(num\[i\]!=0);
            
            for(j=1; j<= count; j++){
            	
            	if(num\[1\] > num\[j\]){
            		num\[1\] = num\[j\];
            	}
            }
            
            printf("%d is the greatest of them all !", num\[0\]);
            
            return 0;
            

            }

            A Offline
            A Offline
            asa76
            wrote on last edited by
            #11

            The first problem is that the variable i has not been initialized. The second problem is that you are checking the wrong array index. See the comments in the modified version of your code below.

            #include #include int main(){

            int num\[50\], i, j;
            int count = 0;
            
            i = 0;  // Added this
            printf("Enter your integer: \\n");
            
            do{
            	scanf(" %d", &num\[i\]);
            	count++;
            	
            }while(num\[i++\]!=0);  // Changed this
            
            for(j=1; j<= count; j++){
            	
            	if(num\[1\] > num\[j\]){
            		num\[1\] = num\[j\];
            	}
            }
            
            printf("%d is the greatest of them all !", num\[0\]);
            
            return 0;
            

            }

            D 1 Reply Last reply
            0
            • T Tarun Jha

              can you write the correct code. please!

              G Offline
              G Offline
              geodoom
              wrote on last edited by
              #12

              First suspicious point : you declare i but you do not initialize it (you also declare j but at least you plan to initialize it in the for loop so it is acceptable). Second suspicious point: you are to hasty to increment i. If you want to insist using a

              do {
              ...
              } while (condition)

              loop , and not a

              while (condition)
              { ... }

              then you must realize that you need to check the value just read (if it was zero) and then , if it not 0 , meaning the user want to keep giving values , increase i++ , getting ready for reading the next element. Try to implement those 2 points , and also as an extra exercise , implement the while(condition) { ... } It is not hard, you can do it in around half an hour or less, and you will benefit a lot by pondering on how it will differ from your do { ... } while(condition)

              T 1 Reply Last reply
              0
              • A asa76

                The first problem is that the variable i has not been initialized. The second problem is that you are checking the wrong array index. See the comments in the modified version of your code below.

                #include #include int main(){

                int num\[50\], i, j;
                int count = 0;
                
                i = 0;  // Added this
                printf("Enter your integer: \\n");
                
                do{
                	scanf(" %d", &num\[i\]);
                	count++;
                	
                }while(num\[i++\]!=0);  // Changed this
                
                for(j=1; j<= count; j++){
                	
                	if(num\[1\] > num\[j\]){
                		num\[1\] = num\[j\];
                	}
                }
                
                printf("%d is the greatest of them all !", num\[0\]);
                
                return 0;
                

                }

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #13

                asa76 wrote:

                for(j=1; j<= count; j++)
                {
                if (num[1] > num[j])
                {
                num[1] = num[j];
                }
                }

                printf("%d is the greatest of them all !", num[0]);

                You do realize this is wrong, don't you?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                1 Reply Last reply
                0
                • G geodoom

                  First suspicious point : you declare i but you do not initialize it (you also declare j but at least you plan to initialize it in the for loop so it is acceptable). Second suspicious point: you are to hasty to increment i. If you want to insist using a

                  do {
                  ...
                  } while (condition)

                  loop , and not a

                  while (condition)
                  { ... }

                  then you must realize that you need to check the value just read (if it was zero) and then , if it not 0 , meaning the user want to keep giving values , increase i++ , getting ready for reading the next element. Try to implement those 2 points , and also as an extra exercise , implement the while(condition) { ... } It is not hard, you can do it in around half an hour or less, and you will benefit a lot by pondering on how it will differ from your do { ... } while(condition)

                  T Offline
                  T Offline
                  Tarun Jha
                  wrote on last edited by
                  #14

                  i will try. thank you

                  1 Reply Last reply
                  0
                  • M Manish K Agarwal

                    here you go

                    #include
                    #include

                    int main(){

                    int num\[50\], i = -1, j;
                    int count = 0;
                    
                    printf("Enter your integer: \\n");
                    
                    do{
                    	scanf(" %d", &num\[i+1\]);
                    	i++;
                    	count++;
                    	
                    }while(num\[i\]!=0);
                    
                    for(j=1; j<= count; j++){
                    	
                    	if(num\[1\] > num\[j\]){
                    		num\[1\] = num\[j\];
                    	}
                    }
                    
                    printf("%d is the greatest of them all !", num\[0\]);
                    
                    return 0;
                    

                    }

                    T Offline
                    T Offline
                    Tarun Jha
                    wrote on last edited by
                    #15

                    thank you.

                    1 Reply Last reply
                    0
                    • T Tarun Jha

                      THE WORKING CODE

                      Quote:

                      #include #include int main(){ int num[50], i=0, j; int count = 0; printf("Enter your integer: \n"); do{ scanf(" %d", &num[i]); i++; count++; }while(num[i-1]!=0); for(j=1; j<= count-1; j++){ if(num[0] < num[j]){ num[0] = num[j]; } } printf("%d is the greatest of them all !", num[0]); return 0; }

                      G Offline
                      G Offline
                      geodoom
                      wrote on last edited by
                      #16

                      Since you present a working solution you deserve to receive some more elegant alternatives. Here is how I improved your code , there are some nice tricks for you to learn from it

                      #include
                      #define SIZE 15
                      int main()
                      {
                      int num[SIZE], i=0 , max;
                      int count = 0; // be sure that we do not read more than 15 elements
                      printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
                      /*
                      scanf(" %d", &num[i]);
                      while (num[i]!=0 && count < SIZE )
                      {
                      ++count;
                      scanf(" %d", &num[++i]);
                      }
                      */
                      /* Either the above while loop */
                      do{
                      scanf(" %d", &num[i]);
                      count++;
                      } while(num[i++]!=0 && count < SIZE);
                      /* Or the above do { } while loop */
                      max = num[0]; /* Avoid to altert any of your array elements */
                      for(i=1; i< count; i++)
                      if(max < num[i]) max = num[i]; /* each time we find a better max, update our max value */
                      printf("%d is the greatest of them all !\n", max);
                      return 0;
                      }

                      I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE ! Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].

                      T 1 Reply Last reply
                      0
                      • G geodoom

                        Since you present a working solution you deserve to receive some more elegant alternatives. Here is how I improved your code , there are some nice tricks for you to learn from it

                        #include
                        #define SIZE 15
                        int main()
                        {
                        int num[SIZE], i=0 , max;
                        int count = 0; // be sure that we do not read more than 15 elements
                        printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
                        /*
                        scanf(" %d", &num[i]);
                        while (num[i]!=0 && count < SIZE )
                        {
                        ++count;
                        scanf(" %d", &num[++i]);
                        }
                        */
                        /* Either the above while loop */
                        do{
                        scanf(" %d", &num[i]);
                        count++;
                        } while(num[i++]!=0 && count < SIZE);
                        /* Or the above do { } while loop */
                        max = num[0]; /* Avoid to altert any of your array elements */
                        for(i=1; i< count; i++)
                        if(max < num[i]) max = num[i]; /* each time we find a better max, update our max value */
                        printf("%d is the greatest of them all !\n", max);
                        return 0;
                        }

                        I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE ! Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].

                        T Offline
                        T Offline
                        Tarun Jha
                        wrote on last edited by
                        #17

                        Thank you very much.. :)

                        1 Reply Last reply
                        0
                        • M Manish K Agarwal

                          here you go

                          #include
                          #include

                          int main(){

                          int num\[50\], i = -1, j;
                          int count = 0;
                          
                          printf("Enter your integer: \\n");
                          
                          do{
                          	scanf(" %d", &num\[i+1\]);
                          	i++;
                          	count++;
                          	
                          }while(num\[i\]!=0);
                          
                          for(j=1; j<= count; j++){
                          	
                          	if(num\[1\] > num\[j\]){
                          		num\[1\] = num\[j\];
                          	}
                          }
                          
                          printf("%d is the greatest of them all !", num\[0\]);
                          
                          return 0;
                          

                          }

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #18

                          See here.

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups