After watching the 18 videos of Bucky's C++ Tutorial Series, I designed a simple stopwatch. Opinions?

+3 Jay Bennett · January 11, 2015
So I've watched 1-18 of the C++ tuts, and after seeing the 18th one on while loops, I managed to make a simple stopwatch. Not anything fancy, just a simple stopwatch that can go all the way up to 99:59:59. Any opinions on my code? Bearing in mind that I'm working on Windows with Visual Studio 2013 (so don't post about the "system("cls")" and "Sleep(1000)"

Source code:
// StopwatchTest - Trying to build a stopwatch using a while loop
//

#include "stdafx.h"
#include <iostream>
#include "Windows.h"
using namespace std;

int main(){

int hoursTens = 0;
int hoursUnits = 0;
int minutesTens = 0;
int minutesUnits = 0;
int secondsTens = 0;
int secondsUnits = 0;

while (hoursTens < 10, hoursUnits < 10) {
cout << hoursTens << hoursUnits << ":" << minutesTens << minutesUnits << ":" << secondsTens << secondsUnits;
secondsUnits = secondsUnits + 1;
Sleep(1000);
system("cls");
if (secondsUnits == 10) {
secondsUnits = 0;
secondsTens = secondsTens + 1;
}
if (secondsTens == 6) {
secondsTens = 0;
minutesUnits = minutesUnits + 1;
}
if (minutesUnits == 10) {
minutesUnits = 0;
minutesTens = minutesTens + 1;
}
if (minutesTens == 6) {
minutesTens = 0;
hoursUnits = hoursUnits + 1;
}
}

//This will be executed AFTER the while loop ends
hoursTens = 9;
hoursUnits = 9;
minutesTens = 5;
minutesUnits = 9;
secondsTens = 5;
secondsUnits = 9;

cout << hoursTens << hoursUnits << ":" << minutesTens << minutesUnits << ":" << secondsTens << secondsUnits << endl;
return 0;
}

I shoulda probably used strings to join things together, but this is the basic function of it.

So, any opinions? I'm still learning C++ (as I said, I've only watched 18 videos (so far)) and this was me just trying to use my knowledge to try and piece together a relatively-useful-but-not-really-useful program :P

Post a Reply

Replies

Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · January 11, 2015
you are doing well so far. Gonna check it out
0 Jay Bennett · January 12, 2015
Updated my code, so now it actually updates the hoursTens integer (my mistake :P), when it reaches it's maximum time it doesn't take another second before the program finishes, and I removed a couple of lines that weren't needed, and added some that were needed:


// StopwatchTest - Trying to build a stopwatch using a while loop
//

#include "stdafx.h"
#include <iostream>
#include "Windows.h"
using namespace std;

int main(){
int hoursTens = 0;
int hoursUnits = 0;
int minutesTens = 0;
int minutesUnits = 0;
int secondsTens = 0;
int secondsUnits = 0;

while (hoursTens < 10, hoursUnits < 10) {
cout << hoursTens << hoursUnits << ":" << minutesTens << minutesUnits << ":" << secondsTens << secondsUnits;
secondsUnits = secondsUnits + 1;
Sleep(1000);
system("cls");
if (secondsUnits == 10) {
secondsUnits = 0;
secondsTens = secondsTens + 1;
}
if (secondsTens == 6) {
secondsTens = 0;
minutesUnits = minutesUnits + 1;
}
if (minutesUnits == 10) {
minutesUnits = 0;
minutesTens = minutesTens + 1;
}
if (minutesTens == 6) {
minutesTens = 0;
hoursUnits = hoursUnits + 1;
}
if (hoursUnits == 10) {
hoursUnits = 0;
hoursTens = hoursTens + 1;
}
//Stop the while loop
if (hoursTens == 9, hoursUnits == 9, minutesTens == 5, minutesUnits == 9, secondsTens == 5, secondsUnits == 9) {
hoursTens = 11;
hoursUnits = 11;
}

}

hoursTens = 9; //Set it back to 9 to create the illusion that it never changed & it reached the maximum time
hoursUnits = 9; //Set it back to 9 to create the illusion that it never changed & it reached the maximum time
cout << hoursTens << hoursUnits << ":" << minutesTens << minutesUnits << ":" << secondsTens << secondsUnits << endl;
return 0;
}
0 c student · January 14, 2015
http://i.imgur.com/nQRabjS.png
:silly:
0 Steven vd_W · January 14, 2015
Nice!
+1 Jay Bennett · January 20, 2015
cstudent: remember that this was made in Visual Studio 2013 using the VC++ compiler. I have no idea if it works with the GCC / G++ compiler.

Steven vd_W: ty :D
  • 1

C++

106,979 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator