Time display program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 20
Reputation: dcving is an unknown quantity at this point 
Solved Threads: 0
dcving's Avatar
dcving dcving is offline Offline
Newbie Poster

Time display program

 
0
  #1
Oct 28th, 2004
This is what I have so far, I haven't corrected the input prompt display, nor the output display of the time into HH:MMS.
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:MMS. 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();
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,590
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 710
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Time display program

 
0
  #2
Oct 28th, 2004
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 20
Reputation: dcving is an unknown quantity at this point 
Solved Threads: 0
dcving's Avatar
dcving dcving is offline Offline
Newbie Poster

Re: Time display program

 
0
  #3
Oct 28th, 2004
yeah, i forgot to compile it before i posted it on here...i see all the errors, but i don't know how to correct them...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,590
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 710
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Time display program

 
0
  #4
Oct 28th, 2004
>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>.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 20
Reputation: dcving is an unknown quantity at this point 
Solved Threads: 0
dcving's Avatar
dcving dcving is offline Offline
Newbie Poster

Re: Time display program

 
0
  #5
Oct 28th, 2004
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);
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Time display program

 
0
  #6
Oct 28th, 2004
Wow,

variables and types are a mess here! Narue is right, the compiler will bitch! Oops, where did the structure type come from?

Eric, doing this thing in small stages might help! Like your input section, comment out the other code, Compile and test each stage!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 20
Reputation: dcving is an unknown quantity at this point 
Solved Threads: 0
dcving's Avatar
dcving dcving is offline Offline
Newbie Poster

Re: Time display program

 
0
  #7
Oct 28th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,590
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 710
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Time display program

 
0
  #8
Oct 28th, 2004
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:
  1. // Read HH:MM:SS from standard input
  2. cin>> Hours;
  3. cin.get(); // Toss colon
  4. cin>> Minutes;
  5. cin.get(); // Toss colon
  6. cin>> Seconds;
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC