Help with sentinel control using EOF . . .
This is a discussion on Help with sentinel control using EOF . . . within the C++ Programming forums, part of the General Programming Boards category; 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]!='
'; j++){
text[i][j] = tolower(text[i][j]);
}
}
...
-
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.
-
and the Hat of Guessing
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.
-
I mean that i must enter EOF to end the input . . . .
-
and the Hat of Guessing
So then there you go. Fix "gets" to "getline" and make that your loop control:
Code:
while (cin.getline(<string>)) {
<stuff>
}
-

Originally Posted by
tabstop
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%.
-
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);
}

Originally Posted by
anduril462
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....

Originally Posted by
quzah
..... 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
Similar Threads
-
By saahmed in forum C++ Programming
Replies: 8
Last Post: 02-15-2006, 05:22 PM
-
By codeHer1 in forum C Programming
Replies: 2
Last Post: 03-24-2005, 08:34 AM
-
By TerryBogard in forum C Programming
Replies: 1
Last Post: 12-17-2002, 12:11 AM
-
By oscuro in forum C++ Programming
Replies: 1
Last Post: 03-12-2002, 04:48 PM
-
By Unregistered in forum C++ Programming
Replies: 1
Last Post: 11-11-2001, 10:50 PM