'cin' troubles

I'm having troubles with a cin statement in my code and need some guidance. I need my code to refresh an array with completely different ASCII characters, then have the user enter a keystroke and have the refresh speed increase by a few hundred miliseconds every time. Now my C++ teacher gave me a code to use (using the windows.h library) but i have no idea how to apply it or if theres an easier way.
1
2
3
4
5
void SetCoord(int x, int y)
{
    COORD thing = (x,y);
    SetConsoleCursorPosition (GetStdHandle ( STD_OUTPUT_HANDLE), thing);
}


that's the code he gave me but i don't know how to apply it to my code. (will post below)
any help would be great! if you have any other questions let me know! this is only the portion of the game that this involves.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void func2()
{
cout << "----------------------------------------------------------------------------" << endl;
            cout << "|  You have chosen the Moving Menace game.                                 |" << endl;
            cout << "|  You will be looking for a specific character in this game.              |" << endl;
            cout << "|  When you see the character, enter the key 'G'.                          |" << endl;
            cout << "|  Just as a warning the more you find the character, the faster it moves. |" << endl;
            cout << "|  Are you ready?                                                          |" << endl;
            cout << "----------------------------------------------------------------------------" << endl;
            Sleep(10000);
            //It's over 9000!
            cout << "3" << endl;
            Sleep(1000);
            cout << "2" << endl;;
            Sleep(1000);
            cout << "1" << endl;
            Sleep(1000);
            system("cls");
            cout << "GO!";
            Sleep(1000);
            system("cls");

             // initialize the array
            while(1)
            {

                cout << "Look for this character: '*' \n" << endl;
                char cTest;
                iSize = 16;
                for (int x = 0; x < iSize; x++)
                {
                    for (int y = 0; y < iSize; y++)
                    {
                        cTest = char(rand() % 223 + 33);
                        cArray[x][y] = cTest;
                    }
                }

                // display everything in array
                for (int x = 0; x < iSize; x++)
                {
                    for (int y = 0; y < iSize; y++)
                    {
                        cTest = cArray[x][y];
                        cout<< cTest;
                    }
                    cout << endl;
                }
/*my problems start here! well maybe earlier...*/
                Sleep(1500);
                system("cls");

            }


}
What 9000?
(I'm sorry, I'm sorry. I could help myself, I know this is a serious forum.)
That function will place the cursor at the (x, y) you specify, where x = column and y = row and (0, 0) is the upper corner of the screen. This way you can avoid using the (very slow) system("cls");

The Sleep() function takes an unsigned integer argument, so you just need a variable to hold how long to sleep. Each time through the loop you can reduce its value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unsigned timeout = 1500;  // 1.5 seconds

...

while (1)
{
    SetCoord( 0, 0 );
    // draw stuff here
    ...

    if (timeout)
    {
        Sleep(timeout);
        if (timeout > 200) timeout -= 200;
        else timeout = 0;
    }
}

BTW, the comment you have on line 23 is not correct...

Hope this helps.
i know about line 23 i just haven't changed it yet. and this is wonderful! it solves a problem i was going to have to solve later in the code but im still stuck on running 2 processes at the same time :/ like i said i need the array to refresh at the same speed UNTIL the user enters his keystroke then speeds up..if thats even possible..
Ah, then you'll want to use the WaitForSingleObject() function for the first part.
This here will help you get started:
http://www.cplusplus.com/forum/beginner/5619/#msg25047

Once you get that first ENTER (and read it) you can either use the Sleep() function as you have or continue using WaitForSingleObject() so that the user can press a key at any time to stop everything.

Good luck!
closed account (zwA4jE8b)
hey duoas, I tried your "http://www.cplusplus.com/forum/beginner/5619/#msg25047" code, and nothing really happend. Is it supposed to print "." while it waits for input?
What do you mean, "nothing really happend"?
Did it not print "."s every half second while waiting for you to begin typing?
If not, tell me more about your compiler and OS.
closed account (zwA4jE8b)
no, it did not print '.'s... I am using VS 2010 and running win 7 x64.

I copy and pasted the code exactly.

I just get "Please enter your name." followed by a flashing underscore in my command window.

I can then enter the name. and the program continues. but no dots.
I'm really not sure what the problem is.
Please don't take this the wrong way, but more often than not when a person says something like "I did it exactly as you did" he missed something anyway.

Are you sure you started with a Windows console application and linked with the correct libraries, that you haven't messed with the iostream::sync_with_stdio() or other properties, that you are compiling for the proper platform, that you are using a standard Windows console to run the application (and not, even, running it from the IDE), that you don't have any extraneous semicolons in your code, etc?

I cannot get VS to install on my PC (apparently I'm one of the few for which .NET will not install or work properly), so I cannot test with modern versions.

Maybe someone else here knows what may have gone wrong?
closed account (zwA4jE8b)
I ran it from an open console and it works!
Last edited on
Yay!

The IDE does have nice features, but it is not uncommon for the "run from the IDE" functions to modify your process in unexpected ways with various hooks...
Topic archived. No new replies allowed.