#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Error in a simple c program


    Hi,
    I am a beginner to c language
    check the error
    I have created a simple c program but its not giving the solution not asking not numbers , only it asks name and than print all the statements with 00.000 answer


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    float a,b,c,e;
    char d;

    printf(" ============================================================================== \n");
    printf(" \t \t \t Welcome To This Software \n \n");
    printf(" ============================================================================== \n");
    printf("This program can calculate your agregate according to rules and regulations \n of University Of Gujrat \n");
    printf("\n Enter Your Name : ");
    scanf("%s",&d);
    printf("\n Enter Your Marks In Matriculations Exams : ");
    scanf("%f",&a);
    printf("\n Enter Your Marks In Fsc. : ");
    scanf("%f",&b);
    printf("\n Enter Your Marks You Obtained in NTS : ");
    scanf("%f",&c);
    e=(a/1050*10)+(b/1100*60)+(c/100*30);
    printf("\n %c, Your agregate according to UOG is %f"s,e);
    getch();
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Close. You could use the %s format specifier, and you need an array to store the name.
    Code:
    #include<stdio.h>
    int main() {
      float a,b,c,e;
      char d[128];			/* space for a long name */
    
      printf(" ============================================================================== \n");
      printf(" \t \t \t Welcome To This Software \n \n");
      printf(" ============================================================================== \n");
      printf("This program can calculate your agregate according to rules and regulations \n of University Of Gujrat \n");
      printf("\n Enter Your Name : ");
      scanf("%99s",d);		/* don't overrun the buffer */
      printf("\n Enter Your Marks In Matriculations Exams : ");
      scanf("%f",&a);
      printf("\n Enter Your Marks In Fsc. : ");
      scanf("%f",&b);
      printf("\n Enter Your Marks You Obtained in NTS : ");
      scanf("%f",&c);
      e=(a/1050*10)+(b/1100*60)+(c/100*30);
      printf("\n %s, Your agregate according to UOG is %f\n",d,e);
      puts("fix your console!");
      getchar();
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Note that there's a syntax error in the code, so if you are running the program and getting output, then it from a previous version.

    Code:
    printf("\n %c, Your agregate according to UOG is %f"s,e);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    b49P23TIvg,
    Sir
    I am new to this and want to learn,
    so please tell me all what you have done with the program I've sent and what are the mistaked done by me and how you have correct them PLEASE sir
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Close. You could use the %s format specifier, and you need an array to store the name.
    Code:
    #include<stdio.h>
    int main() {
      float a,b,c,e;
      char d[128];			/* space for a long name */
    
      printf(" ============================================================================== \n");
      printf(" \t \t \t Welcome To This Software \n \n");
      printf(" ============================================================================== \n");
      printf("This program can calculate your agregate according to rules and regulations \n of University Of Gujrat \n");
      printf("\n Enter Your Name : ");
      scanf("%99s",d);		/* don't overrun the buffer */
      printf("\n Enter Your Marks In Matriculations Exams : ");
      scanf("%f",&a);
      printf("\n Enter Your Marks In Fsc. : ");
      scanf("%f",&b);
      printf("\n Enter Your Marks You Obtained in NTS : ");
      scanf("%f",&c);
      e=(a/1050*10)+(b/1100*60)+(c/100*30);
      printf("\n %s, Your agregate according to UOG is %f\n",d,e);
      puts("fix your console!");
      getchar();
      return 0;
    }

    Pasting your code work fine till we have to enter name, After that screen blinks and goes back to coding screen
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    You'd better put the getch() back into your program.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I think it's the old "getchar() is reading in the ENTER left over from the last scanf" trick. Third time they've fallen for it this week.

    May I suggest
    scanf(" %c", %ch); // of course you'd have to declare char ch; as well
    Be sure to include that space before the "%", because that's what will skip over all the white space preceding the letter you enter. Though I don't know what it will do if you only hit the ENTER key. I never have to worry about that, since I always run console programs from the console as they were intended to be run.

IMN logo majestic logo threadwatch logo seochat tools logo