Scanf and gets buffer?

Your while loop and use of gets() is generally not good practice .

Up vote 1 down vote favorite share g+ share fb share tw.

Im having a problem with scanf and gets. And I kno that its bound to errors but I couldn't find any other way. This way, the name is printing out but It doesn't print out the first letter of it.

Here's my code: #include float calculations(int age, float highBP, float lowBP); char option; int counter, age; char temp_name50; float highBP, lowBP, riskF, optimalH = 120.0, optimalL = 80.0; typedef struct { char name50; /*which represents the patient’s name*/ int age; /*which represents the patient’s age*/ float highBP; /*highBP, which represents the patient’s high (systolic) blood pressure*/ float lowBP; /*lowBP, which represents the patient’s low (diastolic) blood pressure*/ float riskF; /*riskFactor, which represents the patient’s risk factor for stroke due to hypertension. */ }patient;/*end structure patient*/ patient *pRecords30; void printMenu() { printf("\n---------------------------------------------------------\n"); printf("|\t(N)ew record\t(D)isplay db\t(U)pdate record\t|\n"); printf("|\t(L)oad disk\t(W)rite disk\t(E)mpty disk\t|\n"); printf("|\t(S)ort db\t(C)lear db\t(Q)uit \t\t|\n"); printf("---------------------------------------------------------\n"); printf("choose one:"); }/*end print menu*/ void enter() { if(counter == 30) printf("database full. "); else{ printf("name: "); while(getchar()=='\n'); gets(temp_name); strcpy(pRecordscounter->name , temp_name); printf("age: "); scanf("%d", &age); pRecordscounter->age = age; printf("highBP: "); scanf("%f", &highBP); pRecordscounter->highBP = highBP; printf("lowBP: "); scanf("%f", &lowBP); pRecordscounter->lowBP = lowBP; float temp = calculations(age, highBP,lowBP); pRecordscounter->riskF = temp; /*printf("name: %s, age: %d, highbp:%.1f, lowBP:%.1f\n", pRecordscounter->name,pRecordscounter->age,pRecordscounter->highBP,pRecordscounter->lowBP); printf("risk factor: %.1f\n", pRecordscounter->riskF);*/ counter ++; } }/*end of void enter function*/ memallocate(int counter){ pRecordscounter = (patient *)malloc (sizeof(patient)); }/*end memallocate function*/ void display() { printf("===============================\n"); int i; for(i=0; iname); printf("age: %d\n", pRecordsi->age); printf("bp: %.2f %.2f\n", pRecordsi->highBP, pRecordsi->lowBP); printf("risk: %.2f\n\n", pRecordsi->riskF); }/*end of for loop*/ printf("========== %d records ==========", counter); }/*end of display method*/ float calculations(int age, float highBP, float lowBP) { float risk; if((highBP =50) risk = 0.5; } else if(highBP optimalL && lowBP =50) risk = 1.5; } else if ((highBP >optimalH && highBP =50) risk= 1.5; } else if((highBP > optimalH && highBP optimalL && lowBP =50) risk = 2.5; } else if(highBP (optimalL+11) && lowBP=50) risk = 3.5; } else if((lowBP (optimalH+11) && highBP=50) risk = 3.5; } else if((highBP>=(optimalH+11) && highBP =(optimalL+11) && lowBP=50) risk = 4.5; } else { risk = 5.0; if(age >=50) risk = 5.5; } return risk; }/*end of calculation function*/ main() { printMenu(); char option=getchar(); while(option!

= 'q' || option! = 'Q'){ if(option == 'N' || option == 'n') { memallocate(counter); enter(); printMenu(); } if (option == 'L' || option == 'l') { printMenu(); } if(option == 'S' || option == 's') { printMenu(); } if(option == 'D' || option == 'd') { display(); printMenu(); } if(option == 'W' || option == 'w') { printMenu(); } if(option == 'C' || option == 'c') { printMenu(); } if(option == 'U' || option == 'u') { printMenu(); } if(option == 'E' || option == 'e') { printMenu(); } if(option == 'Q' || option == 'q') { exit(0); } option = getchar(); }/*end while*/ system("pause"); }/*end main*/ sample output: --------------------------------------------------------- | (N)ew record (D)isplay db (U)pdate record | | (L)oad disk (W)rite disk (E)mpty disk | | (S)ort db (C)lear db (Q)uit | --------------------------------------------------------- choose one: n name: judy age: 30 high bp: 110 low bp: 88 3 --------------------------------------------------------- | (N)ew record (D)isplay db (U)pdate record | | (L)oad disk (W)rite disk (E)mpty disk | | (S)ort db (C)lear db (Q)uit | --------------------------------------------------------- choose one: n name: cindy white age: 52 high bp: 100.7 low bp: 89.4 --------------------------------------------------------- | (N)ew record (D)isplay db (U)pdate record | | (L)oad disk (W)rite disk (E)mpty disk | | (S)ort db (C)lear db (Q)uit | --------------------------------------------------------- choose one: d =============================== name: udy age: 30 bp: 110.00 88.00 risk: 1.0 name: indy white age: 52 bp: 100.70 89.40 risk: 1.5 ========== 2 records ========== buffer scanf gets link|improve this question asked Jul 15 '11 at 19:41Nidale 103 0% accept rate.

1 Please boil your sample code down to a test case that can illustrate your question. – jbruni Jul 15 '11 at 19:51.

Your while loop and use of gets() is generally not good practice. Try something like: fflush(stdin); fgets(pRecordscounter->name, sizeof(pRecordscounter->name), stdin); Try if (strlen(pRecordscounter->name) > 0) { pRecordscounter->namestrlen(pRecordscounter->name) - 1 = '\0'; }.

Thank you sir! It works perfectly! – Nidale Jul 15 '11 at 20:22 You're welcome!

– Peter K. Jul 15 '11 at 20:27 kk I know I said it works perfectly.. but now, I'm having problems with it.. its storing the new line character at the end of the string so everytime I print it, it prints a new line – Nidale Jul 16 '11 at 14:19 any other suggestions? This user input thing is causing so many problems... – Nidale Jul 16 '11 at 14:19.

You lose the first character to while(getchar()=='\n');. I don't know why that statement is necessary, but it loops until it gets a character that is not '\n' (which is 'j' and 'c' in your case).

Because if I take it out, it doesn't accept user input for the name. It skips right over to age. – Nidale Jul 15 '11 at 19:54 @Nidale Hmm...interesting.

I suggest you try fflush(stdin); to flush remaining characters in the input stream before you accept input the name. – Ram Jul 15 '11 at 20:00 yeah, I just did.. it still doesn't work for some reason. – Nidale Jul 15 '11 at 20:04 @Nidale That is bizarre.

What if you remove the while loop and just say getchar();? – Ram Jul 15 '11 at 20:08 the thing is I dt want to just get a character, I want to get the whole line.. it's a char name20 – Nidale Jul 15 '11 at 20:16.

While (getchar() == '\n'); This line eats the newlines plus one character. When getchar() does not return a newline, it has already consumed the first character. Look at ungetc() to write that character back onto the stream.

This: while(getchar()=='\n'); loops until it gets a non-newline, which will be the first character of the name. Try this instead: do c = getchar(); while(c == '\n'); ungetc(c, stdin).

Now, it's just not letting me take user input. – Nidale Jul 15 '11 at 19:59 Can you copy-and-paste the lines from printf("name: "); to printf("age: ");. – MRAB Jul 15 '11 at 20:15.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions