-2

The C program below does not throw an error but does not work either, and gives garbage value. Please tell me why?

I am using Codeblock editors.The program runs fine when I do not use the array in the program.But I have thoroughly read it and I didn't find any mistake.

#include<stdio.h>

int main(){
    char name1[27];
    int maths;
    int physics;
    int Cs;
    int bio;
    int english;
    int urdu;
    int ps;
    int isl;

    printf("What is your name : ");
    scanf(" %s",name1);
    printf("\n\nwhat is your score in Mathematics : ");
    scanf(" %d",&maths);
    printf("\n\nwhat is your score in physics : ");
    scanf(" %d",&physics);
    printf("\n\nwhat is your score in computer science : ");
    scanf(" %d",&Cs);
    printf("\n\nwhat is your score in biology : ");
    scanf(" %d",&bio);
    printf("\n\nwhat is your score in English : ");
    scanf(" %d",&english);
    printf("\n\nwhat is your score in Urdu : ");
    scanf(" %d",&urdu);
    printf("\n\nwhat is your score in Pakistan study : ");
    scanf(" %d",&ps);
    printf("\n\nwhat is your score in Islamiat : ");
    scanf(" %d",&isl);

    printf("\n****************************************************************************************");
printf("\nName            Mathematics    Physics    Com.sc    Biology    English    Urdu    Pak-Study    Islamiat");
    printf("\n****************************************************************************************");
    printf("\n%-16s%-15d%-11d%-10d%-11d%-11d%-8d%-13d%0d",name1,maths,physics,Cs,bio,english,urdu,ps,isl);
    return (0);
}
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Muslim
  • 45
  • 5
  • 1
    What kind of _garbage_? The code look fine as far as name is less then 26 chars. – LPs Dec 16 '16 at 08:47
  • 1
    Note that you cannot use a space while entering the name. %s will only scan upto a space character – Rishikesh Raje Dec 16 '16 at 08:51
  • Possible duplicate of [scanf Getting Skipped](http://stackoverflow.com/questions/14484431/scanf-getting-skipped) – Elcan Dec 16 '16 at 08:52
  • 1
    The code works unless you put a space inside the name for some reason – Elcan Dec 16 '16 at 08:55
  • @FlorentUguet Well, maybe you got the point. – LPs Dec 16 '16 at 08:55
  • (a) The data you're providing for input is relevant and should have been provided with your post. (b) Not a single IO operation in this program is actually checked for success/failure. Doing so would probably have pointed you to the problem almost immediately. – WhozCraig Dec 16 '16 at 08:56
  • Yeah, I first answered before @Rishikesh Raje showed that issue – Elcan Dec 16 '16 at 08:57

1 Answers1

0

If the problem is entering name with spaces, like Name and Surname you should use fgets to retrieve input string and remove manually the newline char

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

int main(void)
{
    char name1[27];
    int maths;
    int physics;
    int Cs;
    int bio;
    int english;
    int urdu;
    int ps;
    int isl;

    printf("What is your name : ");
    fgets(name1, sizeof(name1), stdin);
    name1[strcspn(name1, "\n")] = 0;

    printf("\n\nwhat is your score in Mathematics : ");
    scanf(" %d",&maths);
    printf("\n\nwhat is your score in physics : ");
    scanf(" %d",&physics);
    printf("\n\nwhat is your score in computer science : ");
    scanf(" %d",&Cs);
    printf("\n\nwhat is your score in biology : ");
    scanf(" %d",&bio);
    printf("\n\nwhat is your score in English : ");
    scanf(" %d",&english);
    printf("\n\nwhat is your score in Urdu : ");
    scanf(" %d",&urdu);
    printf("\n\nwhat is your score in Pakistan study : ");
    scanf(" %d",&ps);
    printf("\n\nwhat is your score in Islamiat : ");
    scanf(" %d",&isl);

    printf("\n****************************************************************************************");
    printf("\nName            Mathematics    Physics    Com.sc    Biology    English    Urdu    Pak-Study    Islamiat");
    printf("\n****************************************************************************************");
    printf("\n%-16s%-15d%-11d%-10d%-11d%-11d%-8d%-13d%0d",name1,maths,physics,Cs,bio,english,urdu,ps,isl);
    return (0);
}
LPs
  • 16,045
  • 8
  • 30
  • 61