| | |
Comparisons
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
I have to read in values from a file and output only the ones that past the test. However, I'm not sure how to compare a few of them.
I need to skip the word if it ends with '\n', '\t', or a space. So, how exactly would you handle and test for this. I have the rest down path, just these three are giving me issues. He didn't tell us what the input file would look like, so I'm assuming it's one line has one word on it, so that's what I coded for. But then that leads to the problem that all the lines end with the \n char, hence my dilemma.
Any suggestions of what to do?
This code works so far, but it doesn't do anything if it ends in the last three bad[] chars.
I need to skip the word if it ends with '\n', '\t', or a space. So, how exactly would you handle and test for this. I have the rest down path, just these three are giving me issues. He didn't tell us what the input file would look like, so I'm assuming it's one line has one word on it, so that's what I coded for. But then that leads to the problem that all the lines end with the \n char, hence my dilemma.
Any suggestions of what to do?
This code works so far, but it doesn't do anything if it ends in the last three bad[] chars.
char bad[20] = {'{', '[', '(', '"', '‘', ',', '.', '?', ';', ':', '%', '@', '-', '}', ']', ')', '`', ' ', '\n', '\t'};
main ()
{ BST A;
string n;
int max = 0, x = 0;
ifstream In;
getFile (In);
while (getline(In, n))
{ cout << n << endl;
if (check(n, x) == true)
{ max = (max>=x) ? max : x;
for (int i = 0; i < n.length(); i++)
{ n[i] = tolower(n[i]); }
A.Insert(n); } }
cout << setw(max) << "Word" << "\tOccurance\n";
A.Print(max);
system("pause");
return 0;
}
bool check (string word, int& size)
{ size = word.length();
string n;
for (int i = 0; i < 17; i++)
{ if (word[size] == bad[i]) return false;
n = word[size-2] + word[size-1];
if (n == bad2[0] || n == bad2[1] || n == bad2[2]) return false; }
for (int x = 0; x < 5; x++)
{ if (word[0] == bad[x]) return false; }
return true;
} Every line in a text file ends with '\n'. According to your instructions you would have to skip the last word of every line that is read. Lines do not have embedded '\n' characters.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
Yeah, so I'm not entirely sure how to handle the coding for this. I mean, I'm basically done, I just don't know what to use as a test. If the input file looks like this
Then basically all the words should drop out cause they all either end with a space or a newline character, even though they aren't on the word technically. But, if you think of the space as a separator, then there's the issue that how would you determine if a character ends with a space. Then there is also the factor of how do you determine if it ends with a \t or \n character as well. The only way I can figure is that it's hard coded in, like
But, if that's the case, it seems a little he would have told us that or something. I just don't know which one to code for.
C++ Syntax (Toggle Plain Text)
one two three you're hi these they're abc.com one
C++ Syntax (Toggle Plain Text)
word\n word2\t word3 // this one ends in a space
![]() |
Similar Threads
- vector (1xm array) comparisons (Computer Science)
- string array, if statement, and comparisons (C++)
- PHP substring comparisons...what kinds are there? (PHP)
- Build-Heap number of comparisons (Computer Science)
- Heap of 8 with 8 comparisons (Computer Science)
- counting comparisons when sorting (C++)
- video card (Monitors, Displays and Video Cards)
- Monitor or Video Card problem?? (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: Char array and Getline question - really basic
- Next Thread: C++ Segement Fault on Unix
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






