fixing the sequence of display without rearranging the defined string

hello. when i run this program the sequence of the message was reverse.can you help where did i go wrong??


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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "iostream"[
#include "time.h"
#include "windows.h"
#include "conio.h"
#include "string"

using namespace std;


void GotoXY( HANDLE StdOut, SHORT x, SHORT y )
{
    
    COORD Cord;
    Cord.X = x;
    Cord.Y = y;
    SetConsoleCursorPosition( StdOut, Cord );
}

string ReadTime()
{
    
    time_t tim=time(NULL);
    char *s=ctime(&tim);

	return s;
}
 
void setCurPos(int column, int row)
{
	int col = column, rowl = row;


    HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );  

 
    GotoXY( hStdout, col, rowl );
}

void main(int argc, char* argv[])
{

	string timeNow, prevTime;
//defined string:
	string theString = "                       Computer Engineering                       ";

	int stringLength = theString.length();
	int startChar = stringLength;
	int maxDelay = 15000;
	int delay = maxDelay;
	prevTime = ReadTime();

    while( kbhit() == 0 )
    {
        
        timeNow = ReadTime(); 
		if(timeNow!=prevTime)
		{
		prevTime = timeNow;

		
		setCurPos(25, 12); cout<<timeNow; setCurPos(79,24);
		}
		
		if(delay == 0)
		{
			delay = maxDelay;
			int charPointer = startChar;
			for (int x = 25; x<=48; x++) 
			{
				setCurPos(x, 14); cout<<theString[charPointer];
				charPointer--;
			}
			startChar -= 1;
			if(startChar<=20) startChar = stringLength;
		}
			
		delay--;

    }
}
the output goes like this:

Wed Oct 15 18:25:17 2014
     gnireenignE retupmoC

Last edited on
Did you write this code yourself?
(Because if you did, you ought to know why you are getting it in reverse.)

Take a look a the variables "startChar" and "charPointer", lines 47, 67, 70 and 71. Do you see why it is going in reverse?

Also, there's an off-by-one error there. Line 67 assigns the "charPointer" an index that is one-past the last character in the string.


As a final matter of variable name choice, "charPointer" is a terrible name in C++, because it isn't a pointer, it is an index. Hence, a better name would be "charIndex". (An even better name would be something like "currentIndex"...)



Hints:
To create a scrolling marquee, you only need to set the cursor position once per delay. Set it to the beginning of the line.

Next, the thing that should change is the index of the first (or 'start') index into the string.
If you want the text to scroll left, increase the index each delay.
If you want the text to scroll right, decrease the index each delay.

Don't forget about what happens when the index hits either end of the string: if (index < 0) then you must set it to the index of the last character; if (index >= thString.length()) then you must set it to the index of the first character.

Likewise, don't forget that your loop into the string itself (line 68) should be:
1) controlled by the length of the string, not hard-coded
2) modulo the length of the string (use the remainder operator, %).

Hope this helps.
Last edited on
Topic archived. No new replies allowed.