Thread: c program to debug

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    c program to debug

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define AM(a,b) ((a+b)/2)
    #define ABS(d) (d>=0?d:(-d))
    #define TOLOWER(ch) (ch=ch+32)
    #define MAX(a,b) (a>=b?a:b)
    void main()
    {
     int d,abst;
     float a,b,am,max;
     char ch='A',cl;
     clrscr();
     printf("\nEnter two numbers:");
     scanf("%f%f",&a,&b);
     am=AM(a,b);
     printf("\nThe arithmetic mean of %f and %f is %f",a,b,am);
     printf("\nEnter a number:");
     scanf("%d",&d);
     abst=ABS(d);
     printf("\nThe absolute value of %d is %d",d,abst);
     printf("\nEnter a uppercase character:");
      scanf("%c",&ch);
     cl=TOLOWER(ch);
     printf("c=%c",cl);
     printf("\nEnter two number:");
     scanf("%f%f",&a,&b);
     max=MAX(a,b);
     printf("\nThe bigger of %f and %f is %f",a,b,max);
     getch();
    }
    
    the program is not giving the correct output.
    it automatically take character argument.
    please help me.

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    /* debugchar.c */
    
    #include<stdio.h>
    #include<conio.h>
    #define AM(a,b) ((a+b)/2)
    #define ABS(d) (d>=0?d:(-d))
    #define TOLOWER(ch) (ch=ch+32)
    #define MAX(a,b) (a>=b?a:b)
    int  main()   <== int main see faq  changed from void
    {
     int d,abst;
     float a,b,am,max;
     char ch='A',cl;    <== why do you assign ch = 'A' ? then turn around and scanf ch ?
     clrscr();
     printf("\nEnter two numbers:");  <== two numbers for what ? better description needed.
     scanf("%f%f",&a,&b);
     am=AM(a,b);
     printf("\nThe arithmetic mean of %f and %f is %f",a,b,am);
     printf("\nEnter a number:");
     scanf("%d",&d);
     abst=ABS(d);
     printf("\nThe absolute value of %d is %d",d,abst);
     printf("\nEnter a uppercase character:");
      scanf("%c",&ch);
     cl=TOLOWER(ch);
     printf("c=%c",cl);
     printf("\nEnter two number:");
     scanf("%f%f",&a,&b);
     max=MAX(a,b);
     printf("\nThe bigger of %f and %f is %f",a,b,max);
     getch();
    }
    what is the correct output ? what is the output you were expecting ?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Kryptkat already mentioned that you need to use int main. Once you do that, you need to make sure main returns an int at the end, usually 0.

    As for the program automatically "taking character argument", I assume you mean the scanf calls act like you typed in data when you didn't. You actually did type in multiple characters, namely the enter key (a newline character, or '\n' in C). scanf leaves the newline in the input buffer, so next time you ask for data, there's some waiting. Read this FAQ article: Cprogramming.com FAQ > Flush the input buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 10-18-2009, 02:33 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread