HI!
Today I would like to know what do you think about the code below:
It's a timer.
Please tell me would you have done something differently?
does the code has something that makes it bad or is it well made?
also what do you think about using the windows.h? should i use(it has some usefull commands that i can use pretty easily(used to program using the batch language)) or is it horrible mistake?
#include <iostream>
#include<ctime>
#include "windows.h"
usingnamespace std;
int A1(){
//I use this function to take the user input and check that it is 1 or 2.
//if its not 1 or 2 it will cause infite loop. to be honest with you this function
//really does not prefent the infite loop if user input != 2 or 1
int d=0;
for(;;){
system("CLS");
int o;
cout << "Would you like to try again?"<< endl;
cout << "1.YES!" << endl;
cout << "2.NO!" << endl;
cout << "Answer using 1 or 2!" << endl;
cin >> o;
switch(o){
case 1:
return d;
case 2:
d=1;
return d;
default:
cout << "Please use only numbers 1 and 2!" << endl;
Sleep(1000);
break;
}
}
}
int main()
{
for(;;)
{
system("CLS");
cout << "Press anykey to start the timer!" << endl;
system("PAUSE > NUL");
int x = time(0); // starting time
system("CLS");
cout << "Press anykey to stop the timer!" << endl;
system("PAUSE > NUL");
int y = time(0); // ending time
system("CLS");
int c = y-x; // the time that has passed
int a=c/60;
//minutes if has it will be 1 else 0. because int can only have whole numbers
int b= a/60; // hours using the same tecnig;D
if(a>0&&b==0){ // if minutes have passed
c=c-60*a; // this prevents the program out put being something like 1:99
cout << "time that has passed: 00:" << a << ":" << c << endl;
}
elseif(a==0&&b==0){ // if only seconds have passed
cout << "time that has passed: 00:00:" << c << endl;
}
elseif(b>0){
a=a-60*b; // prevents the out put being 1:78:99
c=c-60*a; // prevents the out put being 1:78:99
cout << "time that has passed:" << b << ":" << a << ":" << c << endl;
}
Sleep(1500);
system("CLS");
int e=A1();
if(e == 1)
{
break;
}
}
}
so what you think about the code is it bad or good?
did i do something wrong?
the reason why I'am asking this because someone disliked my code with out telling me a reason so i just want to know if he's a troll or if my code really sucks?
Your variable names are meaningless. b, a and c signifiy hours, minutes and seconds, so why not name them that way?
Other than that, the code doesn't really do much, so there isn't much to discuss. Try a larger project; usually, "bad code" will come back to haunt you anyway.
Good spacing, make sure you prototype your functions. When you write large programs with functions calling other functions, it can be a huge pain if the compiler doesn't recognize the functions.