tags:

views:

82

answers:

4
   #include <stdio.h>

    int main(){
    int i;
    char name[10],surname[10],id[12],dpart[20];
    printf("******** ID Documentation ***********\n\n\n");
    printf("1) What is your Name? \n");
    scanf("%s",&name);
    printf("2) What is your Surname? \n");
    scanf("%s",&surname);
    printf("3) What is your ID Number \n");
    scanf("%s",&id);
    printf("4) What is your Department? \n");
    scanf("%[^\n]",&dpart);
    system("CLS");
    for(i=0;i<50;i++){printf("*");}printf("\n");
    printf("|");
    for(i=0;i<48;i++){printf("%%");}
    printf("|");printf("\n");printf("|");
    for(i=0;i<14;i++){printf("%%");}
    printf(" STUDENT ID CARD ");for(i=0;i<17;i++){printf("%%");}printf("|\n");printf("|");
    for(i=0;i<48;i++){printf("%%");}
    printf("|\n");
    printf(" //////// NAME: %6s ",name);for(i=0;i<26;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n");
    printf(" //////// SURNAME: %6s ",surname);for(i=0;i<23;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n");
    printf(" //////// ID NUMVER: %11s ",id);for(i=0;i<16;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n");
    printf(" //////// DEPARTMENT: %22s ",dpart);for(i=0;i<4;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n");
    printf("|");
    for(i=0;i<48;i++){printf("%%");}
    printf("|");printf("\n");
    printf("|");
    for(i=0;i<48;i++){printf("%%");}
    printf("|");printf("\n");
    printf("|");
    for(i=0;i<48;i++){printf("%%");}
    printf("|");printf("\n");for(i=0;i<50;i++){printf("*");}
   }

I was helping one of my friends on his homework. We have to read the department data than display it on a id card, but when it comes to reading department, the program skips it and adds the value entered for id number to department. Ps: I know that using scanf() is a very bad work, but they didn't learned anything else so i cannot suggest using sscanf to him And also don't care the messy code when writing out data, it works

A: 

scanf("%s") generally takes a single word as input, you can get around this using

scanf("%[^\n]s"), which means that you will accept any character but \n as input.

If that's not working use fgets like bjskishore123 suggested. He has an example of using it below.

GWW
I have using it already in the code, but it is not working, not working if i add the s in the end or not
gkaykck
A: 

You need to use fgets instead of scanf to read spaces as well.

Here is the working program

 #include <stdio.h> 

int main(){ 
    int i; 
    char name[10],surname[10],id[12],dpart[20]; 
    char tmpBuf[3];
    printf("******** ID Documentation ***********\n\n\n"); 
    printf("1) What is your Name? \n"); 
    //scanf("%s",&name); 
    fgets(name,8,stdin);
    fgets(tmpBuf, 2, stdin);

    printf("2) What is your Surname? \n"); 
    //scanf("%s",&surname); 
    fgets(surname,9,stdin);
    fgets(tmpBuf, 2, stdin);

    printf("3) What is your ID Number \n"); 
    //scanf("%s",&id); 
    fgets(id,11,stdin);
    fgets(tmpBuf, 1, stdin);

    printf("4) What is your Department? \n"); 
    //scanf("%[^\n]",&dpart); 
    fgets(dpart,19,stdin);
    system("CLS"); 
    for(i=0;i<50;i++){printf("*");}printf("\n"); 
    printf("|"); 
    for(i=0;i<48;i++){printf("%%");} 
    printf("|");printf("\n");printf("|"); 
    for(i=0;i<14;i++){printf("%%");} 
    printf(" STUDENT ID CARD ");for(i=0;i<17;i++){printf("%%");}printf("|\n");printf("|"); 
    for(i=0;i<48;i++){printf("%%");} 
    printf("|\n"); 
    printf(" //////// NAME: %6s ",name);for(i=0;i<26;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n"); 
    printf(" //////// SURNAME: %6s ",surname);for(i=0;i<23;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n"); 
    printf(" //////// ID NUMVER: %11s ",id);for(i=0;i<16;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n"); 
    printf(" //////// DEPARTMENT: %22s ",dpart);for(i=0;i<4;i++){printf("/");}printf(" \n|");for(i=0;i<48;i++){printf("-");}printf("|\n"); 
    printf("|"); 
    for(i=0;i<48;i++){printf("%%");} 
    printf("|");printf("\n"); 
    printf("|"); 
    for(i=0;i<48;i++){printf("%%");} 
    printf("|");printf("\n"); 
    printf("|"); 
    for(i=0;i<48;i++){printf("%%");} 
    printf("|");printf("\n");for(i=0;i<50;i++){printf("*");} 
   } 

Let me know if you still have problems.

bjskishore123
-1 "fflush(stdin)" is undefined behaviour, see ANSI
fflush is removed and fgets is used to remove characters in stdin. could you remove downvote ? :)
bjskishore123
+1  A: 

scanf returns the number of successful assignments. USE THAT RETURN VALUE (EDIT) ON ALL SCANF CALLS

/* ... */
printf("1) What is your Name? \n");
if (scanf("%s",&name) != 1) { /* error */ printf("error on line %d.\n", __LINE__); }
printf("2) What is your Surname? \n");
if (scanf("%s",&surname) != 1) { /* error */ printf("error on line %d.\n", __LINE__); }
/* ... */
pmg
+1  A: 

The problem is that you have an unconsumed newline in the input buffer. A space in the format string will consume zero or more whitespace characters, so you can fix the problem by changing this line to include a space before the % as shown:

scanf(" %[^\n]",dpart);
bstpierre
thanks buddy, great :D
gkaykck