A question about cin in C++ - c++

When I declare int weight and then input a double value 165.1 a 2nd cin >> height; doesn't work and there is no any error message. Can you tell me why?
VS2010 Console Application used.
#include <iostream>
using namespace std;
const double lbs_to_kg = 2.2046, inches_to_meter = 39.370;
int main()
{
int weight, height;
double kilograms, meters;
cout << "\nEnter weight in pounds: ";
cin >> weight;
kilograms = weight / lbs_to_kg;
cout << "\nEnter height in inches: ";
cin >> height;
meters = height / inches_to_meter;
cout << "\nYour BMI is approximately "
<< "\nbody fat ratio is "
<< kilograms / (meters * meters)
<< ". Under 25 is good."
<< endl;
}
output:
Enter weight in pounds: 165.1
Enter height in inches:
Your BMI is approximately
body fat ratio is 1.57219e-013. Under 25 is good.

If you try to have cin extract data into a variable that can't hold it, the data is left in the input stream and cin is flagged as having failed. You need to check if it's failed with !cin, and use cin.clear() to clear the fail flag so you can read again (future extract operations will automatically fail until the flag is cleared). You can either extract the data into a different variable that's capable of holding it, or use cin.ignore() to discard it

Related

Not understanding setprecision function in c++

I'm learning C++ and am supposed to make a program which takes height in inches, weight in pounds, and age from the user, and gives them the size of their clothing. You get the size of their hat by dividing their weight by their height and multiplying that result with 2.9. I have been testing my code and the output is correct but always has an extra 1 (like 9.941 instead of 9.94) at the end. My answer should only have two digits after the decimal point, even if it's a zero. Does anyone have any tips? Thanks guys. Here is my code:
#include<iostream>
#include<iomanip>
using namespace std;
void HatSize(double userHeight, double userWeight) {
cout << setprecision(2) << fixed << ((userWeight / userHeight) * 2.9);
}
int main(){
double height;
double weight;
double age;
cout << "Give me your height in inches, weight in pounds, and age in years. I will give you your hat size, jacket size (inches at chest)\nand waist size in inches." << endl;
cin >> height;
cin >> weight;
cin >> age;
HatSize(height, weight);
cout << HatSize;
return 0;
}
You're using setprecision correctly, the issue is that you have an additional statement that is generating the 1.
Remove the cout << HatSize; line. HatSize is a function that returns void, so you're sending the actual function itself as input to cout, which is being interpreted as 1.
I would also recommend adding a << endl to the cout in your HatSize function, so that your output finishes with a newline.
void HatSize(double userHeight, double userWeight) {
cout << setprecision(2) << fixed << ((userWeight / userHeight) * 2.9)
<< endl; // Newline to make output nicer
}
int main(){
double height;
double weight;
double age;
cout << "Give me your height in inches, weight in pounds, and age in years. I will give you your hat size, jacket size (inches at chest)\nand waist size in inches." << endl;
cin >> height;
cin >> weight;
cin >> age;
HatSize(height, weight);
// cout << HatSize !!!!!!!!! Get rid of this
return 0;
}

Length conversion program cannot convert decimal number in C++

I have to write a program that takes length as input in feet and inches and the program should then convert the lengths in centimeters and display it on screen. So this is my code:
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
double totalinches;
double centimeter;
float inch_to_centimeter = 2.54f;
int feet_to_inch = 12;
int feet;
int inch;
cout << "Enter the length in feet:";
cin >> feet;
cout << "Enter the length in inches:\n ";
cin >> inch;
totalinches = (feet * feet_to_inch) + inch;
cout << "Total length in inches:\n ";
cout << totalinches;
centimeter = totalinches * (inch_to_centimeter);
cout << "The number of centimeter= ";
cout << centimeter;
When I run the code, the code can only calculate natural number (non-decimal number) but whenever I type decimal number, the code is error. This is the error that I got when I type decimal number. The code itself skip the "Enter the length in inches" and the result is always 60 and 152.4. Sorry for my bad English and i am new to code.
Enter the length in feet:5.74
Enter the length in inches:
Total length in inches:
60The number of centimeter= 152.4
C:\Dev\C++\HomeWork\Debug\HomeWork.exe (process 22468) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

How to debug my C++ BMI calculator program?

I've only recently learnt C++. I had a school project for making a BMI calculator. Unfortunately it is showing up errors beyond my scope of understanding. I'm not sure if I should use a different data type for my height and weight variables - should these by double?
#include <iostream>
using namespace std;
float bmi_calc(int height, int weight){
float bmi_user = weight / height * height;
return bmi_user;
}
int main()
{
int weight_user;
int height_user();
cout << "Enter your weight in kilograms";
cin >> weight_user;
cout << "Enter your height in meters";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
}
fixed code:
#include <iostream>
using namespace std;
float bmi_calc(float height, float weight) {
return weight / (height * height);
}
int main()
{
float weight_user;
float height_user;
cout << "Enter your weight in kilograms ";
cin >> weight_user;
cout << "Enter your height in meters ";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
return 0;
}
since your height is given in meters you need a float, since a int can only be used for whole numbers. also use proper parenthesis to get the formula right :)

