|
-
January 21st, 2009, 06:17 PM
#1
Newbie simple program problem...
Hello everyone,
I am having a simple problem and I was wondering if someone could help me. I am making a simple time calculator program for my class and I am stuck on one part. For some reason in this set of code, it keeps printing like "38228 minutes" no matter what the input for seconds is. This code is not complete.
// time calculator
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
int main ()
{
string a;
string b;
cout << "Please type the form of time you will be using..." << "\n";
cin >> a;
if (a == "seconds")
{
int seconds;
int minutes = seconds /60;
int hours = minutes / 60;
int days = hours / 24;
int weeks = days / 7;
int months = days / 30;
int years = months / 12;
cout << "How many seconds are there?" << "\n";
cin >> seconds;
cout << "There are..." << "\n";
cout << seconds << " seconds" << "\n";
cout << minutes << " minutes" << "\n";
cout << hours << " hours" << "\n";
cout << days << " days" << "\n";
cout << months << " months" << "\n";
cout << years << " years" << "\n";
}
else
{
cout << "Incorrect form of time... Please enter a correct for of time (seconds, minutes, days, etc.)." << "\n";
}
system("PAUSE");
return 0;
}
Thank you ahead of time for any help...
-
January 21st, 2009, 06:29 PM
#2
Re: Newbie simple program problem...
I dont see where you actaully enter the value. You enter the type, and not the value.
HTH,
ahoodin
To keep the plot moving, that's why.

-
January 21st, 2009, 06:33 PM
#3
Re: Newbie simple program problem...
In C++, operations occur line by line, not all at once. As you currently have it coded, you are trying to do the math before you get your input; you are taking whatever garbage number the compiler has initiallized seconds to, and are getting a garbage result after doing math on it.
You need to declare your variables first, then do the math after cin>>seconds;
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|