?
Solved

getline function

Posted on 2004-11-22
6
Medium Priority
?
298 Views
Last Modified: 2010-04-01
hi, i am facing a strange problem in use of getline. i have used    ---   cin.getline(p.name,'\n' ) --- but it only picks first 10 characters and as a result corrupts next inputs. if i use - cin.getline(p.name,80 ) then it works well. please help me that why the first statement is not working correctly. for the reference i m pasting the code below  

***********************************
# include <iostream.h>
# include <conio.h>
     struct Person{
     char name[50];
     long phoneNumber;
     char gender;
     int age;
     };
void main()
{
     Person p;
     cout << "Enter name of person: " ;
     cin.getline(p.name,'\n' );                            // problem line
     cout << "Enter phone number: ";
     cin >> p.phoneNumber;
     cout << "Enter gender: ";
     cin >> p.gender;
     cout << "Enter age: ";
     cin >> p.age;
     cout<<"Name: "<<p.name<<"\n";
     cout<<"Phone Number: "<<p.phoneNumber<<"\n";
     cout<<"Gender: "<<p.gender<<"\n";
     cout<<"Age: "<<p.age<<"\n";
     getch ();
}
*****************************************

regards
fa.
0
Comment
Question by:fatimao
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
avizit earned 100 total points
ID: 12651944
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#problems


std::cin.getline() can run into problems when used with std::cin >> var.

    * getline can be provided a third argument--a "stop" character. This character ends getline's input. The character is eaten and the string is terminated. Example:
      std::cin.getline(str, 100, '|')
    * If std::cin.getline() is not provided a "stop" character as a third argument, it will stop when it reaches a newline.



i.,e the stop charcater has to be third argument , if not it uses the newline
0
 
LVL 11

Expert Comment

by:avizit
ID: 12651948
The second argument to "getline" is the number of characters to read. Hence when you put '\n' its read 10 as 10 is the ascii value of the newline
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
ID: 12652011
I recommend you use the global getline function instead, and use std::string instead of a c-style char array.

Also <iostream.h> is not part of the C++ standard.
To make your code portable, use <iostream> extensionless version instead, which is part of the C++ standard.

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

     struct Person{
       string name;
     long phoneNumber;
     char gender;
     int age;
     };
void main()
{
     Person p;
     cout << "Enter name of person: " ;
       getline(cin, p.name, '\n');
     cout << "Enter phone number: ";
     cin >> p.phoneNumber;
     cout << "Enter gender: ";
     cin >> p.gender;
     cout << "Enter age: ";
     cin >> p.age;
     cout<<"Name: "<<p.name<<"\n";
     cout<<"Phone Number: "<<p.phoneNumber<<"\n";
     cout<<"Gender: "<<p.gender<<"\n";
     cout<<"Age: "<<p.age<<"\n";
     getch ();
}


0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question