Simple C++ Unit Conversion [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to create a beginner-level program that converts input from one unit of time to another. Essentially what I'm after is this: the user inputs a number of days, hours, and minutes, and those values get converted to both weeks and seconds. Here is what I have so far, which displays "0" for both weeks and seconds no matter the input.
EDIT: Fixed multiplication, new code for weeks. Still a garbage value.
#include <iostream>
using namespace std;
int main()
{
int days, hours, minutes, weeks, seconds;
cout << "--------------------" << endl;
cout << "Enter a number of days: ";
cin >> days;
cout << "Enter a number of hours: ";
cin >> hours;
cout << "Enter a number of minutes: ";
cin >> minutes;
seconds = (days*86400)+(hours*3600)+(minutes*60);
weeks = seconds/604800;
cout << "--------------------" << endl;
cout << "Equal to " << seconds << " seconds" << endl;
cout << "Equal to " << weeks << " weeks" << endl;
return 0;
}
I'm sure there is something I'm doing wrong in the conversion part. Any help is much appreciated, thank you.

All of your variables are ints, but you are performing very large divisions on them. When an int is divided into a non-whole number, the decimal is dropped rather than rounded (so 3/2 = 1, for example). The simplest way to fix this is to turn all your variables into doubles and possibly round them to two digits of precision using setprecision on cout. Alternatively, look into std::round

Weeks is int and also all the elements of your division, this implies in an integer division. To get the proper result, you should make weeks float and convert one of the operands BEFORE the division, like:
float weeks = float(seconds) / 604800;
or
float weeks = seconds / 604800.0;
I strongly disencourage the second way because makes code more difficult to read.

Related

I cant seem to get the amount of decimals right using setprecision c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I'm currently getting into subroutines/subprograms or whatever you call them in english and in this specific assignment that i'm trying to solve I have to calculate the average length of two words.
void Length(string const text_1,
string const text_2,
int & total_length,
double & mean_length)
{
total_length = text_1.length() + text_2.length();
mean_length = static_cast<double>(total_length) / 2;
}
void Length_Program(int val)
{
string text_1;
string text_2;
int total_length{};
double mean_length{};
cout << "Mata in två ord: ";
cin >> text_1 >> text_2;
cout << "Totallängd: ";
Length(text_1, text_2, total_length, mean_length);
cout << total_length << endl;
cout << "Medellängd: " << fixed << setprecision(1) << mean_length;
}
I have set the precision to setprecision(1) and I assume it will only write one decimal but I keep getting two decimals.
my example is: abcd E
it should say that it is an average of 2.5 words but it says 2.51 for some reason. Can someone help me understand what i'm doing wrong?
Your problem is that you forgot << endl on your last output line. The return code shown by the OS is appended to your output. The setprecision is working just fine.

For loop displaying incorrect information stored inside array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this really simple program, with a for loop designed to run 3 times.
For every iteration, it will ask the user to enter in the weather. Each time, the user's input is stored in an integer array called C.
Once the loop concludes, I have another for loop that print's out the user's input. This next loop works fine for the 2 values, but gives off some weird messed up value once it reaches the third iteration.
int main(){
//Variable declaration:
int days;
int C[2];
int F[2];
for(days=0; days<3; days++){
cout << "What is the temperature in celsius for day " << days + 1 << ":" << endl;
cin >> C[days];
F[days] = (C[days] * 9/5) + 32;
}
cout << "\nCelsius\t\t Farenheit\n-------\t\t ---------" << endl;
for(days =0; days <3; days++){
cout << C[days] << "\t\t " << F[days] << endl;
}
return 0;
}
This next loop works fine for the 2 values but gives off some weird messed up value once it reaches the third iteration.
Your definition of C states it has 2 entries, but your loop runs for
three iterations (0, 1, and 2).

Simple C++ calculation giving odd outputs [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm working on a C++ program to calculate a speeding ticket fine for different speeds.
"The speeding ticket fine policy in (City) is $50 plus $5 for each mph over the limit plus a penalty of $250 for any speed over 85 mph. Write a program that accepts a speed limit and a clocked speed and either prints a message indicating the speed was legal or prints the amount of the fine, if the speed is illegal.In the program, you also need to display whether the number of miles over the speed limit, and if he/she is driving over 85 mph."
Here's my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double speedlimit,clockedspeed,speeddifference,fineunder85,fineover85;
speeddifference = clockedspeed-speedlimit;
fineunder85 = (speeddifference*5)+50;
fineover85 = (speeddifference*5)+300;
cout<<"Enter the speed limit: "<<endl;
cin>>speedlimit;
cout<<"Enter the clocked speed: "<<endl;
cin>>clockedspeed;
if ((clockedspeed > speedlimit) && (clockedspeed > 85))
{
cout<<"The clocked speed is: Illegal"<<endl;
cout<<"Miles over the speed limit: "<<setprecision(2)<<speeddifference<<endl;
cout<<"Driving over 85 mph: Yes"<<endl;
cout<<"The fine is: $"<<setprecision(2)<<fineover85<<endl;
}
else {
cout<<"The clocked speed is: Illegal"<<endl;
cout<<"Miles over the speed limit: "<<setprecision(2)<<speeddifference<<endl;
cout<<"Driving over 85 mph: No"<<endl;
cout<<"The fine is: $"<<setprecision(2)<<fineunder85<<endl;
}
}
I'm getting weird outputs for (speeddifference) like "-7e-310" and the fine isn't adding the additional penalty ($5/mile over the limit) but is only outputting "$50" or "$300".
Just looking for help, I've searched everywhere and I've come up short.
ty all
You're calculating the speed difference and fines before you even input the numbers! Remember that the program runs line by line (without functions, classes, etc. just procedural programming). Furthermore, you declare the variables without defining them with a set value which is why you are getting random values.
Move the:
speeddifference = clockedspeed-speedlimit;
fineunder85 = (speeddifference*5)+50;
fineover85 = (speeddifference*5)+300;
to after you receive input.
Edit:
There seems to be numerous other errors and I've tried my best effort to fix all of them. Here's a reworked version.
#include <iostream>
int main()
{
double fine = 0;
double speed_limit;
double clocked_speed;
std::string over_85 = "no";
std::cout << "Speed limit:\n";
std::cin >> speed_limit;
std::cout << "Clocked speed:\n";
std::cin >> clocked_speed;
double speed_difference = clocked_speed - speed_limit;
fine += 50 + speed_difference * 5;
if (clocked_speed > 85) {
fine += 250;
over_85 = "yes";
}
if (clocked_speed <= speed_limit) {
std::cout << "\nLegal\n";
}
else {
std::cout << "\nIllegal\n"
<< "Miles over: " << speed_difference << '\n'
<< "Over 85mph: " << over_85 << '\n'
<< "Fine: $" << fine << '\n';
}
}
Changes:
Set a variable string over_85; to remove need of complicated if statement.
Remove need for two different fines and instead have one fine with an if statement to add the $250 fine if necessary.

Beginning C++: using if else, n--, number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am struggling to get this program to work. Could someone please look it over to see what is wrong? (The compiler says that sum_positive is not defined in this way.) Also, I have searched this problem and found an answer on here, but I did not see the problem that way and want to avoid plagiarism when I turn in my homework. Thank you!
Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
Program:
#include <iostream>
using namespace std;
int main()
{
int number, n = 10, sum_positive = 0, sum_negative = 0, sum = 0;
int count = 0, positive_count = 0, negative_count = 0;
cout << "Please enter in 10 whole numbers, one at a time." << endl;
do
{
cout << "Please enter another number." << endl;
cin >> number;
n--;
}
while (n > 0);
if (number >= 0)
{
sum_positive += number;
//positive_count++; count++;
}
else
{
sum_negative += number;
//negative_count++; count++;
}
sum = sum_positive + sum_negative;
cout << sum_postive << endl;
cout << sum_negative << endl;
cout << sum << endl;
return 0;
}
there is a spelling mistake in your code of sum_positive in line (cout << sum_postive << endl;) - you have written sum_postive but you declared sum_positive.
you are running a loop and taking input in the same variable number, so it will overwrite all the inputs and will store only the last value in variable number which user entered. So your sum will always be equal to the last value user entered.
For this you need to use an array.
Example:
int number[10], n=10, sum_positive=0;
do{
cout<<"e`enter code here`nter number:";
cin>>number[n];
n--;
}while(n>0);
now for sum also you need to use an loop.
If you do not know about an array, study about it how to use it.

Returning values from a separate function in C++? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am doing a programming assignment in my CS class using C++ that is giving me problems. The assignment is to write a program that asks the user to input a Fahrenheit temperature. The program must then output the Celsius equivalent of that temperature and the Celsius equivalents of the next 20 Fahrenheit temperatures. This must all be done using a separate function to complete the conversion calculation as well as a function prototype.
I got everything to work except my Celsius outputs all show a value of 0. I cannot seem to find any errors or figure out what is wrong...Please Help!
#include <iostream>
#include <iomanip>
using namespace std;
double Celsius(double temp);
int main() {
double fTemp, cTemp;
cout << "Please input a temperature in degrees Fahrenheit: ";
cin >> fTemp;
cout << "F Temp" << " " << "C Temp"<< endl;
cout << "-------------------" << endl;
for(int count=1; count<= 20; count++){
cTemp = Celsius(fTemp);
cout << setw(6) << fTemp << setw(12) << cTemp<< endl;
fTemp++;
}
return 0;
}
//*************Celsius Function****************//
double Celsius(double temp){
double newC;
newC = (temp-32) * (5/9);
return newC;
}
//************************************************
(5/9) is integer-integer division. You might've as well typed 0 there. Replace it with (5.0/9.0) and it should be fine.
One could simply try (5.0/9.0) or use a type cast by doing something like
double Celsius(double temp){
double newC;
newC = (temp-32) * (float)5/9;
return newC;
}
Thereby, you will not be getting a straight 0 in your outputs.