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:SS.
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:SS.  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();
}

Edited 3 Years Ago by pyTony: fixed formating

>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?

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...

>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 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);
}

Edited 3 Years Ago by pyTony: fixed formating

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!

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:

// Read HH:MM:SS from standard input
cin>> Hours;
cin.get(); // Toss colon
cin>> Minutes;
cin.get(); // Toss colon
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.

This article has been dead for over six months. Start a new discussion instead.