Sign in with
Sign up | Sign in
Your question
Solved

C program cannot be debugged!! PLEASE HELP!

Last response: in Business Computing
Share
March 18, 2014 3:59:04 AM

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

  1. #include<stdio.h>
  2. main()
  3. {
  4. int a,b,sum;
  5. printf("\nEnter two numbers for sum: \n");
  6. scanf("%d %d", &a, &b);
  7. sum=a+b;
  8. printf("The sum is = %d",sum);
  9.  
  10. int c,d,diff;
  11. printf("\nEnter two numbers for difference: \n");
  12. scanf("%d %d",&c,&d);
  13. diff=c-d;
  14. printf("The difference is = %d",diff);
  15.  
  16. int e,f,multi;
  17. printf("\nEnter two numbers for multiplication\n");
  18. scanf("%d %d", &e,&f);
  19. multi=e*f;
  20. printf("The multiplication = %d\n",multi);
  21.  
  22. char Q[1];
  23. printf("Do you want to check for the largest result among\n");
  24. printf("the sum,difference and multiplication?\n");
  25. printf("Press Y for yes / N for no:\n");
  26. scanf("%s",Q);
  27.  
  28. if (Q== "Y" || Q== "y")
  29. {
  30.  
  31. if(sum>diff)
  32.  
  33. {
  34. if(sum>multi)
  35. printf("The largest number evaluated is sum= %d",sum);
  36. else
  37. printf("The largest number evaluated is multiplication= %d",multi);
  38. }
  39.  
  40. else
  41. {
  42. if(diff>multi)
  43. printf("The largest number evaluated is difference= %d",diff);
  44. else
  45. printf("The largest number evaluated is multiplication= %d",multi);
  46. }
  47. }
  48. else
  49. {
  50. printf("Thanks");
  51. }
  52.  
  53. }

<[ code ] 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!

More about : program debugged

March 18, 2014 7:45:26 PM

You logic is backwards. Replace your 'Y's with 'N's.
m
0
l
March 19, 2014 5:28:54 AM

Isnt it all due to character coding ? You should use ASCII 'numbers' insted ?
m
0
l
Related resources

Best solution

March 19, 2014 6:21:26 AM

I see two problems with the following line:
  1. 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.
Share
March 22, 2014 11:20:40 AM

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? :) 
m
0
l
!