I am trying to compare a string and a char like below :
char filter = " " // <<== this is a tab space, so I am assuming its one char
if ( line[counter] == filter[0]){
}
note that line is a normal string defined like : string line;. now for some reason the statement is never true even though there are no syntax errors.
UPDATE
inside line is :
string line = "1 90 74 84 48 76 76 80 85";
2nd UPDATE
here is the complete function I wrote :
void getResults(string line){
int tmpSize = line.length();
int counter = 0;
int tmpCounter = 0;
while(counter != MAX_No_Of_Grades){
if(line[counter] == '\t\t'){
counter++;
}else{
cout << tmpCounter << ". this is : " << line[tmpCounter] << "a" << endl;
tempGrade[counter] += line[tmpCounter];
}
tmpCounter++;
}
}
the function is technically suppose to break the string line into an array by “tabspace”. but right now counter does not change and therefore its an endless loop!
I’m not sure what code you are actually running, because the
char filter = " "line has no terminator and that assignment is definitely illegal. You cannot assign the string literal (achararray) to a singlecharvariable.If you want to determine whether the nth character of a string is a tabulation, the following code would probably be what you are looking for:
As for updates. If typed as
string line = "1 90 74 84 48 76 76 80 85";in your program, there are no tabulations in this string. There are only spaces. Moreover,'\t\t'is a pair of tables. Put a single\tin there for a single tab.Here is a slightly modified version of your sample function:
This version has an extra check in the while loop to stop consuming characters after the end of
lineis reached. It also uses a space character since your test input does not use tabulations.If this is not homework and you can use all the standard library facilities you want, I would suggest looking into more advanced input strategies. Here is a simplified version of your function to extract every single number in the array.
If you want to remove the global variable and handle any number of grades, you can use a
std::vector<>to automatically increase the “array” size as you get more and more grades.