C/Problem of skiping a address in structures.
Expert: Joseph Moore - 9/24/2009
QuestionDear Sir,
I made the following programe:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct record
{
char name[20],college[20],city[20];
int roll; /* initialising structure */
float marks;
};
struct record r[4];
int i; /* initialising pointer */
struct record *ptr;
clrscr();
for(i=0;i<=3;i++)
{
ptr=&r[i];
printf("Enter Student's Name, College, City, Roll No.,Marks: ");
gets(ptr->name);
gets(ptr->college);
/* printing and scanning of data of the student */
gets(ptr->city);
/* i.e. Name, College Name, City Name, Roll No., Marks */
scanf("%d%f",/*ptr->name[i],ptr->college[i],ptr->city[i],*/ptr->roll,ptr->marks);
}
printf("Students Name\tCollege\tCity\tRoll No.\tMarks\n");
for(i=0;i<=3;i++)
{
ptr=&r[i];
puts(ptr->name);
puts(ptr->college); /* printing the data entered */
puts(ptr->city);
printf("%d\t\t%f\n",/*ptr->name,ptr->college,ptr->city,*/ptr->roll,ptr->marks);
}
getch();
}
linkfloat()
{
float a=0,*r;
/* function, without which compiler terminates the programe itself, as it comes to entry of float */
r=&a;
a=*r;
}
And when i run this programe, i am facing a problem i.e. when i am entering the data, 1st student's data is entered properly. But when my compiler asks to enter 2nd student's data, it skips the name and comes directly to the college name, where it ask the ser to enter the name of the college of the 2nd student. And the problem persists in the other students' data entry. And i came to know about the problem when i printed the address's while scanning the data entry.
Sir i will be highly obliged if you help me in overcoming this problem.
regards
Kshitij Bansal
AnswerHi, Kshitij.
The problem you're seeing is a very common problem experience by beginners. The problem is in the way that input works. When you call your scanf function, it takes exactly what you tell it to and nothing more, leaving the rest of the buffer for future input. In this case, if you enter, for example, "100 98.2", then scanf will take the integer and float value, and leave everything that comes after it. What comes after that, you might ask? Well, when you press the return key, it adds a newline character: '\n'. This means that the next time you try to read input, there is a single newline character remaining on the buffer, which is why it appears to skip the reading of the name. To avoid this problem, a very common little line of code is used:
while (getchar() != '\n');
This line of code will eat one character at a time until it's eaten the newline character. That means that in a line of input such as:
100 98.2abcdefghijklmnopqrstuvwxyzblahblahblah
The code will simply skip over the entirety of the "abcdefghijklmnopqrstuvwxyzblahblahblah" including the newline character at the end of it, allowing you to properly input the name on the next loop iteration.
Ok, hopefully that clears things up. If you have any further questions, please do not hesitate to ask. I'm here to help. :)