need some debugging help

This is a discussion on need some debugging help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> main() { int num1; int sum=0; printf("enter a positive number: "); num1= getc(stdin); for (num1=0; num1<4; num1++) ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    need some debugging help

    Code:
    #include <stdio.h>
    main()
    {
        int num1;
        int sum=0;
        
    
        printf("enter a positive number: ");
        num1= getc(stdin);
            
        for (num1=0; num1<4; num1++)
    
        if (num1 == 1)
    
            printf("Message I, num1 is %d\n", num1);
    
        else sum=((num1-1) + 2*(num1-1));
    
            printf("Message II, sum is %d\n", sum);
            
        return 0;
    }
    someone help me with this if statement, for some reason when the number i enter is 1, the program should end there, right? instead it goes and computes the else part also.

    edit:nvm
    Last edited by InvariantLoop; 02-09-2005 at 05:36 PM.
    When no one helps you out. Call google();

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
        else sum=((num1-1) + 2*(num1-1));
    
            printf("Message II, sum is %d\n", sum);
    The only part of your else statement is the sum=... part. The printf() will always be executed. If you want the printf() to be part of the else then you'll need to use braces.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    If a block of code in an if/else statement is longer than one line, you need to put curly braces around that block of code.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    if i put braces around the if/else statement then i get nothing, i just enter the number and then it says press a key to continue.

    edit: nvm i had changed something in the for loop.
    When no one helps you out. Call google();

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Post the code with the curly braces. Are you sure the FAQ entry on the subject of why your program is closing too quickly is not helpful in this case?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    #include <stdio.h>
    main()
    {
        int num1;
        int sum=0;
        //int i;
    
        printf("enter a positive number: ");
        num1= getc(stdin);
            
        for (num1=0; num1<4; num1++)
    
        {
        if (num1==1)
    
            printf("Message I, num1 is %d\n", num1);
        
        else sum =((num1-1) + 2*(num1-1));
    
            printf("Message II, sum is %d\n", sum);
        }
        
        return 0;
    }
    ok that worked but now i have another question. the results i get is this.
    Code:
     enter a positive number: 2
    Message II, sum is -3
    Message I, num1 is 1
    Message II, sum is -3
    Message II, sum is 3
    Message II, sum is 6
    Press any key to continue
    how do i eliminate the message I, num1 is 1?

    edit: ok this isnt right, i get the same result for every number that i enter, how is that possible? second i should get message 1 only when the number i enter is equal to 1, but im not getting that here. some suggestions?
    Last edited by InvariantLoop; 02-09-2005 at 06:01 PM.
    When no one helps you out. Call google();

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    1. Why are you even bothering to input num1 since you set it to zero in the for loop?
    Code:
    num1= getc(stdin);
            
        for (num1=0; num1<4; num1++)
    
        {
    2. You still need curly braces:
    Code:
    else sum =((num1-1) + 2*(num1-1));
    
            printf("Message II, sum is %d\n", sum);
    should be
    Code:
    else 
    {
         sum =((num1-1) + 2*(num1-1));
         printf("Message II, sum is %d\n", sum);
    }
    3. This statement:
    Code:
    printf("enter a positive number: ");
    num1= getc(stdin);
    Is giving you the char code for whatever you type in not the number. Aka if I typed in 1, num1 would contain 49.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I was thinking more like this - curly braces around both the loop code and the else code. You can leave them off the if code, since that only contains one line.

    Code:
    #include <stdio.h>
    main()
    {
        int num1;
        int sum=0;
        //int i;
        printf("enter a positive number: ");
        num1= getc(stdin);       
        for (num1=0; num1<4; num1++)
        {
            if (num1==1) 
                printf("Message I, num1 is %d\n", num1);
            else
            {
                sum =((num1-1) + 2*(num1-1));
                printf("Message II, sum is %d\n", sum);
            }
        return 0;
    }

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    #include <stdio.h>
    
    int main()
    {
        int num1;
        int sum=0;
        //int i;
        printf("enter a positive number: ");
        num1= getc(stdin);       
        for (num1=0; num1<4; num1++)
        {
            if (num1==1) 
                printf("Message I, num1 is %d\n", num1);
            else
            {
                sum =((num1-1) + 2*(num1-1));
                printf("Message II, sum is %d\n", sum);
            }
       } //<--------missed one 
       return 0;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by andyhunter
    3. This statement:
    Code:
    printf("enter a positive number: ");
    num1= getc(stdin);
    Is giving you the char code for whatever you type in not the number. Aka if I typed in 1, num1 would contain 49.
    thats true. but the book im reading hasnt or has forgotten to put something about how to enter ints. so maybe this is why im getting always the same results, how do i enter a int that actually will count as an int?
    When no one helps you out. Call google();

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I believe scanf will do that.

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well you could use scanf for one:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
         int myNumber;
    
         printf("Enter a number: ");
         scanf("%d", &myNumber);
    
         myNumber = myNumber + 1;
    
         printf("1 added to your number is %d", myNumber);
    
         return 0;
    }
    Here is a tut on getting numbers from the user.
    Last edited by andyhunter; 02-09-2005 at 06:18 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    jesus that for loop had me going on the wrong direction. thanks for the input folks. i figured it out.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
               int myNumber;
    	 int sum=0;
    
         printf("Enter a number: ");
         scanf("%d", &myNumber);
    
    	{
    	if (myNumber==1)
    
    		printf("Message I, you entered is %d\n", myNumber);
    	
    	else 
    	{
    		sum =((myNumber-1) + 2*(myNumber-1));
    
    	printf("Message II, sum is %d\n", sum);
    		}
    	}
    
         return 0;
    }
    thanks a lot
    When no one helps you out. Call google();

  14. #14
    Registered User
    Join Date
    Feb 2005
    Posts
    1
    Look people i can't suggest anything. We also have problems regarding PHP, we are having our On-the-job-training and we were asked to work on web designing using PHP4. We don't have any idea about it. We are hoping that anyone could help us. Thanks!

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by Shera
    Look people i can't suggest anything. We also have problems regarding PHP, we are having our On-the-job-training and we were asked to work on web designing using PHP4. We don't have any idea about it. We are hoping that anyone could help us. Thanks!
    ????
    When no one helps you out. Call google();

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 09:48 AM
  2. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  3. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 08:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21