End program by pressing enter key?

I'm trying to end my program when the user presses the enter/return key.

I'm trying to do this with a break statement.

I have two different situations, one when it is a string, and the other one when it is a char.

1
2
3
4
5
6
7
8
9
	for (int i = 0; i < 80; ++i)
	{
		string s;
		cin >> s;
		if (s == "\n")
			break;

		S.PushStack(s);
	}


I'm doing the same thing for char just replacing string with char.

I'm not sure what I am doing wrong but pressing enter does not end the program. Any help?

Thank you
Last edited on
formatted input via the extraction operator skips all white space by default including newlines.

1
2
3
4
5
6
7
8
9
    for ( unsigned i=0; i<80; ++i )
    {
        string s;
        getline(cin, s) ;
        if ( s.length() == 0 )
            break ;
    
        S.PushStack(s);
    }
I see what you did there but what if someone wants to end it by pressing enter just ones?

For example, a user enters: MON TUE WED (enter)

this works because of the space but this:

MON TUE WED(enter)

requires another enter for it to work. How can it work by pressing enter right after WED?

Also, when dealing with char, how will that work?

1
2
3
4
5
6
7
8
9
10
11
	for (int i = 0; i < 80; ++i)
	{
		char c;
		
		cin >> c;

		if (c == '\n')
			break;

		C.PushStack(c);
	}

EDIT:
I figured out the char one but for string, I cannot use getline since that takes in spaces as input and I don't want it to.

I'm doing stack, so I have to output it in reverse.

Any more ideas?
Last edited on
Either read in a complete line, and use s string stream to extract white-space-delimited strings from it
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
#include <iostream>
#include <string>
#include <stack>
#include <sstream>

int main()
{
    std::stack<std::string> stk ;

    std::string line ;
    std::getline( std::cin, line ) ;

    {
        std::istringstream stm(line) ;
        std::string word ;
        while( stm >> word ) stk.push(word) ;
    }

    while( !stk.empty() )
    {
        std::cout << stk.top() << ' ' ;
        stk.pop() ;
    }
    std::cout << '\n' ;
}


Or read character by character, composing the white-space-delimited strings yourself, till a newline is encountered
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
#include <iostream>
#include <string>
#include <stack>

int main()
{
    std::stack<std::string> stk ;

    std::string current_str ;
    char c ;

    while( std::cin.get(c) ) // read char by char
    {
        if( std::isspace(c) ) // if it is a whitespace
        {
            if( !current_str.empty() ) stk.push(current_str) ; // end of current string, push it
            current_str.clear() ; // start afresh with an empty new string

            if( c == '\n' ) break ; // newline, come out of the loop
        }

        else current_str += c ; // not whitespace, append to current string
    }
    
    // if current string is not empty, push it (eof on std::cin)
    if( !current_str.empty() ) stk.push(current_str) ; 

    while( !stk.empty() )
    {
        std::cout << stk.top() << ' ' ;
        stk.pop() ;
    }
    std::cout << '\n' ;
}


Topic archived. No new replies allowed.