-2

I'm trying to make my C++ program exit.

I know I can pause input with while cin >> s, but I don't know what to do to make the entire program exit.

This is my code:

int main()
{
    long int l;
    long int i;

    char s[100000];

    while (cin >> s)
    {
        l = strlen(s);//strlen Returns the length of the C string str. 


        for (i = 0; i<l; i++)
        {
            switch (s[i])
            {
            case 'W':
                cout << "Q";    break;
            case 'E':
                cout << "W";    break;
            case 'R':
                cout << "E";    break;
            default:
                cout << ";";    break;
            }

        }
        cout << (" ");
    }

    system("pause");

    return 0;
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335

3 Answers3

3

Your program will terminate when it runs out of input.

The system("pause"); seems to imply that you're using Microsoft Windows. To signal an end-of-file condition for keyboard input on Windows, type Ctrl-Z. (For Linux and other Unix-like systems, use Ctrl-D at the beginning of a line.)

Incidentally, the program you posted is complete and will not compile. That can be corrected by adding the following lines to the top:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

When posting a question, please include the entire program.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

I was able to get the program to exit by using the command exit(0); within the while loop.

Here is my finished program:

include iostream
include string
using namespace std;

int main(){
    long int l;
    long int i;

    char s[100000];

    cout << "\n Write your code in UpperCase, " 
    cout << "to close the program switch to LowerCase \n" << endl;

    while (cin >> s)
    {
        l = strlen(s);//strlen Returns the length of the C string str. 

        for (i = 0; i<l; i++)
        {
            switch (s[i])
            {
            case 'W':
                cout << "Q";    break;
            case 'E':
                cout << "W";    break;
            case 'R':
                cout << "E";    break;
            default:
                exit(0);   break;
            }
        }
        cout << (" ");
    }
    system("pause");
    return 0;
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
-1

Your loop is waiting until cin >> s returns an "end of file" (EOF). You can accomplish this on Windows by typing Ctrl+Z or on Unix-like systems such as Mac and Linux Ctrl+D.

You could also put a break character in your loop, or change the condition altogether.

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • 2
    No, `cin >> s` isn't always going to be true. Checking for an end-of-file condition on standard input is an extremely common thing to do, and it's exactly what this program does. The condition on the `while` loop is perfectly correct as it is. – Keith Thompson May 18 '15 at 23:59
  • I realize that. I accounted for this in my answer. However, judging by the asker's original post, I'd say she is not aware of this. I recommended adding a break for a more common key or changing the condition altogether because it seems more logical for what she is trying to accomplish. – PC Luddite May 19 '15 at 00:17
  • 1
    The code is clearly intended to terminate when it sees an end-of-file condition on standard input. The OP doesn't know how to make that happen. I suggest that telling the OP about Ctrl-Z (which is not some obcure technicality as you suggest) is a better solution than changing the code. It also makes it possible to redirect the program's input from a file or pipe. – Keith Thompson May 19 '15 at 00:33
  • Fine. I guess you win. – PC Luddite May 19 '15 at 00:36
  • 1
    I'm not trying to "win", just answering the OP's question. – Keith Thompson May 19 '15 at 00:46