You are here:

C/Problem of skiping a address in structures.

Advertisement


Question
Dear 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

Answer
Hi, 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. :)

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.