The Worst Clock Known To Man!

This is a discussion on The Worst Clock Known To Man! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <fstream> #include <iostream> #include <cstring> using namespace std; main(){ int hour; int minute; int second; cout<<"Please ...

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    Essex, UK
    Posts
    6

    Smile The Worst Clock Known To Man!

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    main(){
    int hour;
    int minute;
    int second;
    
    cout<<"Please enter the current hour...\n";
    cin >> hour;
    cin.get();
    cout<<"Thanks, now please enter the minute\n";
    cin>>minute;
    cin.get();
    cout<<"Setting time...\n";
    cin.ignore();
    second = + 1;
    if (second > 59) {
    minute + 1;}
    if (minute > 59) {
    hour + 1;}
    if (hour > 24) {
    hour = 0;
    minute = 0;
    second = 0;
    }
    if (hour > 0 && minute > 0)
    {cout<<"The time is "<<hour<<":"<<minute<<":"<<second<<" tick tock tick tock!\n";
    }
    }
    Behold my new quick 5 minute problem today! Okay so I figured this might actually work unfortunatley I was wrong once again. Clearly I have not tried to create a properly functioning clock however I was hoping I could set a time and it would display it and also count upwards. I figured I would rather share this with you guys and ask for advice on how to make this better and actually get it to function how I originally wanted rather than sitting here smashing my face through my desk. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    16
    You are never reseting second so it will never get past 1 minute...

    After 59 seconds minute + 1? Nothing is setting equal. Look over your code its all simple mistakes. Also you do know about the build in clock function right? Your just making this for fun?

    PS: This will not work because it will be executing at w/e your processor is. So if your running at 2GHz and lets say 1 instruction takes 1 cycle... well 1/2GHz = 1 second in your program.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    So this is the worst clock known to man, but no woman? So you are saying that there is a worse clock out there that only women knows about? Hmmm.
    First things first. You need to indent properly. Then you must make sure main returns int (you haven't specified a return type which is illegal).
    And a counting clock may not be a very good program. The problem is that you cannot guarantee that it will go up by one exactly every second. You have to deal with some error margin, which might not be as intuitive to a newbie.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elysia View Post
    You need to indent properly. Then you must make sure main returns int (you haven't specified a return type which is illegal).
    And a counting clock may not be a very good program. The problem is that you cannot guarantee that it will go up by one exactly every second. You have to deal with some error margin, which might not be as intuitive to a newbie.
    I think there are far worse things wrong with this code other than just imprecision, no?

    Code:
    #include <stdio.h> Use <cstdio>, this one is deprecated.
    #include <fstream>You don't use anything from this header in the code, get rid of it.
    #include <iostream>
    #include <cstring>You don't use anything from this header in the code, get rid of it.
    
    using namespace std;
    
    main(){ As Elysia pointed out this is wrong, make it "int main()"
    int hour;
    int minute;
    int second;
    
    cout<<"Please enter the current hour...\n";
    cin >> hour;
    cin.get();
    cout<<"Thanks, now please enter the minute\n";
    cin>>minute;
    cin.get();
    cout<<"Setting time...\n";
    cin.ignore();
    
    second = + 1; What is this line supposed to do? Either you need to remove the plus, or make the "=+" a "+=" instead, but since the variable is not initialized this would be undefined behavior
    
    if (second > 59) {
    minute + 1;}
    if (minute > 59) {
    hour + 1;}
    if (hour > 24) {
    hour = 0;
    minute = 0;
    second = 0;
    }
    if (hour > 0 && minute > 0)
    {cout<<"The time is "<<hour<<":"<<minute<<":"<<second<<" tick tock tick tock!\n";
    }
    }
    
    The lines in red don't do anything, you calculate a value but you never store i anywhere, either make "minute +1" into "minute++" or "minute +=1"
    Most of these you would have caught if you turned on compiler warnings :-)
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Neo1 View Post
    I think there are far worse things wrong with this code other than just imprecision, no?
    There undoubtedly is, but others pointed out the deficiencies in the code already. So I was merely pointing out that a ticking clock might not be as easy to make as one might think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Isn't
    Code:
    main()
    {
    }
    standard? I remember reading somewhere that main() implies int main() and that no return implies return 0;

    I'm likely wrong but I just had to ask.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    In C90, it's legal, but deprecated. In C99 and C++, it's illegal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Essex, UK
    Posts
    6
    Sorry, I am pretty new to all this I'm just playing around with it at the moment doing some trial and error, thank you very much for the help. I didn't mean anything by the 'Known to man' I thought that expression referred to mankind rather than a gender reference, if you're offended by that I apologise once more. I'm not sure what how to improve my own ability without asking these questions on the forum, I've read the book of C, watched loads of vids and read all the beginner tutorials on the main part of the site - I realise I'm not going to write a brilliant code straight away and the notes you have given me in this thread have really helped. Thanks.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Netalt View Post
    Sorry, I am pretty new to all this I'm just playing around with it at the moment doing some trial and error, thank you very much for the help. I didn't mean anything by the 'Known to man' I thought that expression referred to mankind rather than a gender reference, if you're offended by that I apologise once more.
    I'm kidding with you!
    And if you want mankind, then I say, what about womankind?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Cool Womankind

    Quote Originally Posted by Elysia
    And if you want mankind, then I say, what about womankind?
    What about it?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    That was exactly my question. It's the worst clock known to mankind. Does that mean it's not the worst clock known to womankind?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 05:15 PM
  2. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. we of the cage
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-21-2002, 10:14 AM
  5. Gender Humour Thread
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 06-01-2002, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21