Thread: Help with sentinel control using EOF . . .

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Help with sentinel control using EOF . . .

    Code:
        for(int i=0; i<=3; i++){   
                gets(&text[i][0]);
                }
                for(int i=0; i<=3; i++){  
                        for(int j=0; text[i][j]!='\0'; j++){
                                text[i][j] = tolower(text[i][j]);
                                }
                                }
                        for(int i=0; i<=3; i++){ 
                                searchPtr = &text[i][0];
                                while ( searchPtr = strstr( searchPtr, search ) ) {
                                count++;
                                searchPtr++;
                                }
    }
    can u help me to put a sentinel control on this code using EOF? plz help

    the output should be like this

    Enter a search string : th

    Enter Lines of text :

    //even if i enter many lines of text(shoud be limitless), but if i press ctrl + z it should stop loop and it should compute for occurences

    plz i need help for this
    Last edited by [Student]; 08-19-2011 at 09:06 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can have a sentinel value, or you can read until EOF; you can't do both.

    First, you mistyped "getline" as "gets", so you'll want to fix that.

    Second, if you want to read until EOF, then you use the result value of getline as your loop control rather than looping from 0 to 3 (which, perhaps inconveniently, is four lines of text, not three). I don't see any reason to store the old lines of text, so unless you have one you shouldn't bother -- just make a string and read into the same string each time.

  3. #3
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    I mean that i must enter EOF to end the input . . . .

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So then there you go. Fix "gets" to "getline" and make that your loop control:
    Code:
    while (cin.getline(<string>)) {
        <stuff>
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    You can have a sentinel value, or you can read until EOF; you can't do both.
    EOF is a sentinel value, assuming use of a method of reading that returns EOF when it reaches end of file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You could use getline, by keep in mind by default it will just grab 1 line of text, e.g. each input is delimited by the newline character. If you want to input multiple lines and preserve the formatting one option is just to use a loop and input char by char into a std::string. (Note: on windows in order to signal EOF you need to do <enter>ctrl^z<enter>)

    Now being C++, you can drop all that C crap and do this using std::strings and the members of the string object to find and count your substring. This should get you going in the right direction:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    int main(void){
    	
    	std::string input;
    	int charInput;
    
    	while((charInput=std::getchar())!=EOF)
    		input+=charInput;
    	
    	std::cout <<"\nYou entered: \n\t"<<input;
    	
    	std::transform(input.begin(),input.end(),input.begin(),::tolower);
    	std::cout <<"\nIn lower case: \n\t"<<input;
    
    	/* Use the functions provided by the std::string object to find your
    	substring and count your substring*/
    
    	std::cin.get();
    	return(0);	
    }
    Now for a real C++ solution, you would drop the loop all together and just use the stream iterators to grab your input:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <istream>
    #include <string>
    
    int main(void){
    
    	std::cin >> std::noskipws;
    
    	std::istream_iterator<char>start(std::cin);
    	std::istream_iterator<char>end;
    	std::string input(start,end);
    
    	std::cout<<"You entered:\n\n"<<input;
    
    	std::transform(input.begin(),input.end(),input.begin(),::tolower);
    
    	std::cout<<"\nLower case:\n\n"<<input;
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with sentinel value
    By saahmed in forum C++ Programming
    Replies: 8
    Last Post: 02-15-2006, 06:22 PM
  2. SENTINEL question
    By codeHer1 in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 09:34 AM
  3. Sentinel Loop?
    By TerryBogard in forum C Programming
    Replies: 1
    Last Post: 12-17-2002, 01:11 AM
  4. sentinel
    By oscuro in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 05:48 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM