Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 14th, 2008, 09:48 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
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
Location: , , .
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
Location: , , .
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
Location: , , .
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
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:06 AM.


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