-2

I have a homework that requires me to do operations on a string, I used an iterator in the second function to find the last index of a letter in the string and first index of it to find the distance between them. here is my code, focus on int textProcessing(const string &inputString, char letter); function:

#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
#include <iomanip>
#include <iterator>

using namespace std;

void textProcessing(string &inputString);
int  textProcessing(const string &inputString, char letter);
void textProcessing(string &inputString, char orgChar, char newChar);

int main() {
    ofstream out("text.txt");
    out<<"the earth is a very small stage in a vast cosmic arena";
    out.close();

    unsigned short operation;
    cin >> operation;

    string sentence;
    fstream inputstream;
    inputstream.open("text.txt");



    while (!inputstream.eof()) {

        getline(inputstream, sentence);
        if (sentence.length() > 0) {

            // Your code starts here
            if (operation==0){
                textProcessing(sentence);
            }
            else if(operation==1){

                char rletter;
                cin>>rletter;
                if(isupper(rletter)){
                    cout<<"Invalid";
                }
                else {
                    int ans = textProcessing(sentence, rletter);
                    cout << ans;
                }
            }
            else if(operation==2){
                char orignal;
                char newl;
                cin>>orignal>>newl;
                textProcessing(sentence,orignal,newl);
            }
            else{
                cout<<"Invalid";
            }
            // Your code ends here
        }
    }
    return 0;
}
void textProcessing(string &inputString){


    inputString.erase(remove(inputString.begin(),inputString.end(),'a'),inputString.end());
    inputString.erase(remove(inputString.begin(),inputString.end(),'o'),inputString.end());
    inputString.erase(remove(inputString.begin(),inputString.end(),'u'),inputString.end());
    inputString.erase(remove(inputString.begin(),inputString.end(),'w'),inputString.end());
    inputString.erase(remove(inputString.begin(),inputString.end(),'y'),inputString.end());
    inputString.erase(remove(inputString.begin(),inputString.end(),'i'),inputString.end());
    inputString.erase(remove(inputString.begin(),inputString.end(),'e'),inputString.end());
    cout<<inputString;


};
int  textProcessing(const string &inputString, char letter){
    int count=0;
    auto it=find(inputString.rbegin(),inputString.rend(),letter);
    int last_index =distance(inputString.begin(), (it + 1).base());
    int first=inputString.find(letter);
    int distance=(last_index-first);
    return distance;

};
void textProcessing(string &inputString, char orgChar, char newChar){
    if((isupper(orgChar))/*||(isupper(newChar))*/){
        cout<<"Invalid";
    }
    else {
        for (int i = 0; i <= inputString.length(); i++) {
            if (inputString[i] == orgChar) {
                inputString[i] = newChar;
            }

        }
        cout<<inputString;
    }

}

and here is the error from assignment website:

Your program displayed : student.cpp: In function ‘int textProcessing(const string&, char)’: student.cpp:79:10: error: ‘it’ does not name a type

System Message: ERROR/3 (<string>, line 3)

Unexpected indentation.
auto it=find(inputString.rbegin(),inputString.rend(),letter);
^
System Message: WARNING/2 (<string>, line 5)

Block quote ends without a blank line; unexpected unindent.
student.cpp:80:52: error: ‘it’ was not declared in this scope
int last_index =distance(inputString.begin(), (it + 1).base());
^ 
  • 1
    Compiles fine [here](https://onlinegdb.com/971b9ffrd). Make sure you're using `C++11` and higher otherwise `auto` won't work. – Jason May 13 '22 at 12:18
  • *"Unexpected indentation."* huh? What kind of system checks indentation when compiling? – Yksisarvinen May 13 '22 at 12:20
  • That looks like the only use of `auto` in this code. Try specifying the type instead. – Retired Ninja May 13 '22 at 12:20
  • C++ isn't indentation sensitive. It must be something for the site you're using. With that said, have you checked that all your indentation is using the same type of blank, so you don't mix tab and space? – Some programmer dude May 13 '22 at 12:20
  • The first and last messages indicate that you are supposed to solve this with an older standard than C++11 (before type inference, when `auto` was a storage class). The rest seems like something weird in the site you're using. – molbdnilo May 13 '22 at 12:20
  • And if the homework-site is so picky about indentation, then I'm surprised it doesn't complain about the spurious semicolons after a couple of the function definitions. And other small inconsistencies. Or that [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Some programmer dude May 13 '22 at 12:22
  • 1
    Unrelated to your current issue, but `while (!inputstream.eof()) { getline(inputstream, sentence);` would be better as `while (getline(inputstream, sentence)) { ... }` [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Retired Ninja May 13 '22 at 12:22
  • 1
    By the way: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – molbdnilo May 13 '22 at 12:23
  • @RetiredNinja this is the part that is static from my instructor. – Baraa najjar May 13 '22 at 12:24
  • 1
    you need to switch the C++ version like 11 or higher, moreover you can write switch-case instead of if-then-else – Lado May 13 '22 at 12:25

1 Answers1

1

The problem ‘it’ does not name a type is most surely caused by the use of auto in a Pre-C++11 standard compiler.

Make sure you're using C++11 or higher as auto is a C++11 feature.

There may be other problems with your code but the question is about the compiler error ‘it’ does not name a type.

There are many other online websites to compile and run your program one of which is this.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • unfortunately this is the only compiler I can use because it is forced on us by the instructors – Baraa najjar May 13 '22 at 12:30
  • 1
    @Baraanajjar You should ask your instructor about *"how to compile the program with `C++11` "* and then it will work with the compiler you're using. There are options(flags) that you can pass to the compiler that enable us to compile the program with a particular version of C++. You can ask the instructor about those flags for C++11. – Jason May 13 '22 at 12:31
  • And while you are at it ask him to switch to compile as c++20 and maybe teach modern c++. Knowing pre-c++11 will make you half useless in the workplace and may make it even harder to learn modern c++ later on because you have to unlearn bad habbits. – Goswin von Brederlow May 13 '22 at 16:56