Hi:
I'm trying to get this algorithm working. It should open an external file and read the contents and return the number of characters. It should also return the position of a string from a function.
The character count is working but I can't for the life of me get the second function working.
Thanks
I keep getting an error saying "invalid conversion from int to const character*"
code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int CharacterCount (void);
string StringSearch (void);
ifstream textFile("myTextPlain.txt");//open file in debugger folder
int main()
{
//char chtr;//declaration...
if (! textFile)//check if correct file/can open or not
{
cout << "Error opening output file" << endl;
return -1;
}
cout << "\n" << CharacterCount() << endl;
cout << "\n" << StringSearch() << endl;
textFile.close();//Always close the file
return 0;
}
int CharacterCount()
{
int count = 0;//start count at zero
char chtr;//declaration...
while (! textFile.eof())//keep getting characters until end of file
{
textFile.get(chtr);//get characters from the file
count++;//increment through the characters
cout << chtr;//print
}
return count;
}
string StringSearch(void)
{
string found;//something
//char chtr; //declaration...
while (! textFile.eof())//keep getting characters until end of file
{
string str(textFile.get());//get characters from the file
string str2("cat");
size_t found;
found=str.find(str2);
if (found!=string::npos)
{
cout << "first 'needle' found at: " << int(found) << endl;
}
}
return string;//change 0 to something
}
Hi:
do you mean I need to initialize 'size_t found;' on line 50?
I have made some changes and gotten rid of the errors but it is not returning any values
But I'm gonna leave it here till I get some advice cause I think i'm getting into deeper waters maybe
any help appreciated
changed function
string StringSearch(void)
{
char chtr;
string str, str2, found;//something
//char chtr; //declaration...
while (! textFile.eof())//keep getting characters until end of file
{
textFile.get(chtr);
//string str(textFile.get());//get characters from the file
string str2("cat");
size_t found;
found=str.find(str2);
if (found!=string::npos)
{
cout << "first 'needle' found at: " << int(found) << endl;
}
}
return 0;//change 0 to something
}
I'm closing this thread as I have changed the program to make it function in a different way.
Please don't continue to write here as I will not have time to come to a solution to this problem until a much later date.
Thanks