473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I fix my add method?

9
My add method gets stuck after the user inputs a name. Can anyone help me fix it please.
Expand|Select|Wrap|Line Numbers
  1. void add(void)                        //FUNCTION TO ADD A RECORD IN LIST 
  2.  int i,j,k; 
  3.  char cho,ch; 
  4.  k=1; 
  5.  do 
  6.      { 
  7.          temp=(struct Record *)(malloc(sizeof(struct Record)));        //DYNAMIC MEMORY ALLOCATION 
  8.          printf("Enter Telephone Number : "); 
  9.          scanf("%ld",&temp->telNo); 
  10.          ch=getchar(); 
  11.          printf("Enter Name : ");  
  12.          j=0;  
  13.          ch=getchar();  
  14.          while(ch!='n')  
  15.              {  
  16.                  temp->name[j++]=ch;  
  17.                  ch=getchar();  
  18.                  }  
  19.          temp->name[j]=' ';  
  20.          printf(" Enter Record's Address:: ");  
  21.          scanf("%s",temp->address);  
  22.          printf("\n Enter Record's Year of Birth. :: ");  
  23.          scanf("%d",&temp->yearOfBirth);  
  24.          ch=getchar();  
  25.          printf("\n Do You Want to Save This Record (y/n)??");  
  26.          cho=getchar();  
  27.          if((cho=='n')||(cho=='N'))  
  28.              {  
  29.                  free(temp);                //IF NOT TO SAVE THEN FREE THE ALLOCATED MEMORY  
  30.                  goto label;  
  31.                  }  
  32.          temp->next=top;  
  33.          top=temp;  
  34.          printf(" One  Record Added");  
  35.          label:  
  36.          ch=getchar();  
  37.          printf(" Enter Another Record (y/n) ::");  
  38.          cho=getchar();  
  39.          }while((cho=='y')||(cho=='Y'));  
  40.  }  
  41.  
Aug 13 '10 #1
7 1201
newb16
687 512MB
How is Record::name (or the whole Record) declared?
Aug 13 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. while(ch!='n')   
is used to enter a name. Do you mean that names can't contain an n or do you mean:

Expand|Select|Wrap|Line Numbers
  1.  while(ch!='\n')   
to receive characters until you reach the enter key pressed by the user.
Aug 13 '10 #3
tam13
9
Here is how record is declared:

Expand|Select|Wrap|Line Numbers
  1. struct Record                           
  2. {  
  3.  long int telNo;  
  4.  int yearOfBirth;  
  5.  char name[40],address[40];  
  6.  struct Record *next;                  
  7.  }rec1,rec2;                        
  8. struct Record *temp,*previous,*current;         
  9. struct Record *top=NULL; 
Aug 13 '10 #4
tam13
9
Expand|Select|Wrap|Line Numbers
  1. while(ch!='\n'); 
That is what I meant to put. However when I make the change it suddenly skips over name to address. Then when I enter address it goes into an infinite loop.
Aug 13 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Probably your are fetching the \n from the telephone number. Most likely you parsed out the telephone number and left the \n in the input stream.

You might do a getchar() before you start parsing the name to eat that leftover \n.
Aug 13 '10 #6
tam13
9
After which line in the code should I do the getchar()?
Aug 14 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
Here is a piece of your code:

Expand|Select|Wrap|Line Numbers
  1. ch=getchar();  
  2.          printf("Enter Name : ");   
  3.          j=0; 
  4.  
  5.          ch=getchar();   
  6.          while(ch!='\n')   
  7.              {   
  8.                  temp->name[j++]=ch;   
  9.                  ch=getchar();   
  10.                  }   
  11.          temp->name[j]='\0';   
  12.          printf(" Enter Record's Address:: ")
The first getchar() fetches the enter key from the telephone number.
The second getchr() fetches the first character of the name.
The third getchar() inside the loop fetches the rest of the characters in the name. This will include the \n that breaks thge loop.
Then I changed you code to temp->name[j]='\0'; to put a null terminator on the name to make it a string. Your original code appended a space and I didn't thisnk that was correct.

Based on cursory debugging using Visual Studio.NET 2008, you code appears to work as far as obtaining data is concerned.

You might consider using fgets() to fetch you name since a lot of code in your program is the guts of fgets().
Aug 15 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
7
by: greenflame | last post by:
I am trying to make a matrix object. I have given it some properites. I am trying to add a method. When I call the method by Test.showDims(...) I want to only enter one input, that is the method by...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
3
by: allendowney | last post by:
Hi All. In a complex inheritance hierarchy, it is sometimes difficult to find where a method is defined. I thought it might be possible to get this info from the method object itself, but it...
9
by: VK | last post by:
<OT>I am finishing TransModal 0.1 so planning to move it from alpha to beta stage.<OT> Besides that I am planning to write an introductory to inheritance schema currently used in Javascript...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.