Problem with carriege return '\r'

Hello, I have very strange problem with printing data in console using '\r'. I would like to show current time of a football match in my program and i would like to do it in one line(new value erase old one) and this is the most logical use of '\r'. After compiling without any errors program runs, but there is no expected effect.

I'm using GCC 4.3.3-1(latest for my distro).

Here's code. Most of variables are in Polish, but I hope it won't be a problem

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
#include <iostream>
using namespace std;

void daley(int arg){
     int x = 0;
     for(int i = 0; i<arg*100000;i++){
	  x++;
     }
}

int main(){
     int czas_meczu = 90,dodany_czas;
     for(int i = 1; i<= czas_meczu; i++){
	  cout << "\r";
	  cout << "\t" << i << "\t";
	  if(i == 89){
	       cout << "Podaj czas przedluzenia meczu: ";
	       cin >> dodany_czas;
	       czas_meczu = czas_meczu + dodany_czas;
	  }
	  daley(75);
	  
     }


}


I also found in one C++ learning book example that shows how '\r' works, buit it also doesn't work.

I hope that, anyone knows the answer. :)

Pozdrawiam! Greetings!
Last edited on
I don't think the problem is with the '\r' (but I could be wrong!). I think it likely that your delay function is just way too fast.
Here's a quick replacement (works on both Win32 and POSIX systems)
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
#include <iostream>
using namespace std;

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__)

  #ifndef __WIN32__
    #define __WIN32__
  #endif

  #include <windows.h>

  inline void daley( unsigned long msec )
    {
    Sleep( msec );
    }

#else  // presume POSIX (unix)

  #include <unistd.h>

  inline void daley( unsigned long msec )
    {
    usleep( msec * 1000 );
    }

#endif

int main(){
     int czas_meczu = 90,dodany_czas;
     for(int i = 1; i<= czas_meczu; i++){
	  cout << "\r";
	  cout << "\t" << i << "\t";
	  if(i == 89){
	       cout << "Podaj czas przedluzenia meczu: ";
	       cin >> dodany_czas;
	       czas_meczu = czas_meczu + dodany_czas;
	  }
	  daley(500);  // 1/2 sekunda
	  
     }

return 0;
}

Hope this helps.
Hello, thank you for your answer. I tried your suggestion, but it still doesn't work. I also tried wait function that uses <ctime> librery form this site.

My code after changes:
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
#include <iostream>
using namespace std;
//#include <stdio.h>
#include <time.h>
#include <sys/select.h>

//NULL jest zdefiniowany w ktorejs z bibliotek

int kbhit(void);
void wait(int seconds);	


int main(){
     int czas_meczu = 90,dodany_czas;
     char bufor[2];
   
     for(int i = 1; i<= czas_meczu; i++){
	  cout << "\t" << i << "\t";
	  
	  if(i == 89){
	       cout << "Podaj czas przedluzenia meczu: ";
	       cin >> dodany_czas;
	       czas_meczu = czas_meczu + dodany_czas;
	  }
	  wait(2);

	  if(kbhit()){
	       cout << "Tu będzie jakieś zdarzenie :D" << endl;
	       cin.ignore(1);
	  }
	  cout << "\n";
     }
     cout << endl;
}

//************************************************************************************************
int kbhit(void){
     struct timeval tv;
     fd_set read_fd;

     tv.tv_sec=0;
     tv.tv_usec=0;
     FD_ZERO(&read_fd);
     FD_SET(0,&read_fd);

     if(select(1, &read_fd, NULL, NULL, &tv) == -1)
	  return 0;

     if(FD_ISSET(0,&read_fd))
	  return 1;

     return 0;
}


void wait(int seconds){
     clock_t endwait;
     endwait = clock() + seconds * CLOCKS_PER_SEC ;
     while (clock() < endwait){
     }
}


Any ideas?
Last edited on
Topic archived. No new replies allowed.