I am just starting out learning C++. I have been working on this program for several days with no success. I would sincerely appreciate any advice or guidance you could provide. I apologize in advance for any bad form, and I also thank you in advance for taking the time to help me.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
ifstream inFile;
string fileName, text;
char reply, enter, ch;
int character, word, counter=0;
cout<< "Please enter the file name ";
getline(cin, fileName);
//Opens the file
inFile.open(fileName.c_str());
while(!inFile)
{
cout<< "\nNo such file. Enter again. ";
getline(cin, fileName);
inFile.open(fileName.c_str());
}
while(1)
{
cout<<"\nCount word or character? (w/c): ";
cin>> enter;
if(enter=='w'){
cout<<"\nEnter word to search: ";
cin>>text;
inFile.seekg(0, inFile.beg);
counter=0;
int i=0;
while(inFile.get(ch))
{
if(ch == ' ')
{
i=0;
}
else if(ch == text[i])
{
i++;
if(i == text.length())
{
counter++;
i=0;
}
}
}
}
else if(enter=='c')
{
cout<<"\nEnter character to search: ";
cin>> text;
counter = 0;
while(inFile.get(ch))
{
if(ch == text[0])
counter++;
}
}
cout<< "\nNumber of "<< text[0]<< " in file is " << counter;
cout<< "\nWant to proceed this file again? (y/n) ";
cin>> reply;
if (reply == 'n')
break;
}
inFile.close();
cout<<"\nThank for trying";
return 0;
}