While Loop
May 14, 2019 at 6:53am UTC
I set up a while loop as a validation routine that lets the user type quit to quit. For some reason the loop enters as true when I have tried to make it false. It's probably something really basic that I either forgot or am just not seeing it. What is wrong with my loop?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
int main()
{
const int SIZE = 256;
char inString[SIZE]{};
char quit[] = { 'q' ,'u' ,'i' ,'t' , '\0' };
int first, second;
cout << "Enter a string: " ;
cin.getline(inString, SIZE);
while (inString != quit) {
cout << inString;
if (isAPalindrome(inString, &inString[0], &inString[strlen(inString) - 1])) {
cout << " is a palindrome" << endl;
}
else cout << " is not a palindrome." << endl;
cout << "Pick 2 elements, everthing in between "
<< "and including the elements wil be checked for palindrome." << endl;
cout << "1st element: " ;
cin >> first;
cout << "2nd element: " ;
cin >> second;
if (isAPalindrome(inString, &inString[first], &inString[second])) {
while (first < second) {
cout << inString[first];
}
cout << " is a palindrome" << endl;
}
else {
while (first < second) {
cout << inString[first];
}
cout << " is not a palindrome." << endl;
}
cin.ignore();
cout << "Enter a string: " ;
cin.getline(inString, SIZE);
}
system("pause" );
return 0;
}
May 14, 2019 at 7:04am UTC
> while (inString != quit)
Use strcmp() when you have vanilla char arrays and pointers.
If you want to use !=, then use a std::string.
May 14, 2019 at 7:16am UTC
we to #include <string> for strcmp() correct. Unfortunately I am not allowed to use that.
May 14, 2019 at 7:23am UTC
Actually it seems to be working without that header.
Thank you Salem!
May 14, 2019 at 10:39am UTC
strcmp() is found by #include <cstring>
> Actually it seems to be working without that header.
Which header?
If it 'works' without either, that just makes you 'lucky', not 'good'.
Topic archived. No new replies allowed.