Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 14th, 2008, 09:48 AM
Registered User
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default cin.getLine() function skips. Please help!

When I run the programme of which this function is a part, the programme changes the land line phone number and cellular phone number but not the name, address and email. It just jumps to the next line. the cin.getLine() has problem.

void directory::modification()
{

      clrscr();

      cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ ";
      cout<< "\n\t=====@ @ @ @ @ @ @@ @ @ =====";
      cout<< "\n\t=====@@@@@@ @ @ @ @ @ @ @ @ @ @@@ =====";
      cout<< "\n\t=====@ @ @ @ @ @ @ @@ @ @ =====";
      cout<< "\n\t @@@@@@ @@@@@ @@@@@ @ @@@@@ @ @ @@@@@@ \n\n\n\n";
      long pn;
     int n,i;
     ifstream inFile;
     ofstream outFile;

     inFile.open("addressesFile");
     if(!inFile)
     {
         cout<<"\n File not found!";
         outFile.close();
         exit(-1);
      }
    outFile.open("new");
    n=test();
    if(n==0)
    {
        cout<<"\n The file is empty. ! ";
        getch();
        return;
    }


   for(i=0;i<n;i++)
      {
          inFile.read((char*)&obj,sizeof(obj));
          outFile.write((char*)&obj,sizeof(obj));
      }
      inFile.close();
      outFile.close();
      outFile.open("addressesFile",ios::trunc);
      inFile.open("new");
      if(inFile.fail())
      {
          cout<<"\n The file was not found ! ";
          getch();
          return;
      }
   char ch;
   cout<<"\n Enter the Land Line Phone Number of the Contact :";
   cin>>pn;
   ch=cin.get();
   //cin.get(ch);
   for(i=0;i<n;i++)
    {
       inFile.read((char*)&obj,sizeof(obj));
       char d;
       if(pn==landLinePhone)
          {
            cout<<" Name : "<<obj.contactName<<"\n";
            cout<<" Home Address : "<<obj.homeAddress<<"\n";
            cout<<" EMAIL ADDRESS : "<<obj.email<<"\n";
            cout<<" Cellular Phone Nmber : "<<obj.cellularPhone<<"\n";
            cout<<" Land Line Phone Number : "<<obj.landLinePhone<<"\n\n";

            d=check("HOUSE PHONE NUMBER ");
            if((d=='y') || (d=='Y'))
            {
                cout<<"\n Enter the new land line phone number of the contact, please. :";
                cin>>landLinePhone;
                ch=cin.get();
                //cin.get(ch);
            }
            d=check("OFFICE PHONE NUMBER ");
            if((d=='y') || (d=='Y'))
              {
                cout<<"\n Enter the new cellular phone number of the contact, please. :";
                cin>>cellularPhone;
                ch=cin.get();
                //cin.get(ch);
              }
              d=check("Name of the Contact");
            if((d=='y') || (d=='Y'))
            {
                cout<<"\n Enter the name of the contact, please. : ";
                cin.getline(contactName,20,'\n');
                //cin.get();
              }
            if(check("HOME ADDRESS")=='y')
            {
                cout<<"\n Enter the new Home Address of the contact, please. :";
                cin.getline(homeAddress,50,'\n');
                //cin.get();
            }
            if(check("EMAIL ADDRESS:")=='y')
            {
                cout<<"\n Enter the new E-mail Address of the contact, please. :";
                cin.getline(email,25,'\n');
                //cin.get();
            }
       }//the outer if ends here.
       outFile.write((char*)&obj,sizeof(obj));
    }
    outFile.close();
    inFile.close();
}

Reply With Quote
  #2 (permalink)  
Old February 19th, 2008, 12:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Its hard to assess this code for any problem as what goes in 'check' is not given here. Also to be able to better scrutinize your code for any problem its always advisable to mark smaller areas that you think are most susceptible of a lapse, (in this case its the area where you are using getline), and running or dry running them separately. Stuck still? You can always right back but with cleaner, succinct code snippet this time.

Regards
Ankur
Reply With Quote
  #3 (permalink)  
Old February 23rd, 2008, 04:57 PM
Registered User
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

//Sometimes the getLine works and at other times it does not work.
void directory::enterTheDetails()
{
  clrscr();
  newDataLogo();
  gotoxy(30,10);
  char ch;
  cout<<" \n Please enter the first name of the new contact : ";
  cin>>contactFirstName;
  ch = cin.get();
  cout<<" \n Please enter the last name of the new contact : ";
  cin>>contactLastName;
  ch = cin.get();
  cout<<" \n Please enter the Home Address of the new contact : ";
  cin.getline(homeAddress,50,'\n');
  cout<<" \n Please enter the email address of the new contact. : ";
  cin.getline(email,25,'\n');
  cout<<" \n Please enter Land line Phone Number of the new contact: ";
  cin>>landLinePhone;
  ch = cin.get();
  cout<<" \n Please enter Cellular Phone Number of the new contact: ";
  cin>>cellularPhone;
  ch=cin.get();
}

Reply With Quote
  #4 (permalink)  
Old February 29th, 2008, 11:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

On the face of it, it doesn't look like having a problem other than that the code could do without a few statements. When you are getting inputs using cin, getline etc, you have to always be aware of the carriage return that usually causes problem when you are switching between numerical data inputs ans string type inputs a lot. Here that doesn't seem to be problem but try calling cin.ignore(); before every getline statement, just in case. I dont know what reoccurring cin.get() is acheiving here.

Regards
Ankur
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dates Skips Over Month harpua Pro PHP 0 January 31st, 2006 02:10 PM
Problems with cin eye_of_telescope C++ Programming 1 March 17th, 2005 10:41 AM
C++ Beginner having (the usual?) getline problems Slapworth C++ Programming 2 October 10th, 2004 01:45 PM
more getline problmes Gubber C++ Programming 1 July 17th, 2004 06:16 PM
cin.getline alfrepa Visual C++ 0 April 14th, 2004 08:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.