1

i'm using code-blocks 16.01 when i debug this code it show me a correct output but when i run it it show incorrect output! how to solve this?

int main()
{
    char ch[100],var[100],val[100],tempVa[100];
    int i = 0,j=0,count=0;
    while (1)
    {
        puts("Enter the expression (or (end) to exit):");
        gets(ch);
        if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0)
            exit(-1);
        else if(2 == sscanf(ch,"%s = %s", var, val))
        {   i = 0;
            printf("Variable is : %s\t Value Before evaluating : %s\n",var, val);
            while (i<=strlen(val))
            {
                while (val[i]!='-'&&val[i]!='%'&&val[i]!='/'&&val[i]!='*'&&val[i]!='+'&&i<strlen(val))
                    tempVa[j++]=val[i++];

                i++;
                for (count=0; count<strlen(tempVa); count++)
                    printf("%c", tempVa[count]);
                for (count=strlen(tempVa); count>=0; count--)
                    tempVa[count]='\0';
                j=0;
            }
        }
        else
            printf("Invalid!");
    }
    return 0;
}

Smaple Input : Hassan = Merna+Mohamed+Ahmed

Debugging OutputDebug

Run Output Run

From where does those garbage come?!

10
  • Your images are not working. Could you write the output in text format? Commented May 15, 2017 at 21:04
  • 1
    hint: you need to null-terminate tempVa after you copy contents into it. In debug, it is likely zeroed out at start, but in release, it's just whatever's in the memory at the time. Commented May 15, 2017 at 21:04
  • @ErikW Run : i.sstatic.net/mmR5e.png Debug : MernaMohamedAhmed Commented May 15, 2017 at 21:07
  • @Joe` for (count=strlen(tempVa); count>=0; count--) tempVa[count]='\0';` i already Nulled it here i think. Commented May 15, 2017 at 21:09
  • Another pic of Run, sometimes it works, some times it doesn't work .. i don't know what is the reason :( link Commented May 15, 2017 at 21:11

1 Answer 1

1

I tested your code and it works Edits:

  1. You should import the string.h library (anyways, you should always resolve any warning you have).
  2. Use return -1, that is why main is an int function.
  3. Like @joe said in the comments, you should always terminate your string with '\0'.

Code:

#include <stdio.h>
#include <string.h> // edit

int main()
{
    char ch[100], var[100], val[100], tempVa[100];
    int i = 0, j = 0, count = 0;
    while (1)
    {
        puts("\nEnter the expression (or (end) to exit):");
        gets(ch);
        if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0)
            return -1; // edit
        else if(2 == sscanf(ch, "%s = %s", var, val))
        {
            i = 0;
            printf("Variable is : %s\t Value Before evaluating : %s\n", var, val);
            while (i <= strlen(val))
            {
                while (val[i] != '-' && val[i] != '%' && val[i] != '/' && val[i] != '*' && val[i] != '+' && i < strlen(val))
                    tempVa[j++] = val[i++];

                i++;
                for (count = 0; count < strlen(tempVa); count++)
                    printf("%c", tempVa[count]);
                for (count = strlen(tempVa); count >= 0; count--)
                    tempVa[count] = '\0';
                j = 0;
            }
        }
        else
            printf("Invalid!");
    }
    return 0;
}

Sample run:

Enter the expression (or (end) to exit): Hassan = Merna+Mohamed+Ahmed
Variable is : Hassan Value Before evaluating : Merna+Mohamed+Ahmed MernaMohamedAhmed
Enter the expression (or (end) to exit):

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.