1

I have a piece of code shown below

      #include <stdio.h>
      #include <stdlib.h>
      void Advance_String(char [2],int );
      int Atoi_val;
      int Count_22;
      int Is_Milestone(char [2],int P2);
      char String[2] = "0";
      main()
      {
         while(1)
         {

           if(Is_Milestone(String,21)==1)
           {
                 if(atoi(String)==22)
                 {
                     Count_22 = Count_22 + 1;
                 }
           }
           Atoi_val = atoi(String);
         Advance_String(S,Atoi_val); 
         }
       }
      int Is_Milestone(char P1[2],int P2)
      {
             int BoolInit;
             char *Ptr = P1;

             int value = atoi(Ptr);
             BoolInit = (value > P2);
             return BoolInit;
      }
     void Advance_String(char P1[2],int Value)
     {

             if(Value!=7)
             {
               P1[1] = P1[1]+1;
             }
             else
             {
               P1[1] = '0';
                  P1[0] = P1[0]+1 ;
             }
     }

Now my problem is Count_22 never increments as the char increments never achieves the value 21 or above.Could anyone please tell me the reason for this unexpected behaviour?My question here is to find the value of Count_22.Is there any problem with the code?

Thanks and regards,

Maddy

maddy
  • 827
  • 2
  • 11
  • 19
  • 4
    Where is S defined and what is it? Before posting try to make sure your code compiles first. – JRL Apr 29 '10 at 11:58
  • 2
    Note, char String[2] = "00"; is wrong - it takes a 3 char array to store a 2 char string (plus one for the null terminator). Don't you get a compiler warning for that? I don't think that's the actual source of your problem but you could get other issues from that. – Vicky Apr 29 '10 at 12:00
  • What is S? This code compiles? Because S is undeclared. – vladv Apr 29 '10 at 12:00
  • 1
    if(Is_Milestone(S,21==1) will also not compile. – Vicky Apr 29 '10 at 12:03
  • ...and not a single line of comment in sight. Beautiful. – DevSolar Apr 29 '10 at 12:07
  • I had edited the questioin.I had changed the value in the array and changed the string identified.The name is string instead of S – maddy Apr 29 '10 at 12:20
  • @maddy: but you still forgot a parenthesis at line, `if(Is_Milestone(String,21==1)`. You really should copy/past compiling code instead of editing in SO. – kriss Apr 29 '10 at 12:25
  • @Vicky: char String[2] = "00"; is perfectly legal C, check in standard if you want. The terminating 0 of "00" in only copied if there is room enough in initialized array. This is handy when you really use char x[] as arrays not strings. (think of things like char hex[16" = "0123456789ABCDEF";) – kriss Apr 29 '10 at 12:42
  • The actuall code is very big.i had just pasted the piece of code which is stuck in my case. – maddy Apr 29 '10 at 12:46
  • @maddy: see my answer, there is some clue. Basically you should think more, and write less code and overall **explain what you want to achieve** (even for yourself). If the sample code is typical of the rest you would need a miracle to make it work. – kriss Apr 29 '10 at 13:27
  • @kriss...Thanks for the advice u had shown.Here after i will make it sure that i post code which compiles actually.I shall post a very simple piece of code which is really erasy to comprehend now. – maddy Apr 30 '10 at 04:53

2 Answers2

3

Your code is probably one of the worst pieces of C code i've ever seen (no offense, everybody has to learn sometime).

It has syntax errors (maybe copy/paste problem), logical problems, meaningless obfuscation, bad practices (globals), buffer overflow (atoi used on a char where there is no place to store the terminating zero byte), uninitialized values (Count_22), surprising naming convention (mixed CamelCase and underscore, variables and functions beginning with capital letter), infinite loop, no header and I forget some.

More, if you want anyone to help you debug this code, you should at list say what it is supposed to do...

To answer to the original question: why Count_22 is never incremented ?

Because Is_Milestone is always false (with or without @Jay change). Is_Milestone intend seems to be to compare the decimal value of the string "22" with the integer 21 (or 1, boolean result of 21 == 1) depending on the version).

It's logical because of Advance_String behavior. both because String has bad initial value (should probably be char String[3] = "00";) and because of the Value != 7 test. I guess what you wanted was comparing the digit with 7, but atoi works with a full string. Another minor change to achieve that Atoi_val = atoi(String+1); in the body of your loop. Then again you won't see much as the loop never stop and never print anything.

If it is a first attempt at an exercice given by some teacher (something like "programming a two digit counter in base 7" or similar). You should consider not using atoi at all and converting characters digit to value using something like:

digit_value = char_value - '0';

example:

char seven_as_char = '7';
int seven_as_int = seven_as_char - '0';

If you can explain what you are really trying to do, we may be able to show you some simple sample code, instead of the horror you are trying to debug.

EDIT

It is really more simple with original code...

After reading the Ada source, I can confirm it is indeed an Ascii based octal counter. The original code is allready of poor quality, and that explains part of the bad quality of the resulting C code.

A possible direct port could be as following (but still need a serious cleanup to look like native C code... and is quite dumb anyway as it prints a constant):

#include <stdio.h>
#include <stdlib.h>

void Advance_String(char * P1)
{
     if((P1[1]-'0') != 7){
         P1[1]++;
     }
     else{
         P1[1] = '0';
         P1[0]++ ;
     }
}


int Is_Milestone(char * P1, int P2)
{
    return (atoi(P1) > P2);
}

main()
{
    int Count_11 = 0;
    int Count_22 = 0;
    int Count_33 = 0;
    int Count_44 = 0;
    char S[3] = "00";

    int cont = 1;

    while(cont)
    {
        if(Is_Milestone(S, 10)){
            if(atoi(S) == 11){
                Count_11 = Count_11 + 1;
            }
            if(Is_Milestone(S, 21)){
                if(atoi(S) == 22){
                    Count_22 = Count_22 + 1;
                }
                if(Is_Milestone(S, 32)){
                    if(atoi(S) == 33){
                        Count_33 = Count_33 + 1;
                    }
                    if(Is_Milestone(S, 43)){
                        if(atoi(S) == 44){
                            Count_44 = Count_44 + 1;
                        }
                        if (atoi(S) == 77){
                            cont = 0;
                        }
                    }
                }
            }
        }
        Advance_String(S);
    }
    printf("result = %d\n", Count_11 + Count_22 + Count_33 + Count_44);
}
kriss
  • 23,497
  • 17
  • 97
  • 116
  • Kriss.Thanks a lot for the valuable suggestions given.Well this was not exactly a c question or some excercise to be solved.I was given some ada files and task was required to convert them to the C files.So i cant just do any work around as i had done the conversion without changing the algorithms. – maddy Apr 30 '10 at 07:08
  • @maddy: the original ada code is probably easier to read that the currnt C code. Either the original Ada code is quite poor (and some of the errors made in C can't be made using Ada as compiler catch them) or the C port is a failure. Basically it would be much easier to ask question like: "I want to port some Ada code, there is Ada working code, there is my C try, may you help me enhance it". You still didn't said what your program is supposed to do. It's impossible to write a program without knowing that. How would you know you succeeded ? – kriss May 03 '10 at 05:54
  • @Kriss..Do u want me to show up the ada code which i need to port to c? – maddy May 04 '10 at 05:36
  • @maddy: could be a good, idea, but if it's a large piece of code do it on pastbin (http://pastebin.org/pastebin.php) or copypaste (http://www.copypastecode.com) and provide a link. But explaining what you are trying would probably be enough. – kriss May 04 '10 at 07:17
  • @Kriss..http://www.copypastecode.com/27994/ is the place where i had pasted the code.please do have a look at it.Thanks a lot for spending time in this issue. – maddy May 04 '10 at 11:56
  • @Kriss..Thanks a lot kriss for all the help and advice given.I had found out what was missing in my code when i did a comparison with yours.Thousand thanks again for helping me out with this. – maddy May 05 '10 at 03:27
2

This statement

if(Is_Milestone(S,21==1) // Braces are not matching. If statement is not having the closing brace. Compilation error should be given.

should be

if(Is_Milestone(S,21)==1)

I guess.

Also, the code you have posted doesn't seem to be correct. It will surely give compilation errors. You have declared Count22, but are using Count_22.

Please check.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
Jay
  • 24,173
  • 25
  • 93
  • 141
  • @Jay: it is not an answer, merely a comment. If we belive OP he has compiling code as he ask about runtime behavior... – kriss Apr 29 '10 at 12:28
  • @kriss..thats exactly what i am stuck.I am having a compiling code which is infact very big.I had pasted the piece which actually is doubtfull.is there anyway by which Count_22 increments as char elements never reach that value. – maddy Apr 29 '10 at 12:49