cin.getline does not return value

Aug 18, 2008 at 6:17am
#include <iostream>
#include <conio>


int main()
{
int choice;
const int SIZE = 81;
char name[SIZE];
cout <<" \n\n\n\n ---------------1. Save the information--------------"<<endl;
cout <<" ---------------2. Retrieve the information----------"<<endl;
cout <<" ---------------3. Exit------------------------------"<<endl;



cout <<"\n\nPlease enter your choice : <1>,<2>,<3> ";
cin >> choice;

if (choice == 1)
{
cout <<"Enter your friend's name :";
cin.getline(name,SIZE);
cout <<name;
}
getch();
return 0;

}


This is part of my program to save and retrieve again my friend's personal data.
The problem is the "cin.getline(name,SIZE);"cannot return the value of my friends name.The program just collapse by itself when i press any key after the "cout <<"Enter your friend's name :";" executed. I suspect the line of "cin >> choice;" might get involve with this .Because when i remove that line of code and also the if statement , the name could successfully key in and output as it suppose to be .Please, anyone got any better idea for this problem ?#i only want to use the cin.getline to occupy the space between the name#
Aug 18, 2008 at 6:46am
When you do cin >> choice; you do not read in the end of line character, which is left in the input stream. Then when you call cin.getline(name,SIZE) you try to read in a line but immediately hit that '\n' character and it returns an empty string.

To avoid this, you need to call ignore() on cin to get rid of all the characters in the stream up to and including the '\n'. Add this before your getline() call:
 
cin.ignore( 1000, '\n' );
Aug 18, 2008 at 8:58am
Thanks ,but it really did get rid of it .But here is another problem :

cout <<"Please enter your friend's IC number :";
cin >> icnum;
cout <<icnum;

this is another lines of code i added just now i declare the "icnum" as double but the result did not exactly output the ic number .For example , i key in 012345678901 the result is 1.23457e+10. how could it be!?


Aug 18, 2008 at 9:02am
If you are storing any numbers that can begin with a leading zero, you cannot use int, double, etc. You must use a string. If you try and read the number into a number type like int or double, it will drop the leading zero. The number you got was actually correct, just in scientific notation because it was a very large number.
Aug 18, 2008 at 9:19am
So, anyway to fix it to display the proper number of IC ?Actually ,the information would output to a text file .Would it still becomes the hexadecimal /octal number ? The problem still goes on even the number is not begin with leading zero
Aug 18, 2008 at 9:37am
Declare icnum as a string instead. If you never perform math on the number, then having it as a string will solve your problem.
Aug 18, 2008 at 10:06am
HUH ?i never heard about declare as a string,not even in books.Could u show me the example like adding the lines of code you mentioned about to my source code that i posted .
Aug 18, 2008 at 10:20am
[code=c++]
string icnum;
cout << "Please enter your friend's IC number :";
cin >> icnum;
cout << icnum;
[/code]

Note that you will either have to have [code=c++]using namespace std;[/code] at the top (under your #includes, before your main is a good spot) OR use std::string instead of just string.

Check out http://www.cplusplus.com/reference/string/string/ for more info on strings and the methods you can use on them.
Aug 18, 2008 at 11:13am
it still not working ,i had done according what you said .Now there is no output after enter the IC number which is total 12 numbers.Is it related to my compiler ?I'm using Borland C++ 5.5.I hate to say this ,i know this compiler is kind of old stuff and lack of compatibility with the latest source code .
Aug 18, 2008 at 2:38pm
In the above code, a std::flush (usually executed with std::endl) is missing. Try

1
2
3
4
std::string icnum;
std::cout << "Please enter your friend's IC number :";
std::cin >> icnum;
std::cout << icnum << std::endl;


Hope that helps.
Last edited on Aug 18, 2008 at 2:38pm
Aug 18, 2008 at 3:00pm
Thanks it works like a charm!But i found another alternate way ,i add the header of <iomanip> and add setprecision(x)with the cout icnum.It works too.Thanks for you guys!!
Topic archived. No new replies allowed.