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#
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:
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!?
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.
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
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 .
[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.
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 .
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!!