Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having issues with converting age in years to seconds. It comes up with a wrong number and a negative one. Can someone please help?
#include <iostream>
using namespace std;
int main()
{
int years;
years = years;
cout << "How many years have you lived: " ;
cin >> years;
cout << "You have lived: " << years; cout << " years:" << endl;
int days = 0.5 + (years * 365.25);
cout << days; cout << " days" << endl;
int hours = days * 24;
cout << hours; cout << " hours" << endl;
int minutes = hours * 60;
cout << minutes; cout << " minutes" << endl;
int seconds = minutes * 60;
cout << seconds; cout << " seconds" << endl;
return 0;
}
You are getting wrong/negative seconds because of overflow problem. int can't hold so big numbers (seconds).
So, use long long minutes and long long seconds instead of int
And, don't forget to remove the line
years = years;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have tried multiple times but still fail. The hours converted is correct but it will has additional 2 seconds for any input given.
#include <iostream>
using namespace std;
class Time
{
private:
int T1, T2, T3;
public:
void getseconds(void);
void getinhours(void);
};
void Time::getseconds(void)
{
cout << "Enter time-1 in seconds: ";
cin >> T1;
cout << endl;
cout << "Enter time-2 in seconds: ";
cin >> T2;
cout << endl;
}
void Time::getinhours(void)
{
T3 = (T1 + T2) / 3600;
cout << "The Time is: " << T3 /*hours*/ << " : " << (T3 % 3600) / 60 /*seconds*/ << " : " << T3 % 60 /*minutes*/;
cout << endl;
}
int main()
{
Time t;
t.getseconds();
t.getinhours();
system ("pause");
}
Your error is dividing at the wrong time and place:
T3 = T1 + T2;
cout << "The Time is: " << T3 / 3600 /*hours*/ << " : " ;
cout << (T3 / 60) % 60 /*minutes*/ << " : ";
cout << T3 % 60 /*seconds*/ << endl;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I developed this program to add and the retrieve 15$ in the stack.
I was wondering is there another more efficient way to write my code.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
const int MAX = 100;
int count;
stack<int, vector<int> > billStack;
for (int i=0; i<15; i++) {
billStack.push(i); // add 15 bills onto stack
}
cout << "The stack has " << billStack.size() << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
for (int i = 0; i< cash; i++) {
billStack.pop();
}
cout << "Cash out :" << cash << ". Remaining: " << billStack.size() << endl;
return 0;
}
Considering that you don't ever use the actual contents of the stack, just its size:
#include <iostream>
using namespace std;
int main()
{
int stackSize = 15;
cout << "The stack has " << stackSize << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
stackSize -= cash;
cout << "Cash out :" << cash << ". Remaining: " << stackSize << endl;
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
this is my sample main :
double hours = 35.45;
double rate = 15.00;
double tolerance = 0.01000;
cout.setf(ios::scientific);
cout << "Scientific notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate << ", pay = "
<< hours * rate << ", tolerance = " << tolerance << endl << endl;
cout.setf( ios::fixed ); // if i replace but cout << fixed, it works
cout << setprecision( 3 );
cout << "Fixed decimal notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate << ", pay = "
<< hours * rate << ", tolerance = " << tolerance << endl << endl;
system( "pause" );
Why are they not equal? if they are equal then what is wrong here?
The flag std::ios_base::fixed is just one of two flags in std::ios_base::floatfield. The function std::ios_base::setf() is oblivious of the relationship between the different flags and merely sets a bit pattern. If you want to set std::ios_base::fixed and clear other fields from the subgroup, you'd use
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
When you use the manipulator std::fixed it is equivalent to this call.
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
int elapsedtime(int time1, int time2)
{
int minutes1 = ((time1/100*60)+(time1%100);
int minutes2 = ((time2/100*60)+(time2%100);
return(minutes2-minutes1);
}
/**************************************************************************************
do
{
cout << "What is your starting time? ";
cin >> time1;
cout << "What is your ending time? ";
cin >> time2;
if (time2>=time1)
{
cout << "Your elapsed time is " << elapsedtime <<endl;
cout << "If you would like to enter other times?(Y/N)" <<endl;
cin >> choice;
}
else
{
cout << "Error: Your starting time must be lower than your ending time."<<endl;
cout << "If you would like to enter other times?(Y/N)" <<endl;
cin >> choice;
}
}
I keep getting an output of 1 for every elapsed time?
You have
cout << "Your elapsed time is " << elapsedtime <<endl;
Did you mean
cout << "Your elapsed time is " << elapsedtime(time1, time2) <<endl;
Update
For some reason,
cout << "Your elapsed time is " << elapsedtime <<endl;
does not produce a compiler error under g++ 4.7.3 even though the intent is to use elapsedtime(time1, time2).
I was able to successfully compile and build:
#include <iostream>
using namespace std;
int elapsedtime(int time1, int time2)
{
return 0;
}
int main()
{
cout << "Your elapsed time is " << elapsedtime <<endl;
}
Perhaps another SO post is needed to answer the question, "How does g++ not report error with the above code?".
sI am learning C++ and I keep getting a strange error. setprecision is giving me multiple decimal points in the one answer.
Why doe the output have multiple decimal points?
Program
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main () {
int time, counter, range;
double investment, rate, balance;
cout << "Investment amount: " << endl;
cin >> investment;
cout << "Rate: " << endl;
cin >> rate;
cout << "Length of time: " << endl;
cin >> time;
cout << "Incremental Range: " << endl;
cin >> range;
balance = 0;
counter = 0;
cout << "\n\n\nRate \t 5 Years \t 10 Years \t 15 Years \t 20 Years \t 25 Years \t 30 Years \n" << endl;
cout << fixed << setprecision(2);
while(counter < 6)
{
counter = counter + 1;
balance = investment * pow((1+ rate/100), time);
cout << setw(2) << rate << setw(12) << balance;
time = time + range;
}
cout << endl;
return 0;
}
Output
Rate 5 Years 10 Years 15 Years 20 Years 25 Years 30 Years
5.00 1276.285.00 1628.895.00 2078.935.00 2653.305.00 3386.355.00 4321.94
As you can see 1276.285.00 etc should be 1276.28.
Why does the output have multiple decimal points?
I don't think you want to write rate each iteration:
cout << setw(2) << rate << setw(12) << balance;
The last part of "double-decimal-point" number is your rate.