I fixed the whole program. :twisted: . I compiled it after each step to make sure there were no errors, if so I corrected them. :mrgreen: . I do, however, have 2 questions. :?: .
How do I get the highlighted lines below to center above the **************** :?: And why won't the program give me the total seconds after I entered the formula :?:

#include <string>
	#include <iostream>
	#include <conio.h>
	#include <cstdlib>
	using namespace std;

	int main(){
		string Time; 
		string Hours;  
		string Mins;		// declare indentifiers and strings
		string Secs; 
		int Pos;
		int Pos1;
		int HH;
		int MM;
		int SS;
		int Product1;	
		int	Product2; 
		int TotalSeconds; 
		cout << " " << endl;
		cout << "Enter the number of hours: ";
		cin >> Hours;
		cout << " " << endl;
		cout << "Enter the number of minutes: ";	// Prompt the user
		cin >> Mins;
		cout << " " << endl;
		cout << "Enter the number of seconds: ";
		cin >> Secs;
		//display time in H:M:S
		cout << " " << endl;
		cout << Hours << ":" << Mins << ":" << Secs;		cout << " " << endl;
		cout << "*******************************";
		cout << " " << endl;
		

		

		// string the given time
		
		Pos = Time.find(":",0);
		Hours = Time.substr(0,Pos);
		Pos1 = Time.find(":",Pos+1);
		Mins = Time.substr(Pos+1,Pos1 - Pos+1);
		Secs = Time.substr(Pos1+1,Time.size() - Pos1+1);

		//  Change time to numbers

		HH = atoi(Hours.c_str());
		MM = atoi(Mins.c_str());
		SS = atoi(Secs.c_str());

		// Formulate time into seconds

		Product1 = HH * 60;
		Product2 = (MM * 60) + Product1;
		TotalSeconds = Product2 + SS;		
		cout << " " << endl;
		cout << "There are " << TotalSeconds << 
			" total seconds in the given time.";


			getch();		// Wrap-up and exit
			return (0);
	}

Recommended Answers

All 15 Replies

nevermind about the formula..i figured it out

nevermind that nevermind....how do i get the total seconds to show??

>How do I get the highlighted lines below to center above the ****************
Print a bunch of spaces before Hours:

cout<<"          "<< Hours <<":"<< Mins <<":"<< Secs <<endl;

>And why won't the program give me the total seconds after I entered the formula
Amazing. Getting a decent question out of you people is like pulling teeth. Read very carefully: What is it doing? What were you expecting it to do? How does the first answer differ from the second?

TotalSeconds = ((HH*60)+MM)*60+SS;

still doesn't give me total seconds

>still doesn't give me total seconds
I'm not playing this game. Answer my questions and I'll help you; otherwise you can just sit and wait until someone less capable thinks he knows what the answer is.

>What is it doing?
The formula is taking the atoi variables but I don't know exactly what it is doing because it keeps giving me 0 (zero)

What were you expecting it to do?
I was expecting it to multiply the given hours by 60 (product1)..multiply the given minutes by 60 and add that to the previous answer (product1)... and then take that answer (product2) and add the given seconds, showing the total seconds of the time in standard form

>How does the first answer differ from the second?
It is in parenthases...and it adds the previous answer...?

Thank you, now was that so hard? The problem is that you're trying to parse an empty string. Time has the default contents (""), so your finds and substr's don't do very much. As a result, your atoi calls also don't do too much. Just be happy you aren't doing any divisions. ;)

I tried to do something similar but much more simple, but i dont know what i did wrong. Wont compile it right.

//Clock for seconds
#include <iostream.h>

int main ()
{
    int HH;
    int MM;
    int SS;
    int result;
    cout << "Enter Hours :HH:";
    cout << " ";
    cin >> HH;
    cout << "Enter Minutes :MM:";
    cout << " ";
    cin >> MM;
    cout << "Enter Seconds :SS:";
    cout << " ";
    cin >> SS;
    cout << HH ":" MM ":" SS;
    cout " ";
    cout "*****************";
    result = HH * 60 + MM * 60 + SS;
    cout << result;
    cin.get ();
    return 0;
}

Don't know if i even did anyhting right, im just learning and havn't even finished the Tutorial, and am on like 1.4 of it, and tried to do it,dont know much about C++ yet...

I tried to do something similar but much more simple, but i dont know what i did wrong. Wont compile it right.

//Clock for seconds
#include <iostream.h>

