Time

Pages: 12
I have a couple messages that if the user enters a wrong choice it says invalid choice then it returns a value to main. But the thing is I don't want to screen to disappear so fast. It would be nice if I had it say
Invalid selection returning to main menu. (for 5 seconds)

I was looking at the time functions in the references. Do people ever do things by time? Say there was a function called TIME it would be cool to write something like...
While(TIME < 5)
cout << Invalid sele......

Would be cool. Anyone ever do something like this?
Most people use std::cin.get(); to 'pause' the console so it won't close right away.
This function in the iostream standard library.
Last edited on
But you would have to use hit enter to make the program continue. I'm aware you can do that although I never have done it . I think you have to hit enter am I right ? Wouldn't it be cool to use the time though?
Check out the sleep function.
I can't find the reference on this site. I don't know these libraries.
http://msdn.microsoft.com/en-us/library/ms686298(VS.85).aspx

This function will wait for say 10 ms then proceed.

Edit: I found this example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(500);}

char x[6] = {'h','e','l','l','o','!'};

int main(){
    for (int i=0;i<6;i++)
    {
        cout << x[i];
        z();
    }
getch();
return 0;
}


Also I'm confused on what library I need. I read it was not in standard library. In this example I believe they used windows.h I tried this and sleep was undefined. Also what is .h I know it means header file what is that.
Thanks
Last edited on
@jlillie89

Use #include <windows> Then where you want a pause before continuing, add in Sleep(1000); 1000 gives 1000 ms or 1 second. So, adjust that number to how long of a delay you need. 1500, for 1.5 seconds, etc.
This is the only windows thing that came up in the box when you type #include <Windows.h>
This the same? I thought I had used it and it said sleep was undefined.

EDIT;
It is Sleep not sleep ahhha

Maybe I need sleep ..thanks to all.
Last edited on
Actually not so cool


1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <Windows.h>
using namespace std;

//Constants
const int SIZE = 10;


I get an error


1>------ Build started: Project: newnew, Configuration: Debug Win32 ------
1> newnew.cpp
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(9): error C2377: 'SIZE' : redefinition; typedef cannot be overloaded with any other symbol
1> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\windef.h(355) : see declaration of 'SIZE'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Weird.
As a general rule of thumb, only macro (ie.. #define ) names should be all uppercase. Since you are not making a macro, you shouldn't have it as all uppercase.

In this case, your SIZE is conflicting with some other thing named SIZE defined in windows.h.
Last edited on
Yeah I just fixed it. Hey what #define. And my teacher tells us to make constants capital like I have done. Please explain your comment.
It's a stylistic thing, but it's somewhat of an "unwritten rule" that all C/C++ programmers should follow.

One of the many problems with macros is that they completely disregard all language scoping rules, which makes them much more prone to name conflicts. The only way to avoid mishaps due to name conflicts is to reserve a certain naming convention for macros and don't use it for anything other than macros. It just so happens that most people tend to use all caps for macros, so that became the unofficial standard.



But really... in the end... just do what your teacher says. Another "unwritten rule" to programming (in any language) is to adopt the style of whatever environment you're working in. If you're class's style is to use all caps for constants, then that is what you should do.
Yeah I will. You know what pisses people off on here is if I post for help and I have void main()

I know use int main(). So whats the issue with void? I mean it works but people get upset.
It's not guaranteed to work. According to the language standard it shouldn't work.

The C++ standard states very clearly that main must return an int. Many compilers support the ability to use void main, but that's mainly to support old, pre-standard code.

The bottom line is that int main will always work, no matter which version of which compiler you're using. void main on the other hand may work just fine in one compiler but fail in another*. So really there is no benefit at all to using void main.

* this is mostly an academic point, as you'd be hard-pressed to find a compiler which doesn't support void main
you'd be hard-pressed to find a compiler which doesn't support void main

GCC is easy to find. Clang is getting popular, too.
Last edited on
> my teacher tells us to make constants capital like I have done.

Yes. It is a good (and widely followed) stylistic convention.

Do not use a name starting with a double underscore eg. __foo anywhere in your code.

Do not use a name starting with an underscore followed by an uppercase character eg. _Foo in the global unnamed namespace.

To avoid name clashes, use namespaces. FileEncryptionStatus in the global unnamed namespace would also cause a name clash with the Win32 API - as would thousands of other names.
GCC and Clang really don't allow it? I'm actually a bit surprised by that.
There's probably an option to make it compile, but by default "void main", it's a hard error in both (tested with 4.7.2 and 3.1 respectively)
Just tried it and:

test.c:
void main() {}

firedraco@ITSACOMPUTER ~
$ gcc test.c
test.c: In function `main':
test.c:1: warning: return type of 'main' is not `int'

firedraco@ITSACOMPUTER ~
$ g++ test.c
test.c:1: error: `main' must return `int'
Very cool. So there you have it, OP, it's not just academic. ;P
It works on mine. I have 2010 express. I don't know if that helps know what compiler I have also what is OP?

Pages: 12