-2

So I am new to c++ and i am having trouble with a program. I am trying to search through a file and then find the first occurrence of all 26 upper case letters(A,B,C...) as well as lower case letters (a,b,c,d...). I have been working with a code and figured it would be easiest to take the file and create it into a vector then go through the vector and find the first instance of each letter. Here is a example of my code.

int main()
{
    int i;
    string file = input; // User inputted file
    vector<string> v;
    ifstream ist{ file };
    if (!ist)
        error("Can not open inputed file ", file);
    while (!ist.eof())
    {
        string x;
        ist >> x;
        v.push_back(x); // Creates a string vector that is filled with everything
        // in the file
    }
    // Find A in text.
    vector<int> location;
    location = find(v.begin(), v.end(), 'A');
    if (location != v.end())
        cout << "Found A at location " << location;
    else
cout << "A was not found"

I was able to successfully retrieve the vector v and it was filled with what the file had inside. The area of trouble is how is it possible to get the location of the letter from the string vector. I am still need to c++ so i could be approaching the problem all wrong. If you could help me out that would be awesome. Thanks.

amanuel2
  • 4,268
  • 2
  • 29
  • 61
ConCon19
  • 13
  • 4
  • 1
    Please post your whole code – amanuel2 Oct 18 '16 at 00:33
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 18 '16 at 00:40
  • 1
    @πάνταῥεῖ: The right tool here is a good introductory [book](http://stackoverflow.com/q/388242/1889329). If you write `vector` to mean *"sequence of characters"*, you really need to get the basics straight. A debugger isn't going to help at this point. – IInspectable Oct 18 '16 at 00:45
  • 2
    On topic: why bother with the vector? Why not read the file character by character and stop reading the file as soon as you find the required character? Off topic: `while (!ist.eof())` is a common mistake. [Read more about it here.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Oct 18 '16 at 00:54
  • @user4581301 So are you saying while in the while loop look for the each character? how would i go about doing that? Should it be a if statement? – ConCon19 Oct 18 '16 at 01:07
  • Suggestion: Write down what you want to do in the order you want to do it. Start walking through it on paper step by step with a small data set. Does it make sense? If so, turn it into code. – user4581301 Oct 18 '16 at 03:07

1 Answers1

0

Your vector v contains all strings from file, while you are searching for letter.

make v vector<char> and read by one character