Thread: Program stops at first input

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Program stops at first input

    Enter numbers until you enter 0 or -1
    and print sum of all positive two-digit numbers entered
    and print how many of these numbers divisible by 3
    If I enter some input my program just stops and doesn't loop anymore to the next scanf
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void) {
      /* Enter numbers until you enter 0 or -1
       and print sum of all positive two-digit numbers entered
       and print how many of these numbers divisible by 3 */
        
        int num[1000], sum = 0 , i = 0, j = 0, numDigits, counterDividible = 0, counterDigits = 0;
        
        
        while( !(num[j] == 0 || num[j] == -1) ){
            scanf("%d", &num[j]);
            
            numDigits = abs(num[j]);
            
            for( i = 0; numDigits != 0; i++){
                counterDigits = numDigits / 10;
            }
            
            if( counterDigits == 2){
                
                sum += num[j];
            }
            
            j++;
            
            
        }
        
        for( i = 0; i < j; i++){
            
            if(num[i] % 3 == 0){
                
                ++counterDividible;
            }
        }
        
        printf("Sum: %d \n Dividible by 3:%d", sum , counterDividible);
        return 0;
       }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    For starters, you should initialize your num array before ever referencing it. Your while loop is referencing a slot in your array and you don't even know what value will be there.

    Secondly, your first for loop is an infinite loop. numDigits is not being changed inside the body of the loop anywhere, so as long as numDigits!=0 the loop will continue.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by camel-man View Post
    For starters, you should initialize your num array before ever referencing it. Your while loop is referencing a slot in your array and you don't even know what value will be there.

    Secondly, your first for loop is an infinite loop. numDigits is not being changed inside the body of the loop anywhere, so as long as numDigits!=0 the loop will continue.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void) {
    
    
        
        int num[1000] ={0}, sum = 0 , i = 0, j = 0, numDigits, counterDividible = 0, counterDigits = 0;
        
        
        do{
        	
        	scanf("%d", &num[j]);
        	
        	if(num[j] < 0){
        		num[j] = -num[j];
    		}
        	numDigits = num[j];
        	
        	for( ; counterDigits != 0; counterDigits++){
        		counterDigits = numDigits / 10;
    		}
    		
    		if( counterDigits == 2){
    			
    			sum += num[j];
    		}
    		
        	j++;
        	
    		
    	} while( !(num[j] == 0 || num[j] == -1) );
    	
    	for( i = 0; i < j; i++){
    		
    		if(num[i] % 3 == 0){
    			
    			++counterDividible;
    		}
    	}
    	
    	printf("Sum: %d \n Dividible by 3:%d", sum , counterDividible);
        return 0;
       }
    Hmm, still I managed to mess up something again.

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Hello,

    ok here are some suggestions so you can can become better at programming.

    - I do not like to declare variables in a single line because it's hard to read but that it just my prefference. When I change it to a per line basis this shows up:

    Code:
    int num[1000];
    int sum = 0;
    int i = 0;
    int j = 0;
    int numDigits;
    int counterDividible = 0;
    int counterDigits = 0;
    Such a big code of variables might tell you that the procedure is trying to do a lot. This bing us to the other section.

    - Write pseudocode it will guide you so you do not end up with a huge single procedure and you devide it into meaningfull parts. Lets get the pseudocode for your task(s)

    1. Enter numbers until 0 or -1
    2. Get sum of all positive and 2 digit numbers
    3. Get the count of divisible by 3.

    This pseudocode helped us correctl identify several functions:
    Code:
    int isTwoDigitNumber(int number);
    int isDivisibleByThree(int number);
    - Use constants and enumerators to make your whole code read easily
    Code:
    enum {NO_ERRORS, ERROR};
    enum {FALSE, TRUE};
    Finally after all this we have something like the code below. I have left the function implementations for you to do.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* constants and enumerators */
    enum {NO_ERRORS, ERROR};
    enum {FALSE, TRUE};
    
    /* function prototypes */
    int isTwoDigitNumber(int );
    int isPossitive(int );
    int isDivisibleByThree(int );
    
    int main(void) {
        /* Enter numbers until you enter 0 or -1
        and print sum of all positive two-digit numbers entered
        and print how many of these numbers divisible by 3 */
    
        int sum = 0;
        int number = 0;
        int counterDivisible = 0;
        int leave = FALSE;
    
        while (!leave){
            scanf ("%d", &number);
    
            if (number == 0 || number == -1){
                leave = TRUE;
            }else{
    
                if (isPossitive(number) && isTwoDigitNumber(number)){
                    sum += number;
                }
    
                if (isDivisibleByThree(number)){
                    counterDivisible++;
                }
            }
        }
    
        printf("Sum: %d \n Divisible by 3:%d", sum , counterDivisible);
        return NO_ERRORS;
    
    }
    
    
    /* checks if number has 2 digits then TRUE else FALSE */
    int isTwoDigitNumber(int number){
    
    }
    
    
    /* checks if number is possitive then TRUE else FALSE */
    int isPossitive(int number){
    
    }
    
    
    /* checks if number is divisisible by 3 then TRUE else FALSE */
    int isDivisibleByThree(int number){
    
    }

    Hope this helps
    Last edited by fredlo2010; 04-25-2016 at 04:25 PM.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Now you're incrementing j just before the condition is checked, so that num[j] is the element after the one you scanf'd the number into.

    Your while condition seems to be missing a !.

    Code:
    do{         
            scanf("%d", &num[j]);
             
            if(num[j] < 0){
                num[j] = -num[j];
            }
            numDigits = num[j];
    
             
            for( ; counterDigits != 0; counterDigits++){
                counterDigits = numDigits / 10;
            }
             
            if( counterDigits == 2){
                 
                sum += num[j];
            }
             
            j++;//←remove this line
    
             
             
        } while( (!num[j] == 0) || !(num[j] == -1) ); // this is probably what you meant to say here.

    Use prints to troubleshoot your code yourself. When that do-while terminates after the first iteration, you should know that its condition is somehow evaluating to false when you don't expect it to. Printing out both parts of that condition would tell you which one is not doing what you expected. That would expose one of the problems. Doing the same thing above line 31(j++;) would expose the other problem if you hadn't noticed it by then.
    Last edited by ShungTzu; 04-25-2016 at 04:52 PM. Reason: no smilies / false, not true

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    This has turned into a "blind leading the blind" thread.

  7. #7
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    With comments like ^that^, he educates no one, and keeps the this forum a very unpleasant place, especially for anyone trying to learn.

    The one thing he does accomplish in the process is to stroke his own ego(something that only exists in his own mind) which is really what he's here for.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by ShungTzu View Post
    With comments like ^that^, he educates no one, and keeps the this forum a very unpleasant place, especially for anyone trying to learn.

    The one thing he does accomplish in the process is to stroke his own ego(something that only exists in his own mind) which is really what he's here for.
    With "help" like yours all you do is confuse the OP with even worse code. How is that being "pleasant"?

    My comment was accurate. You simply don't know what you're talking about but you are either so full of yourself or so stupid that you don't realize it. You're the one with the "ego" problem. I have absolutely no ego whatsoever.

    I was working on an answer that shows what's wrong in all of the posted code above, but you've made this forum so unpleasant for me that I'm going to curl up and cry for a while instead.

  9. #9
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Quote Originally Posted by algorism View Post
    With "help" like yours all you do is confuse the OP with even worse code. How is that being "pleasant"?

    My comment was accurate. You simply don't know what you're talking about but you are either so full of yourself or so stupid that you don't realize it. You're the one with the "ego" problem. I have absolutely no ego whatsoever.

    I was working on an answer that shows what's wrong in all of the posted code above, but you've made this forum so unpleasant for me that I'm going to curl up and cry for a while instead.
    You're still doing it. Notice that nothing you've said in this thread is even about the code at all? You've posted nothing but insults, more insults, and vague allusions to false or misleading statements on my part without so much as a reference to what they are.

    Whenever you uncurl and make good on your threat to pick his code to death, make sure and actually point out what I said that you think is wrong.

  10. #10
    Registered User
    Join Date
    Dec 2015
    Posts
    92


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void) {
    
    
       
    	int num, sum = 0 , i = 0, counter3 = 0, check;
    	do{
    		
    		scanf("%d", &num);
    		
    		check  = num;
    		for( i = 0; check  != 0; i++){
    			
    			check  = check / 10;
    			
    		}
    		
    		if( i == 2){
    			sum = sum + num;
    		}
    		if(abs(num) % 3 == 0){
    			counter3++;
    		}
    		
    	}while( num != 0 && num != -1 );
    	
    	printf("Sum: %d\nDividble by 3: %d", sum , counter3);
    
    
        return 0;
       }
    Did it without arrays it should be okay now ? Trying to find a bug.
    Last edited by High Voltage; 04-25-2016 at 11:59 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I recommend that you redo from scratch. This time, write a bit, compile and test, then fix the bugs. When you are satisfied the small part you wrote works, write a bit more, compile and test, etc.

    In particular, handle this first:
    Enter numbers until you enter 0 or -1
    Notice that you do not need an array for this. Of course, just entering numbers would not be very informative, so you could arrange to print each number after it is entered, except for the terminating value of 0 or -1.

    Once you have a loop that works perfectly, do this:
    and print sum of all positive two-digit numbers entered
    Notice again that you still do not need an array: you do need to identify the positive two-digit numbers (fredlo2010's suggestion of a function is fine, though it is sufficiently trivial that you don't need one) one by one, but you only need a variable to keep track of their sum.

    Finally, after you have tested and are sure that everything works, do this:
    and print how many of these numbers divisible by 3
    Again, notice that an array is not needed: you just need to identify those numbers that are divisible by 3, one by one, and then have a counter to keep track of how many of them there are.
    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

  12. #12
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by laserlight View Post
    I recommend that you redo from scratch. This time, write a bit, compile and test, then fix the bugs. When you are satisfied the small part you wrote works, write a bit more, compile and test, etc.

    In particular, handle this first:
    Enter numbers until you enter 0 or -1
    Notice that you do not need an array for this. Of course, just entering numbers would not be very informative, so you could arrange to print each number after it is entered, except for the terminating value of 0 or -1.

    Once you have a loop that works perfectly, do this:
    and print sum of all positive two-digit numbers entered
    Notice again that you still do not need an array: you do need to identify the positive two-digit numbers (fredlo2010's suggestion of a function is fine, though it is sufficiently trivial that you don't need one) one by one, but you only need a variable to keep track of their sum.

    Finally, after you have tested and are sure that everything works, do this:
    and print how many of these numbers divisible by 3
    Again, notice that an array is not needed: you just need to identify those numbers that are divisible by 3, one by one, and then have a counter to keep track of how many of them there are.
    Big thank you @laserlight you always have constructive and informative answers that really help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void) {
    
    
      int num, sum = 0 , i = 0, counter3 = 0, check;    
    
      do{
            
            scanf("%d", &num);
            check  = num;
            for( i = 0; check  != 0; i++){
                
                check  = check / 10;
                
            }
            
            if( i == 2){
                sum = sum + num;
            }
            if(abs(num) % 3 == 0 && num != 0 && num != -1){
                ++counter3;
            }
        }while( num != 0 && num != -1);
        
        printf("Your sum:%d\nDividible by 3: %d", sum, counter3);
    
    
        return 0;
       }
    Works like a charm
    Last edited by High Voltage; 04-26-2016 at 01:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Stops Responding.
    By KittenAqua in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2011, 05:26 PM
  2. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:18 AM
  3. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  4. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  5. Program stops executing
    By bargomer in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2008, 12:05 PM