'i have an assignment that works in compiler but does not work properly in assignment website
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());
^
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Anoop Rana |