Stumped with a transient population program in c++

this is my first time posting on here. Whenever I've gotten stuck on a programming problem, I've typically been able to find enough information to get me unstuck. I'm afraid that the issue I'm having though, I can't quite find an answer to. It's something I'd need someone to look at to tell me what I may be doing wrong in my code.
I have the program running successfully, and it DOES work. The issue however, is that my produced output is off by a few numbers when compared to the expected output on My Programming Lab. I'm really not sure of what to do to produce the correct output. Allow me to post both my source code, and a screenshot MPL's results screen.
SOURCE CODE:
#include <iostream>
using namespace std;
int populationCalculator(int, double, double, int, int);
int main()
{
int startingPopulation, newArrivals, peopleWhoLeft, years,
newPopulation, finalPopulation;
double deathRate, birthRate;
cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startingPopulation;
while (startingPopulation < 2)
{
cout << "\nThe starting population may not be less than two. Please
re - enter: ";
cin >> startingPopulation;
}
cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "\nBirth rate percent cannot be negative. Please re -
enter:";
cin >> birthRate;
}
birthRate = birthRate / 100;
cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "\nDeath rate percent cannot be negative. Please re -
enter: ";
cin >> deathRate;
}
deathRate = deathRate / 100;
cout << "How many individuals move into the area each year? ";
cin >> newArrivals;
while (newArrivals < 0)
{
cout << "\nArrivals cannot be negative. Please re - enter: ";
cin >> newArrivals;
}
cout << "How many individuals leave the area each year? ";
cin >> peopleWhoLeft;
while (peopleWhoLeft < 0)
{
cout << "\nDepartures cannot be negative. Please re - enter: ";
cin >> peopleWhoLeft;
}
cout << "For how many years do you wish to view population changes? ";
cin >> years;
while (years < 1)
{
cout << "\nYears must be one or more. Please re - enter: ";
cin >> years;
}
newPopulation = populationCalculator(startingPopulation, deathRate,
birthRate, newArrivals, peopleWhoLeft);
cout << "\nStarting population: " << startingPopulation << endl;
for (int loopCount = 1; loopCount <= years; loopCount++)
{
newPopulation = populationCalculator(newPopulation, deathRate,
birthRate, newArrivals, peopleWhoLeft);
cout << "Population at the end of year " << loopCount << " is: " <<
newPopulation << endl;
}
system("pause");
return 0;
}
int populationCalculator(int Population, double deathRate, double birthRate,
int newArrivals, int peopleWhoLeft)
{
int newPopulationCount;
newPopulationCount = Population + (Population * birthRate) - (Population
* deathRate) + newArrivals - peopleWhoLeft;
return newPopulationCount;
}
MPL RESULTS:
http://imgur.com/a/mRmpc
I really will appreciate if anyone can help me figure out why my produced output is off by a few numbers.
Step through your code. You're returning an int where you have double and int multiplication. Make sure that you aren't truncating values that might need to be rounded up or down.
Does birth happen before or after death? Should it occur in steps, or all at once like you have shown?
try casting to a double in your operations that involve doubles and integers. For example
birthRate = (double)(birthRate / 100);
deathRate = (double)(deathRate / 100);
Sometimes programming languages will cast a double to an integer, which causes your numbers to be off.
Perhaps the number were rounded off?
I assume this is the case because I noticed you declared some variables in double but the end result is integer. Try to change the data type of the method
populationCalculator(), newPopulation and of course the returning values of the method populationCalculator() from int to double.

How to calculate BMI in C++ with ft and inches in calculation

How do you calculate BMI = mass (lb) x 703/ (height(in))squared in C++.
BMI answer was 33.4695
I entered:
cout << "weight(lbs)";
cin >> lbs;
cout << "height";
cin >> height >> in;
BMI = ((lbs) * 703)/pow(height(in)), 2);
I'm having and error: no instance of overloaded function "pow" matched the argument list argue types are: (<error-types>).
I have included the <cmath> file and height is defined as double.
Let's start saying you don't need to call pow() at all, but in your case you misplaced the braces. It should be
pow(height, 2)
Assuming that height is a numeric variable.
It's not clear if you want to include somehow the units of measures of phisical quantities (inches or pound) into your variables. That's a more challenging task, but meanwhile you can use this:
float weight = 0, height = 0;
cout << "Enter weight(lbs): ";
cin >> weight;
cout << "Enter height(inches):";
cin >> height;
float BMI = weight * 703.0 / (height * height);
cout << "BMI = " << BMI << '\n';