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 . . .
Related
This question already has answers here:
Why not use Double or Float to represent currency?
(16 answers)
Closed 3 years ago.
The code seems to work fine when it performs the first step of multiplying the number of quarters entered by 0.25, but then it just doesn't work with the next two steps.
#include <iostream>
using namespace std;
int main()
{
int quarter, dime, nickle;
int result;
quarter = 25;
dime = 10;
nickle = 5;
int numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
result = (numQuarters * quarter) + (numNickles * nickle) + (numDimes * dime);
cout << "The total amount of pennies is: " << result;
return 0;
}
I expect the output of 4 quarters, 10 dimes & 20 nickels to be 300 pennies
The output is 102
Edit: Code Fixed and working now!
Hmm... your code seems fine, but I think something was wrong with the Order of Operations in your Math. I just changed the value of nickel to 0.05 and the value of dime to 0.10 (I think that was a mistake in your code). I aslo moved the *100 down to the cout statement, and that helped clear things up...
#include <iostream>
using namespace std;
int main()
{
float quarter, dime, nickle, penny;
float result;
quarter = 0.25;
dime = 0.10;
nickle = 0.05;
float numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
result = (numQuarters * quarter) + (numNickles * nickle)+ (numDimes * dime);
cout << "The total amount of pennies is: " << result * 100;
return 0;
}
Oh, and just like what #Pete Becker said, do your calculations in pennies (whole numbers). Not only does it fix some minor errors with floats as money, it also makes your code easier to read and manipulate.
I am trying to write a program in which a user will be prompted to enter an integer 3 times. After each integer, a sum will be displayed after the input. Then, with the second and third integers, the numbers should be added onto the initial sum within a loop. Here is what I have done:
#include <iostream>
using namespace std;
int main () {
double number=0, total=0;
for (double n=0; n<3; n++){
cout << "Enter an integer: ";
cin >> number;
cout << "Sum is: " << number <<endl;
total+=number; }
}
This is the output so far:
Enter an integer: 2
Sum is: 2
Enter an integer: 3
Sum is: 3
Enter an integer: 4
Sum is: 4
The goal is for the integers to continue to add to the sum until the loop is done. This is the output that I am trying to achieve:
Enter an integer: 2
Sum is: 2
Enter an integer: 3
Sum is: 5
Enter an integer: 4
Sum is: 9
Any help would be appreciated, as I am confused on how to solve this part, and it is the only part I need to figure out in order to complete it. Thanks for taking the time to read this!
cout << "Sum is: " << number << endl;
In this line you are printing the current number, not the total. You need to use total instead.
Also move total += number; before the previous line. Else you will be one step behind when displaying.
Thus your code should look like this:
#include <iostream>
using namespace std;
int main () {
double number=0, total=0;
for (double n=0; n<3; n++){
cout << "Enter an integer: ";
cin >> number;
total+=number;
cout << "Sum is: " << total << endl;
}
}
So i am writing a C++ program to convert meters to feet. The program does compline. However, when i set the value of feet = 3.279 and entered 3.25 for meters. The answer comes back as 9.837. Can someone tell me why this may be happening?
(code below)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int meters;
int feet;
feet = 3.279 * meters;
cout << "Enter Meters to be converted to feet: ";
cin >> meters;
cout << meters * 3.279 << " feet " << endl;
}
Integers are whole numbers. When a non-whole number is assigned to an integer in C++ everything past the decimal is ignored.
int x = 1.123; // x = 1
int y = 1.999; // y = 1
Furthermore, this program uses meters before it has been assigned a value on the following line
feet = 3.279 * meters;
But feet is also never used after this assignment.
The only portion of code that is really being used is
cin >> meters;
cout << meters * 3.279 << " feet " << endl;
When you input 3.25 for meters the value as an integer becomes 3. The calculation on the following line therefore becomes 3 * 3.279 which does in fact equal 9.837.
SOk I edited out all the irrelevant information. Also I did some testing on a new file and it seems when you multiply two numbers that are being put in variables against each other it produces the wrong result. Like in my code the user is entering the length in feet first then in inches. The inches is then divided by 12 and added with feet. Also there getting rounded. Same goes for the width. Then when you multiply the length and width together it produces the wrong result.
Why is that? How do I fix it?
using namespace std;
void setdata();
int main(){
setdata();
return 0;
}
void setdata(){
int idnumber, lengthfeet, lengthinches, widthfeet, widthinches;
float costsqfoot, discount, lengthinchdec, widthinchdec, foot1, reallength, realwidth, arearoom;
foot1= 12;
cout << "What is length of room \t Feet: "; cin >> lengthfeet; cout << "\t \t \t Inches: "; cin >> lengthinches;
cout << "What is width of room \t Feet: "; cin >> widthfeet; cout << "\t \t \t Inches: "; cin >> widthinches;
cout << fixed << setprecision(2);
lengthinchdec = lengthinches / foot1; cout << lengthinchdec << endl; widthinchdec = widthinches / foot1; cout << widthinchdec;
reallength = lengthfeet + lengthinchdec; realwidth = widthfeet + widthinchdec; arearoom = (reallength * realwidth);
cout << endl;
cout << reallength << endl; cout << realwidth << endl;
cout << arearoom;
}
example
input for length feet:30
input for length inch: 5
input for width feet: 18
input for width inch: 11
Output for reallength is 30.42. The lengthinches is being divided by 12 so 5/12 is .42 when rounded up.
Output for realwidth is 18.92. The widthinches is being divided by 12 so 11/12 is .92 when rounded up.
The answer comes out 575.38.
It's supposed to come out 575.54
For your checking on the calculator, you're rounding up the intermediate results.
Your program isn't rounding the intermediate results, only the result of the multiplication.
575.38 is the correct answer!
Imperial units hell :)
multiplied it by hand and got
30 feet 5 inches * 18 feet 11 inches = 82 855 inches2
which is 575.38194444444 sqf
so... what is the problem?
and just for fun formatted the code and pushed it into CoLiRu with all variables set to double instead of float, just in case... http://coliru.stacked-crooked.com/a/2fcef984c5561159 and got the same result
Floating point calculations may be tricky. Values like the result of 5/12 aren't representable by a binary floating point number with a finite number of digits, so they are calculated and stored in float or a double types with a certain amount of rounding errors.
In some cases (like your particular example), one can avoid those errors (even if negligible in practice) using integer arithmetic instead.
Consider this snippet of code and how it deals with your problem:
#include <iostream>
#include <iomanip>
int main() {
using std::cout;
using std::cin;
constexpr int inches_in_one_foot = 12;
constexpr int square_inches_in_one_square_foot =
inches_in_one_foot * inches_in_one_foot;
// Please, note that in real code user input should be checked
int length_feet, length_inches, width_feet, width_inches;
cout << "What is the length of the room?\nFeet: ";
cin >> length_feet;
cout << "Inches: ";
cin >> length_inches;
cout << "What is the width of the room?\nFeet: ";
cin >> width_feet;
cout << "Inches: ";
cin >> width_inches;
// calculate the dimensions in inches
int length = length_inches + inches_in_one_foot * length_feet;
int width = width_inches + inches_in_one_foot * width_feet;
// the area is precisely calculated in square inches
int area = length * width;
int area_feet = area / square_inches_in_one_square_foot;
// note that this ^^^ is an integer division, or: 82855 / 144 = 575
int area_inches = area - area_feet * square_inches_in_one_square_foot;
cout << "\nArea: " << area_feet << " square feet and "
<< area_inches << " square inches.\n";
cout << "or : " << std::fixed << std::setprecision(2)
<< static_cast<float>(area) / square_inches_in_one_square_foot
<< " square feet.\n";
return 0;
}
Entering the values 30 5 18 11, it gaves the following output:
Area: 575 square feet and 55 square inches.
or : 575.38 square feet.
Note that in the second result, the decimal part doesn't represent 38 square inches, but 0.38 square feet, as a two figures approximation of the value 0.381944.
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