| | |
Time display program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
This is what I have so far, I haven't corrected the input prompt display, nor the output display of the time into HH:MM
S.
Please give me any suggestions on the program. thanks! :mrgreen:
/* Program definition: The program will ask for and read an elapsed time
in the following format: HH:MM
S. The program will then compute the elapsed
time in seconds and output the result.
Author: Eric Martin
Date: 29, October 2004 */
#include <string>
#include <iostream>
using namespace std;
int main(){
int Usertime, Hours, Mins, Secs, Product1, // declare indentifiers
Product2, TotalSeconds;
cout << "Please enter the number of hours: ";
cin >> Hours;
cout << " " endl;
cout << "Please enter the number of minutes: ";
cin >> Mins;
cout << " " endl;
cout << "Please enter the number of seconds: ";
cin >> Secs;
cout << " " endl;
string Usertime, Hours, Minutes, Seconds;
int FirstC, SecondC, Hours, Mins, Secs;
cin >> Usertime
FirstC = Usertime.find (":",0);
Hours = Usertime.substr(0,FirstC - 0);
SecondC = Usertime.find(":", FirstC + 1);
Minutes = Usertime.substr(FirstC + 1, SecondC - FirstC + 1);
Seconds = Usertime.substr(SecondC + 1, Usertime.size() - SecondC + 1;
int Hours = atoi(Hours.c_str());
int Mins = atoi(Mins.c_str());
int Secs = atoi(Secs.c_str());
Product1 = Hours * 60;
Product2 = (Mins *60) + Product1; // Formulate the user's time into seconds
TotalSeconds = Product2 + Secs;
cout << "There are " << cin TotalSeconds << " total seconds in the given time.";
getch (); //Wrap-up and close
return();
}
S.Please give me any suggestions on the program. thanks! :mrgreen:
/* Program definition: The program will ask for and read an elapsed time
in the following format: HH:MM
S. The program will then compute the elapsedtime in seconds and output the result.
Author: Eric Martin
Date: 29, October 2004 */
#include <string>
#include <iostream>
using namespace std;
int main(){
int Usertime, Hours, Mins, Secs, Product1, // declare indentifiers
Product2, TotalSeconds;
cout << "Please enter the number of hours: ";
cin >> Hours;
cout << " " endl;
cout << "Please enter the number of minutes: ";
cin >> Mins;
cout << " " endl;
cout << "Please enter the number of seconds: ";
cin >> Secs;
cout << " " endl;
string Usertime, Hours, Minutes, Seconds;
int FirstC, SecondC, Hours, Mins, Secs;
cin >> Usertime
FirstC = Usertime.find (":",0);
Hours = Usertime.substr(0,FirstC - 0);
SecondC = Usertime.find(":", FirstC + 1);
Minutes = Usertime.substr(FirstC + 1, SecondC - FirstC + 1);
Seconds = Usertime.substr(SecondC + 1, Usertime.size() - SecondC + 1;
int Hours = atoi(Hours.c_str());
int Mins = atoi(Mins.c_str());
int Secs = atoi(Secs.c_str());
Product1 = Hours * 60;
Product2 = (Mins *60) + Product1; // Formulate the user's time into seconds
TotalSeconds = Product2 + Secs;
cout << "There are " << cin TotalSeconds << " total seconds in the given time.";
getch (); //Wrap-up and close
return();
}
>Please give me any suggestions on the program.
>Product1, // declare indentifiers
>Product2, TotalSeconds;
Bad form. Ending a physical line with a comma so that you can use a single line comment and continue the logical line on the next physical line is poor style.
>Seconds = Usertime.substr(SecondC + 1, Usertime.size() - SecondC + 1;
Your parens don't match.
>int Hours = atoi(Hours.c_str());
>int Mins = atoi(Mins.c_str());
>int Secs = atoi(Secs.c_str());
atoi is declared in <cstdlib>. Because you failed to include that header, calling atoi is wrong.
>cout << "There are " << cin TotalSeconds << " total seconds in the given time.";
cin should not be in this command at all.
>getch (); //Wrap-up and close
getch is a poor choice of function for this operation, but you also neglected to include getch's common header, <conio.h>.
I fail to see how the syntax errors would elude you since they cause the compiler to bitch and moan. What kind of suggestions were you hoping for with a broken program that fails to compile?
>Product1, // declare indentifiers
>Product2, TotalSeconds;
Bad form. Ending a physical line with a comma so that you can use a single line comment and continue the logical line on the next physical line is poor style.
>Seconds = Usertime.substr(SecondC + 1, Usertime.size() - SecondC + 1;
Your parens don't match.
>int Hours = atoi(Hours.c_str());
>int Mins = atoi(Mins.c_str());
>int Secs = atoi(Secs.c_str());
atoi is declared in <cstdlib>. Because you failed to include that header, calling atoi is wrong.
>cout << "There are " << cin TotalSeconds << " total seconds in the given time.";
cin should not be in this command at all.
>getch (); //Wrap-up and close
getch is a poor choice of function for this operation, but you also neglected to include getch's common header, <conio.h>.
I fail to see how the syntax errors would elude you since they cause the compiler to bitch and moan. What kind of suggestions were you hoping for with a broken program that fails to compile?
I'm here to prove you wrong.
>i forgot to compile it before i posted it on here..
The compiler is one of your best tools for finding obvious problems. If you're too lazy to compile code before publishing it then you need to get your priorities straight.
>but i don't know how to correct them...
Then you lack the basic intelligence required to write programs, because I told you what was wrong and the solution can be gleaned from what I said. In order of my comments, excluding the one on style: Add a closing paren before the semicolon, include <cstdlib>, remove cin, and include <conio.h>.
The compiler is one of your best tools for finding obvious problems. If you're too lazy to compile code before publishing it then you need to get your priorities straight.
>but i don't know how to correct them...
Then you lack the basic intelligence required to write programs, because I told you what was wrong and the solution can be gleaned from what I said. In order of my comments, excluding the one on style: Add a closing paren before the semicolon, include <cstdlib>, remove cin, and include <conio.h>.
I'm here to prove you wrong.
I fixed the obvious problems.. but i don't know what to do about these lines?
#include <string>
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main(){
int endl;
int Usertime;
int Hours;
int Mins; // declare indentifiers
int Secs;
int Product1;
int Product2;
int TotalSeconds;
cout << "Please enter the number of hours: ";
cin >> Hours;
cout << endl;
cout << "Please enter the number of minutes: ";
cin >> Mins;
cout << endl;
cout << "Please enter the number of seconds: ";
cin >> Secs;
cout << endl;
int FirstC, SecondC;
cin >> Usertime;
FirstC = Usertime.find (":",0);
Hours = Usertime.substr(0,FirstC - 0);
SecondC = Usertime.find(":", FirstC + 1);
Mins = Usertime.substr(FirstC + 1, SecondC - FirstC + 1);
Secs = Usertime.substr(SecondC + 1, Usertime.size()) - SecondC + 1;
int Hours = atoi(Hours.c_str());
int Mins = atoi(Mins.c_str());
int Secs = atoi(Secs.c_str());
Product1 = Hours * 60;
Product2 = (Mins *60) + Product1; // Formulate the user's time into seconds
TotalSeconds = Product2 + Secs;
cout << "There are " << TotalSeconds << " total seconds in the given time.";
getch (); //Wrap-up and close
return(0);
}
#include <string>
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main(){
int endl;
int Usertime;
int Hours;
int Mins; // declare indentifiers
int Secs;
int Product1;
int Product2;
int TotalSeconds;
cout << "Please enter the number of hours: ";
cin >> Hours;
cout << endl;
cout << "Please enter the number of minutes: ";
cin >> Mins;
cout << endl;
cout << "Please enter the number of seconds: ";
cin >> Secs;
cout << endl;
int FirstC, SecondC;
cin >> Usertime;
FirstC = Usertime.find (":",0);
Hours = Usertime.substr(0,FirstC - 0);
SecondC = Usertime.find(":", FirstC + 1);
Mins = Usertime.substr(FirstC + 1, SecondC - FirstC + 1);
Secs = Usertime.substr(SecondC + 1, Usertime.size()) - SecondC + 1;
int Hours = atoi(Hours.c_str());
int Mins = atoi(Mins.c_str());
int Secs = atoi(Secs.c_str());
Product1 = Hours * 60;
Product2 = (Mins *60) + Product1; // Formulate the user's time into seconds
TotalSeconds = Product2 + Secs;
cout << "There are " << TotalSeconds << " total seconds in the given time.";
getch (); //Wrap-up and close
return(0);
}
give me a break Narue.. it's my first year in college, and my first attempt at Computer Science.. this is only my 4th exercise..and if I knew how to write programs like a pro I wouldn't be on here.. what would you suggest that I do if I don't know how to correct an error in the compiler? i greatly appreciate your expertice
The problem is that you're trying to do too much, too soon. Clearly, the concept of data types is beyond you at this point, so trying to use std::strings and data type conversions will only frustrate you. I would recommend only using cin>> to read integers and cin.get() to throw away formatting characters from the stream, such as ':'. To read the user time you would do something like this:
Once you become proficient with this simple method, you can move on to more concise methods. You also profit from the added benefit of having a stronger understanding of how the input stream works. This understanding can only come from working with streams at the character level.
>give me a break Narue..
Earn one and I'll give it to you.
>it's my first year in college, and my first attempt at Computer Science..
Is this supposed to be an excuse for your lack of common sense?
>and if I knew how to write programs like a pro I wouldn't be on here
I don't expect you to write programs like a pro. I do expect you to make more than a half-assed attempt though.
>what would you suggest that I do if I don't know how to correct an error in the compiler?
Research the error. Despite being intimidating, compiler warnings and errors are usually informative enough to point to a solution.
C++ Syntax (Toggle Plain Text)
// Read HH:MM:SS from standard input cin>> Hours; cin.get(); // Toss colon cin>> Minutes; cin.get(); // Toss colon cin>> Seconds;
>give me a break Narue..
Earn one and I'll give it to you.
>it's my first year in college, and my first attempt at Computer Science..
Is this supposed to be an excuse for your lack of common sense?
>and if I knew how to write programs like a pro I wouldn't be on here
I don't expect you to write programs like a pro. I do expect you to make more than a half-assed attempt though.
>what would you suggest that I do if I don't know how to correct an error in the compiler?
Research the error. Despite being intimidating, compiler warnings and errors are usually informative enough to point to a solution.
I'm here to prove you wrong.
![]() |
Similar Threads
- having a hard time writing a program (C)
- !NEW!:::::!!Time Display Program!! (C++)
- Help with a Time Keeping Program (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Using the STL LIst Container, how do I create, write,read, and store in file.
- Next Thread: convert from char to value of char
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






