This is my first time on this site, and as such, the format of this question may be wrong in some ways. That being said, here is the exercise I am struggling with.
"Write a C++ program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. (For example, if the elapsed time is 9630 seconds, then the output is 2:40:30.)"
Here is what I have programmed so far in Code::Blocks-
#include <iostream>
#include <string>
using namespace std;
int main()
{
int seconds;
int hours;
int minutes;
int seconds1;
cout << "How long did the event take in seconds?" << endl;
cin >> seconds;
hours= seconds/3600;
minutes= %(seconds/3600);
seconds1= %(seconds/minutes);
cout << "The event took"<<hours<<":"<<minutes<<":"<<seconds1<<"." << endl;
cout << endl;
return 0;
}
My main question is how I would add the modulus operation to this program. I know I need to include it, as there is a clear remainder after variable: hour, and minute.
Also, two error codes appear when this program is run through the compiler:
Line 16) error: expected primary-expression before '%' token
Line 17) error: expected primary-expression before '%' token
minutes= %(seconds/3600);
the syntax for the mod, % operator in an assignment is
minutes = value1 % value2;
You have no left-hand argument, here value1, in your expression.
The seconds1 part is straightforward, you just take the seconds mod 60.
seconds1 = seconds % 60;
The minutes part you'll have to think about a little. You can do it in two ways.
You need to subtract the number of seconds already accounted for by the hours value, then divide by 60. No mod required, or rather a manual mod.
using mod. Get the remainder of the hours not accounted for by the hours calculation, % 3600 then calculate the number of minutes this is.
Related
I am attempting to create a simple program that calculates a users age in seconds after they give their age in years. It works fine for ages 0-68 but any age of 69 or higher breaks the program and just spits out the same wrong number every time. The program is listed below and any help would be appreciated.
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Please enter your age in years ";
cin >> age; //Grabs the users age
unsigned long long int result = age*365*24*60*60; //calculates the users age in seconds
cout << "Your age in seconds is: " << result << " seconds";
return 0;
}
The way C++ works here is basically:
int temp = age*365*24*60*60;
unsigned long long int result = static_cast<unsigned long long>(temp);
So, you can probably see that the expression will overflow an int at around 69 years old (on your architecture). So, you want to force the calculation to work in unsigned long long, so the easiest way is to force one of the values to be unsigned long long, which will make the expression also unsigned long long. For example:
unsigned long long int result = age*365ULL*24*60*60; //calculates the users age in seconds
// ^^^ declare constant as type unsigned long long
Unsigned Long int ranges from -2,147,483,648 to 2,147,483,647
So for any value less than or equal to 68, age in seconds is 2,144,448,000 or less which falls within range.
However for Age 69, Age in seconds comes out to be 2,175,984,000 which overflows the range.
I advise using long Double.
Very new to C++. Doing my best to get a good handle on this.
The extra couts after the read statements were for troubleshooting purposes to make sure it was actually reading user input.
I feel like this is a dumb question but iv been scouring the forums to find something similar to my issue.
I cannot for the life of me figure out how to get this program to start calculating the correct interest. When comparing to an online interest calculator it comes up with a very different number than what the program is calculating.
I have spent tons of hours troubleshooting this and texting different algorithms but I have yet to come out on-top. I dont normally like asking for help because I learn by struggling through something but this one is throwing me for a loop...
So I think I might either be having an issue with the libraries I am using or a calculation error. I know the code is a bit messy and I am learning how to clean it up but as for now I just want to get the code calculating compound interest based off of user input. I have added comments to explain what each section of code does to help with my messiness.
I dont believe its a syntax error but with the way I am either using a few of the commas in the algorithm or my parentheses are somehow wrong.
the formula I am using is,
A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate, times
compounded)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double rate;
double time;
double principle;
double amount;
double amount2;
//Asking for the amount of money that the user would like to invest
cout << "What is the amount of money you would like to invest? ";
cin >> principle;
cout << principle << endl;
//Asking for the interest rate that will be compounded annually
cout << "What is the interest rate you would like to calculate? ";
cin >> rate;
//interest rate divided by 100 so it can be multiplied by the principle and the amount of time the money will be invested later in code.
rate /= 100;
cout << rate << endl;
//Asking for the amount of years that the user will invest.
cout << "How many times would you like to compound your money? ";
cin >> time;
cout << time << endl;
//calculation for amount of money that will be made after all user input is input
amount2 = pow(rate, time);
cout << amount2 << endl;
amount = (principle * (1 + rate/time), amount2);
//Output data after all user data is input and calculated.
cout << "Your will have "; cout << amount; cout << " dollars in
interest! "<< endl;
return 0;
}
This line:
amount = (principle * (1 + rate/time), amount2);
Does absolutely nothing with principle * (1 + rate/time). That expression is evaluated, and then the results are discarded, leaving amount to be assigned the value in amount2. I assume you were trying to call a function with those two expressions as parameters.
http://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator
In a comma expression E1, E2, the expression E1 is evaluated, its
result is discarded, and its side effects are completed before
evaluation of the expression E2 begins.
Now that we have your formula:
A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate * times compounded)
I am not certain you are computing the formula you have listed above, and note the correction to the second line, where I replace the comma with multiplication.
amount2 = pow(rate, time);
Computes rate^time, where you should have:
amount2 = 1 * time; // Where one is the times compounded per period (year)
So that your other line can read:
amount = principle * pow((1 + rate, amount2);
Because you are compounding once per year, you are dividing rate by 1, not by the number of years. By the same token, amount2 is now strictly equal to time;
So I am trying to write a simple program that takes the time input by the user, and calculates the angles between and around the hands of an analog clock. I have run the program without parsing and 2 separate inputs but I wanted to parse the time at the colon (say, 12:35) and set the left side to an hour variable and the right side to a minutes variable. However, reading examples is hard when you don't know what some of the lines of code mean. Would somebody help me with an example and explain what each line is doing and why use that method?
Are you looking for something like this:
unsigned int hours;
unsigned int minutes;
char colon;
std::cin >> hours >> colon >> minutes;
It inputs the hours first, then the colon character, then the minutes.
This is what you are looking for:
string time = "12:35";
unsigned int hour, minutes;
sscanf(time.c_str(), "%2d:%2d", &hour, &minutes);
cout << "Hour: " << hour << endl;
cout << "Minutes: " << minutes << endl;
I'm taking a C++ class, as I just started my first year of college and it has been destroying me. I have been attempting for hours to do my homework but can't come to a solution.
My assignment is to make a C++ program that when given minutes will tell you years and days.
We have been using float and cout and cin in class and some % and / structures which are foreign to me. If someone could help that would be great because I lost all hope at this point.
#include <iostream>
using namespace std;
float = minutes
float = hours
float = days
float = years
float = seconds
int main()
{
using namespace std;
int days, years, minutes, hours, seconds;
cout << "Please Enter Minutes" << endl;
cin >> minutes;
days = input_minutes / 60 / 60 / 24;
hours = (input_minutes / 60 / 60) % 24;
minutes = (input_minutes / 60) % 60;
seconds = input_minutes % 60;
cout << days << " seconds = " << years << " years ";
cin.get();
cin.get();
return 0;
}
I took the liberty to look at the code that you have into the comment box;
first thing :
Declare a variable to store the input value or save the result of a computation
int days; //<--- declaration of a int variable called days
so this in line I don't know what you were trying to do but float = minutes float = hours float = days float = years float = seconds
Please don't do it
second thing:
Don't repeated `using namespace std` twice. Therefore remove it from the `int main` function.
Third :
your computation is kinda OFF, try to solve mathematically then code it.
your code should look like that: (This is not the answer)
#include <iostream>
using namespace std;
int main()
{
int days, years, input_minutes, hours, seconds,minutes;
cout << "Please Enter Minutes" << endl;
cin >> input_minutes;
days = input_minutes / 60 / 60 / 24;
hours = (input_minutes / 60 / 60) % 24;
minutes = (input_minutes / 60) % 60;
seconds = input_minutes % 60;
cout << days << " seconds = " << years << " years ";
system("Pause");
return 0;
}
I can give you a little help on what each of those means. Here are the non-super technical definitions.
A float is an integer that can have decimal places.
cout Will output the value next to <<
cin will store a value from an input (cin >> x) will store the user input in x.
% is the modulus character. It will return the remainder after the division of two numbers. 3%2 will return 1.
/ is just simple, plain old, division.
Joe, I think we shouldn't do that job for you, and this is not exactly a "technical" question. Considering this, I will try to give you some ideas.to get some extra scores:
1 - take the user input, "the number of minutes" from command line args, like:
int main(int argc, char *argv[]) {
int num_mim = atoi(argv[1]);
2 - to take the number of years do int num_years = num_mins / (60 * 24 * 365);
(not taking into account leap years)
3 - to take the number of days do int num_days = num_mins % (60 * 24 * 365) / 60 / 24;
of course simplify the operations by performing the multiplications and divisions that can be made by hand if you want.
% is the modulos operator, it gives you the remainder of the dvision, here we use it to get the remainder of minutes from the years cound and express it in days.
Now it is up to you, look for additional sources of info and assemble your homework.
I am trying to perform a calculation to get from the total microseconds of the current time back to the current time in HHMMSSFFF format by performing a series of calculations. However, when I try to subtract the hours*3600000000 from the total number of microseconds, the returned value is not correct (it is off by one order of magnitude and the number itself is wrong. Does anyone know how to fix this? I tried using long long int and long double but these both outputted the same value. I have copied below the code and the resulting output in the console.
The times are stored in a vector as data comes in (aka a time stamp) which is why there is a temp_counter. I am using time_duration in the boost::posix_time library.
long double total = cur_time.at(temp_counter).total_microseconds();
cout << total << endl;
int hours = total/(3600000000);
cout << hours << endl;
long long int temp = hours*3600000000;
cout << std::setprecision(20) << temp << endl;
total = total - temp;
cout << total << endl;
Output:
35465976558
9
2335228928
33130747630
By my calculations, temp should actually be 32400000000 and the new total should be 3065976558.
The calculation is:
35465976558/
3600000000 = 9,851660155 cast to int equals 9
9 * 3600000000 = ... and here the crap hits the fan because 3600000000 cannot be put into a 32 bit int and doesn't seem to be cast correctly to long long, hence you should cast the right hand side to long long int.
But as you are getting a long double in the first assignment: Why not using long double (or at least double) all the way and avoid the casting horror?