How can I use eof and carriage return together?

Hi everyone
I'm going to write a program that takes string until end of file(eof). An condition must be considered and that is it must also terminate by a new line. For example when it's prompting me to enter a string if I press enter it must terminate and exit the program. How is it possible?
I tried saving carriage return("\t\n") as a string then I compared it with the entered string but it didn't work.
Any idea will be appreciated!
Hello, carriage return is \r (and \r\n is a windows new line with carriage return)
Try escaping the string to compare, use \\r\\n) it means \r\n as a string and not as a arriage return
Last edited on
I guess your idea works for me but I didn't get it correctly. Do you mean I have to compare the input string to \\r\\n? Here is piece of my code:

while(getline(cin, seq, '\n'))
{
if(seq=="\\r\\n")
return 0;
it doesn't work.
By the way I'm having a new problem!! I get out of range error for this part:

void Is_Same(unsigned long x, unsigned long y, unsigned long index)
{
char prev=seq[x];
x++;
while(x<=y)
{
if(prev=!seq[x])
{
Same[index]=false;
break;
}
prev=seq[x];
x++;
}
}

the seq is the input string containing 0's and 1's. It can be up to 1,000,000 long. x and y are ranges. I should compare digits in this range. If all of them are the same I output Yes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

// return  true if all characters ('0' or '1') in the range [x,y] are the same
bool is_same( const std::string& str, unsigned long x, unsigned long y )
{
    if( x > y || str.size() < (y-1) ) return false ;
    return ( str[x] == '0' || str[x] == '1' ) &&
             str.find_first_not_of( str[x], x ) > y ;
}

int main ()
{
    std::string str ;
    std::cout << "enter a string and press RETURN\n" ;
    std::getline( std::cin, str ) ; // 10110111111111111000111000001110001110001
    std::cout << "string: " << str << '\n' ;
    std::size_t x = 5 ;
    std::size_t y = 15 ;
    if( is_same( str, x, y ) ) std::cout << "Yes\n" ;
}

http://ideone.com/tHQ0ii
Thank you JLBorges that really helped. ;)
Topic archived. No new replies allowed.