How to pause the screen

Hi.I am doing C++ in dev-Cpp, and my screen doesn't pause, so I cannot see the result of my code.Here is my code:

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

int f(const char*s)
{
    static   int      n;
    
    if(!*s)
       return 0;
    else
    {
        n++;
        f(s++);
        return n;
    }
}
    
int main()
{
    int   a;
    
     cout << f("soheil")  << endl;    
     cin.ignore( numeric_limits<std::streamsize>::max(), '\n' );
     while(1)
        cin >> a;
     cin.get();
        
    return 0;
}

What's the problem?
Well, while(1) is an infinite loop.

Yes, but the problem is it doesn't pause at all, on my system.No infinite loop is created, although it was my intention.
I'd be happy if others test it too on their system
I ran your program in MS Visual C++. There was build problems. Anyway, here is what I think you want. I had to comment your program to get it to work.

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
// Pause_Solution.cpp
//

#include "stdafx.h"
#include <iostream>

using namespace std;

/*int f(const char*s)
{
    static   int      n;
    
    if(!*s)
       return 0;
    else
    {
        n++;
        f(s++);
        return n;
    }
}
  */  
int main()
{
/*
	int   a;
    
    cout << f("soheil")  << endl;    
    cin.ignore( numeric_limits<std::streamsize>::max(), '\n' );
    while(1)
    cin >> a;
    cin.get();
   */
    system ("PAUSE");
    return 0;
}
lol
LOL.What build errors you got?Tell me please.
Visual C++ is free from Microsoft. It would be best for you to download the file and get it set up on your computer. It is best we be on the same page when it comes to troubleshooting.

jim
Just use std::cin.get() to pause the console window.
@ OP:

http://cplusplus.com/forum/beginner/1988/

It almost looks like you've been to the above thread...

http://cplusplus.com/forum/lounge/79237/

http://www.cplusplus.com/articles/36vU7k9E/

@ bisbeejim:

http://www.cplusplus.com/forum/articles/11153/


(not related to the above link):
What build errors you got?


You don't tell what errors you get...

When I compile the above (code), I get errors as well:

alphabet.cpp: In function 'int main()':
alphabet.cpp:44: error: 'numeric_limits' was not declared in this scope
alphabet.cpp:44: error: expected primary-expression before '>' token
alphabet.cpp:44: error: no matching function for call to 'max()'


This is pretty simple, if you go to the first thread (which I assume you have, because it seems like line 29 was a copy and paste), you will see that you have to #include <limits> You also don't need to use std:: on anything because you are using namespace std; already.

After including limits, I got it to compile fine, but it won't run, pops up with an error about how the program has stopped working.

I won't get into that because I have class in a couple of minutes, maybe one of the more experienced members can properly explain what you are doing wrong and guide you in in the right direction to fixing your program.
Last edited on
What's the problem?
Stack overflow. You've got an infinite recursion there.
f(s++); uses the old value of `s', then increments it.

Edit: the methods for `pause' the screen will fail, as your program crashes.
You need to execute your console programs from the console.
Last edited on
hmm...
but
f(++S);
produces no error.interesting!
Thanks!got my answer!
Last edited on
The compiler can see that f(s++); can be converted to tail-recursion.
@OP: you are depending too much of global variables. Your function can be used just one time.

@Duoas: ¿your point? that may only change the `crash' to an infinite loop
Last edited on
The point: Put your punches where they belong.
Thanks all, esp. ne555 that solved the problem.'tail recursion' or similar things seem to be too much of me!
Topic archived. No new replies allowed.