Hey i am new to use C++...
This is one part of my 200 line calculator and the problem is the Name...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
elseif (x==4)
{float bmi,h,w,age;
char name[20], wat;
cout<< " Type your name \n\n";
cin>>gets(name);
cout<< " Type your age \n\n";
cin>>age;
cout<< " Enter your height.....\n\n ";
cin>>h;
cout<< " Enter you weight in kg.....\n\n";
cin>>w;
cout<<endl<<endl;
bmi=(w/(h*h)*10000);
cout<< " Name : ";puts(name);cout<<endl<<" Age : "<<age<<endl<<"\n BMI INDEX : "<<bmi;
}
I am getting this output
Type your Name
Raheel Ahmed
Type your age
Enter your Height
Enter your Weight
Name: Raheel
Age:
BMI Index: 2e+12.322432 (bla bla doesnt matter)
Why isn't my full name displayed and i cant neither type the other datas?? Note: Use Same Funtions
Don't mix gets with std::cin. Instead use a simple std::string.
The default behaviour for std::cin is to truncate the read operation on a whitespace. Hence only the first part of your name is read in the variable name.
Thereafter the program tries to read your age. It expects a float but it gets a string "Ahmed". Hence std::cin goes into error state and further read operations fail.
Instead try:
1 2 3 4 5
std::string name;
char wat;
cout<< " Type your name \n\n";
// read the entire line upto '\n' character
getline( std::cin, name );