I was learning about the concepts of pointers and just as I thought I got my concepts , this program caused me trouble understanding how the inputs of different datatype are being stored in a program..
And then I learnt about cin.ignore()
and tested it out..
Inputs
32 // int
64.212 // float
4.76545 // double
* // char
Hey look at me! I know pointers! // string
First Program without cin.ignore():
#include<iostream>
#include<string>
using namespace std;
int main()
{
int givenInt;
float givenFloat;
double givenDouble ;
std::string givenString;
char givenChar;
cin>>givenInt;
cin>>givenFloat;
cin>>givenDouble;
cin>>givenChar;
getline(cin,givenString);
cout<<"givenInt = "<<givenInt<<'\n';
cout<<"address of givenInt = "<<&givenInt<<'\n';
cout<<"givenFloat = "<<givenFloat<<'\n';
cout<<"address of givenFloat = "<<&givenFloat<<'\n';
cout<<"givenDouble = "<<givenDouble<<'\n';
cout<<"address of givenDouble = "<<&givenDouble<<'\n';
cout<<"givenChar = "<<givenChar<<'\n';
cout<<"address of givenChar = "<<(void*)&givenChar<<'\n';
cout<<"givenString = "<<givenString<<'\n';
cout<<"address of givenString = "<<&givenString<<'\n';
return 0;
}
Output :
givenInt = 32
address of givenInt = 0x7ffd62e17cd8
givenFloat = 64.212
address of givenFloat = 0x7ffd62e17cdc
givenChar = *
address of givenChar = 0x7ffd62e17cd7
givenString =
address of givenString = 0x7ffd62e17ce0
givenDouble = 4.76545
address of givenDouble = 0x7ffd62e17ce8
Input string does not appear on output..
First Program with cin.ignore():
#include<iostream>
#include<string>
using namespace std;
int main()
{
int givenInt;
float givenFloat;
double givenDouble ;
std::string givenString;
char givenChar;
cin>>givenInt;
cin>>givenFloat;
cin>>givenDouble;
cin.ignore();
cin>>givenChar;
cin.ignore();
getline(cin,givenString);
cout<<"givenInt = "<<givenInt<<'\n';
cout<<"address of givenInt = "<<&givenInt<<'\n';
cout<<"givenFloat = "<<givenFloat<<'\n';
cout<<"address of givenFloat = "<<&givenFloat<<'\n';
cout<<"givenDouble = "<<givenDouble<<'\n';
cout<<"address of givenDouble = "<<&givenDouble<<'\n';
cout<<"givenString = "<<givenString<<'\n';
cout<<"address of givenString = "<<&givenString<<'\n';
cout<<"givenChar = "<<givenChar<<'\n';
cout<<"address of givenChar = "<<(void*)&givenChar<<'\n';
return 0;
}
Output :
givenInt = 32
address of givenInt = 0x7ffcf69bd9a8
givenFloat = 64.212
address of givenFloat = 0x7ffcf69bd9ac
givenChar = *
address of givenChar = 0x7ffcf69bd9a7
givenString = Hey look at me! I know pointers!
address of givenString = 0x7ffcf69bd9b0
givenDouble = 4.76545
address of givenDouble = 0x7ffcf69bd9b8
Input Strings appear on the output..
Second Program with cin.ignore():
#include<iostream>
#include<string>
using namespace std;
int main()
{
int givenInt;
float givenFloat;
double givenDouble ;
std::string givenString;
char givenChar;
cin>>givenInt;
cin>>givenFloat;
cin>>givenDouble;
cin.ignore();
getline(cin,givenString);
cin.ignore();
cin>>givenChar;
cin.ignore();
cout<<"givenInt = "<<givenInt<<'\n';
cout<<"address of givenInt = "<<&givenInt<<'\n';
cout<<"givenFloat = "<<givenFloat<<'\n';
cout<<"address of givenFloat = "<<&givenFloat<<'\n';
cout<<"givenDouble = "<<givenDouble<<'\n';
cout<<"address of givenDouble = "<<&givenDouble<<'\n';
cout<<"givenString = "<<givenString<<'\n';
cout<<"address of givenString = "<<&givenString<<'\n';
cout<<"givenChar = "<<givenChar<<'\n';
cout<<"address of givenChar = "<<(void*)&givenChar<<'\n';
return 0;
}
Output
givenInt = 32
address of givenInt = 0x7ffe1ed66698
givenFloat = 64.212
address of givenFloat = 0x7ffe1ed6669c
givenChar = e
address of givenChar = 0x7ffe1ed66697
givenString = *
address of givenString = 0x7ffe1ed666a0
givenDouble = 4.76545
address of givenDouble = 0x7ffe1ed666a8
Input character goes to the string variable and string's second letter gets printed on character variable..
I thought that since double
uses buffer to store characters (If I am wrong, please correct me), let me use it at last since it does not affect the inputs which are taken after double
. But I was wrong and it gave me similar kind of result as the first program.
And after testing out both programs with and without using cin.ignore(), I got to know that we need to use cin.ignore()
whenever I take inputs of double and strings datatype.
My questions are,
1) Is my concept of "using cin.ignore() when taking inputs of double and string datatype" correct? Should I take the inputs in a particular order only?
2) Why is (void*)
used along with &givenChar
only for getting the address of a character type? &givenChar
should do the job right?