int main ()
{
    int HH;
    int MM;
    int SS;
    int result;
    cout << "Enter Hours :HH:";
    cout << " ";
    cin >> HH;
    cout << "Enter Minutes :MM:";
    cout << " ";
    cin >> MM;
    cout << "Enter Seconds :SS:";
    cout << " ";
    cin >> SS;
    cout << HH ":" MM ":" SS;
    cout " ";
    cout "*****************";
    result = HH * 60 + MM * 60 + SS;
    cout << result;
    cin.get ();
    return 0;
}

Don't know if i even did anyhting right, im just learning and havn't even finished the Tutorial, and am on like 1.4 of it, and tried to do it,dont know much about C++ yet...

There are a couple of << missing and hour has 60*60 seconds.

Thx;) just looked over it on the site, ill try what i can and try to get it to work

//Clock for seconds
#include <iostream.h>

int main () {
    int HH;
    int MM;
    int SS;
    int result;
    cout << "Enter Hours :HH:";
    cout << " "<<endl;
    cin >> HH;
    cout << "Enter Minutes :MM:";
    cout << " " endl;
    cin >> MM;
    cout << "Enter Seconds :SS:";
    cout << " " endl;
    cin >> SS;
    cout << HH << ":" << MM << ":" << SS; cout " " endl;
    cout " " endl;
    cout "*****************";
    result = ((HH * 60) + MM * 60) + SS;
    cout << result;
    cin.get ();
    return 0;
}

Well, thisis whats updated, and the errors it gives me are as fallow:

2 C:\Dev-Cpp\include\c++\3.3.1\backward\iostream.h:31, from C:\Dev-Cpp\Time.cpp In file included from C:/Dev-Cpp/include/c++/3.3.1/backward/iostream.h:31, from C:/Dev-Cpp/Time.cpp

2 C:\Dev-Cpp\Time.cpp from C:/Dev-Cpp/Time.cpp

2 C:\Dev-Cpp\include\c++\3.3.1\backward\backward_warning.h:32 #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.

C:\Dev-Cpp\Time.cpp In function `int main()':

13 C:\Dev-Cpp\Time.cpp syntax error before `;' token

16 C:\Dev-Cpp\Time.cpp syntax error before `;' token

18 C:\Dev-Cpp\Time.cpp syntax error before string constant

Any ideas whats wrong now???

You're missing a few << operators in your output statements.

//Clock for seconds
#include <iostream.h>
int main () 
{
    int HH;
    int MM;
    int SS;
    int result;
    cout << "Enter Hours HH: ";
    cout << " ";
    cin >> HH;
    cout << "Enter Minutes MM: ";
    cout << " ";
    cin >> MM;
    cout << "Enter Seconds SS:";
    cout << " ";
    cin >> SS;
    cout << HH << ":" << MM << ":" << SS;
    cout << " ";
    cout << "*****************" << endl;
    result = ((HH * 60) + MM * 60) + SS;
    cout << result;
    cin.get ();
    return 0;
}

Alright fixed it up, it compiles, and runs, but i get to the point were it hits:

Enter Hours HH: 06
Enter Minutes MM: 30
Enter Seconds SS: 59

After the seconds, i press enter, and it closes the file. Any ideas on what i did wrong?

>Any ideas on what i did wrong?
Yes, your use of cin's >> operator left a newline in the stream for cin.get() to read immediately. The desired effect of pausing until you had a chance to read the output and type enter never happened. This is a typical problem when the naive cin.get() solution is used for keeping a window open and the program grows enough to introduce >>'s issues.

There are several ways of fixing the problem, but most of them are what you would consider obscure. The simplest solution is a loop:

//Clock for seconds
#include <iostream.h>
int main () 
{
    int HH;
    int MM;
    int SS;
    int result;
    cout << "Enter Hours HH: ";
    cout << " ";
    cin >> HH;
    cout << "Enter Minutes MM: ";
    cout << " ";
    cin >> MM;
    cout << "Enter Seconds SS:";
    cout << " ";
    cin >> SS;
    cout << HH << ":" << MM << ":" << SS;
    cout << " ";
    cout << "*****************" << endl;
    result = ((HH * 60) + MM * 60) + SS;
    cout << result;
    while ( cin.get() != '\n' )
      ;
    cin.get ();
    return 0;
}

This loop clears the input stream so that the next call to cin.get() will be forced to wait for input.

Thankyou very much, the tutorial showed me how to do that, but i just spaced that thats something i should do...heh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.