code not working as it should..

This is a discussion on code not working as it should.. within the C Programming forums, part of the General Programming Boards category; 11111...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    code not working as it should..

    11111
    Last edited by transgalactic2; 12-05-2008 at 11:55 AM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm I didn't want to be a jerk but you twisted my arm....

    Quote Originally Posted by transgalactic2 View Post
    Code:
          void main(                                                                void)
          {
       int i;
       flush(stdin);
    return i;
    }
    Last edited by master5001; 12-05-2008 at 12:10 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And also this sentence makes no sense:
    But the debugger shows that it doesnt go to the main for
    so it does only the option for 1.
    ??
    I have no idea what you think you mean there.

    But anyway, you need to set count equal to 0 each time, before you start adding things to it.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    111111
    Last edited by transgalactic2; 12-05-2008 at 11:55 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, but if you miss you don't bring count back down ever. So in your 21 example, you start with 2 and you add up to 6; that makes the total 22. So that loop stops, the if is false, and we start over with 3. We add 3, and now we're at 25. The if is false, and we start over at 4. We add 4, and now we're at 29. The if is false.....

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    1111
    Last edited by transgalactic2; 12-05-2008 at 11:55 AM.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks i solved it

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Don't edit out your problems! It's good reference for people searching for solutions to problems!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i enter 7

    it gives me 7=3+4+5
    ???
    Code:
    #include <stdio.h>
    
    int main(){
    
       //start of option1 code
    int index,tndex,kndex,jndex;
    int num,count,count_up;
    count=0;
    printf("Enter a natural number\n");
    scanf("&#37;d",&num);
    
     for(index=1;index<num;index++){//start for1
        count=0;
         for(tndex=index;count<num;tndex++){//start for2
    
         count=count+tndex;
    
         }//end for2
         count_up=count;
    
         tndex--;
         if (count==num){//start if equal
             for(kndex=tndex;count>0;kndex--){//start inner for
    
              count=count-kndex;
    
             }//end inner for
             kndex++;
             printf("%d=%d",num,kndex);
             count_up=0;
             kndex++;
             for(jndex=kndex;count_up<num-1;jndex=jndex+1){//start for
    
                 count_up=count_up+jndex;
                  printf("+%d",jndex);
    
             }//end for
             printf("\n");
         }//end if equal
         else{
         printf("X can't be written as sum of sequential integers");
         }
     }//end for1
     //end of option1 code
    }//end main

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Maybe your program is actually generating results that we have all been overlooking in math these past millenia. Fix your indendation please, and maybe I will read your code.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Maybe you should set count_up equal to kndex, instead of 0, since at that point in the program you've already dealt with kndex.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    The problem is with the ELSE (when it differs).
    When i delete the content of else it gives me all the results of 21.
    But when i put a massage in it i get only the first resolt of 21

    Code:
    #include <stdio.h>
    
    int main(){
    
       //start of option1 code
    int index,tndex,kndex,jndex;
    int num,count,count_up;
    count=0;
    printf("Enter a natural number\n");
    scanf("&#37;d",&num);
    
     for(index=1;index<num;index++){//start for1
        count=0;
         for(tndex=index;count<num;tndex++){//start for2
    
         count=count+tndex;
    
         }//end for2
    
    
         count_up=count;
    
         tndex--;
         if (count==num){//start if equal
    
             for(kndex=tndex;count>0;kndex--){//start inner for
    
              count=count-kndex;
    
             }//end inner for
             kndex++;
             printf("%d=%d",num,kndex);
             count_up=0;
             kndex++;
    
             for(jndex=kndex;count_up<num-1;jndex=jndex+1){//start for
    
                 count_up=count_up+jndex;
                  printf("+%d",jndex);
    
             }//end for
             printf("\n");
    
         }//end if equal
         else{
    
         }
     }//end for1
     //end of option1 code
    }//end main

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your else statement didn't include "return", did it?

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the best way to do this thing ??
    i am going the wrong way with this code
    if you have some algorithm i can write a new code
    i dont now where is my problem

    i added this return 0 line in the end
    but for 21 i get
    10+11+12
    Code:
    #include <stdio.h>
    
    int main(){
    
       //start of option1 code
    int index,tndex,kndex,jndex;
    int num,count,count_up;
    count=0;
    printf("Enter a natural number\n");
    scanf("&#37;d",&num);
    
     for(index=1;index<num;index++){//start for1
        count=0;
         for(tndex=index;count<num;tndex++){//start for2
    
         count=count+tndex;
    
         }//end for2
         count_up=count;
    
         tndex--;
         if (count==num){//start if equal
             for(kndex=tndex;count>0;kndex--){//start inner for
    
              count=count-kndex;
    
             }//end inner for
             kndex++;
             printf("%d=%d",num,kndex);
             count_up=0;
             kndex++;
             for(jndex=kndex;count_up<num-1;jndex=jndex+1){//start for
    
                 count_up=count_up+jndex;
                  printf("+%d",jndex);
    
             }//end for
             printf("\n");
         }//end if equal
         else{
    
         }
     }//end for1
     //end of option1 code
     return 0;
    }//end main

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    what is the best way to do this thing ??
    i dont now where is my problem
    You could read what people are telling you, that might be a good start. Or you could (heaven forfend) actually trace your code with a piece of paper and watch what happens, that would work too (perhaps even better in the long run).

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

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

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