Hello everybody!
I'm trying to count the number of occurrences of each word in a text file. But program put in the file the first symbol of inputed word only (line 34) and don't enter in the for-statement (line 51). What should I search for in my code in order to find the problem?
Thanks in advance.
First symbol of str will never be newline. Just because you cannot enter any whitespace character using formatted input.
Your program should enter infinite loop and you will have to terminate it. In that case complete flushing of out buffer and writing file to disc is not guaranteed.
Errors were shown:
[C++ Error] Count.cpp(36): E2015 Ambiguity between '_STL::basic_istream<char,_STL::char_traits<char> >::operator >>(bool &)' and '_STL::basic_istream<char,_STL::char_traits<char> >::operator >>(void * &)'
[C++ Error] Count.cpp(38): E2094 'operator!=' not implemented in type 'string' for arguments of type 'char'
When I edited code by the following:
1 2 3
int main ()
{
string str;
Errors were shown:
[C++ Error] Count.cpp(38): E2093 'operator*' not implemented in type 'string' for arguments of the same type
Is any way exist to correct it?
#include <iostream>
#include <fstream>
#include <string>
#include <set>
int main()
{
std::string input; //We'll get the input with this variable
while(input.empty()) //While it's empty (with std::getline, while it's a '\n'
{
std::cout << "Input a valid phrase: "; //Show request
std::getline(std::cin, input); //Get the input from std::cin (user)
std::cout << std::endl; //Newline
}
std::set<char> input_chars(input.begin(), input.end());
std::ofstream words_output(((input + "_phrase") + ".dat").c_str()); //Example: input = "IM HAPPY". file = "IM HAPPY_phrase.dat"
words_output << input << "\n\n";
for(auto letter : input_chars) //for each letter in the set
{
words_output << letter << " found in:\n";
for(auto current_letter = input.begin(); current_letter != input.end(); current_letter++) //for each letter in the input; here we start a linear search
{
if(*current_letter == letter)
{
words_output << std::distance(input.begin(), current_letter) + 1;
//std::distance for calculating where the char was found. +1 is "human" readability.
words_output << "\n";
}
}
words_output << "\n";
}
return 0;
}
User input like this is a little odd, there's no getting around it. A do-while is a little 'chunky', because you need to check that the string is not "BL" twice:
1 2 3 4 5 6 7 8 9 10
string str;
do
{
cout << "Enter a word, BL to quit: ";
cin >> str;
if ( str != "BL" )
// add str to map
} while ( str != "BL" )
I would do a plain while, preferring to double up on console output:
1 2 3 4 5 6 7 8
string str;
cout << "Enter a word, BL to quit: ";
while ( ( cin >> str ) && str != "BL" )
{
// add str to map
cout << "Enter a word, BL to quit: ";
}
Thank you so much. This is working code (If anyone know how to input empty string instead of "BL" (blank line) - a sign of completion of input is null string).
Thank you so much, I've realized it (watch line 35 { out << str << " ";).
Could you explain how to make as a sign of completion of input is null string?
When while ( ( cin >> str ) && str != "\n" ) "\n" is not working.
And when while ( ( cin >> str ) && str != "\n" ) Builder reports that "!=" not implemented in type 'string' for arguments of type 'char'.
'\n' is a newline symbol. It cannot appears inside string when using formatted input (as it counts as a whitespace symbol)
It is not possible (technically it is but it is better to not do that) to get an empty string or string containing whitespace characters using operator>>.
You might want to use std::getline function (but be wary of mixing formatted and unformatted input). In that case you can just use .empty() member function or compare to empty string ""
It is not possible (technically it is but it is better to not do that) to get an empty string or string containing whitespace characters using operator>>.
peek() will always return the next character in the stream,
Correct, peek will return next character in stream. Which will be a newline:
entered: "cat\n" ← newline character appended by pressing enter
extracted: "cat"
left in stream: "\n"
Extracted by peek(): '\n' (will not ask user for input)