Solved

C program cannot be debugged!! PLEASE HELP!

Here is a program that i have written to add,substract and multiply each two different numbers respectively:

#include<stdio.h>
main()
{
int a,b,sum;
printf("\nEnter two numbers for sum: \n");
scanf("%d %d", &a, &b);
sum=a+b;
printf("The sum is = %d",sum);

int c,d,diff;
printf("\nEnter two numbers for difference: \n");
scanf("%d %d",&c,&d);
diff=c-d;
printf("The difference is = %d",diff);

int e,f,multi;
printf("\nEnter two numbers for multiplication\n");
scanf("%d %d", &e,&f);
multi=e*f;
printf("The multiplication = %d\n",multi);

char Q[1];
printf("Do you want to check for the largest result among\n");
printf("the sum,difference and multiplication?\n");
printf("Press Y for yes / N for no:\n");
scanf("%s",Q);

if (Q== "Y" || Q== "y")
{
	
	if(sum>diff)
	
		{
			if(sum>multi)
			printf("The largest number evaluated is sum= %d",sum);
			else
			printf("The largest number evaluated is multiplication= %d",multi);
		}
	
	else
		{
			if(diff>multi)
			printf("The largest number evaluated is difference= %d",diff);
			else
			printf("The largest number evaluated is multiplication= %d",multi);
		}
}			
else
{
printf("Thanks");
}

}


< tags added by moderator>

When i run this program in linux it terminates after giving input Y/y to variable Q.

And when i run it in dosbox, after giving Y/y to the variable Q it prints the last else statement "Thanks".

Either of the dosbox or linux debugg would be kind!
4 answers Last reply Best Answer
More about program debugged
  1. You logic is backwards. Replace your 'Y's with 'N's.
  2. Isnt it all due to character coding ? You should use ASCII 'numbers' insted ?
  3. Best answer
    I see two problems with the following line:
    
    scanf("%s",Q);
    

    #1: You must use &Q as a pointer to the char variable Q. You cannot access a variable directly in a scanf statement.
    #2: You have Q defined as a char, yet try to input a character string into it. This is a mismatch of character types. Instead of "%s", you should be using "%c".

    Not using the & to make a pointer to a variable within the scanf statement is a very common mistake, and is very difficult to debug for a lot of people.
  4. peterorl said:
    Isnt it all due to character coding ? You should use ASCII 'numbers' insted ?


    What you mean Ascii numbers? Can u give an illustration pleasE? :)
Ask a new question

Read More

Business Computing