I'll just post it all... It's not an error. Just a warning. It reads:
Code:
lab7.cpp(93) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
The loop I had the comparison in is near the bottom. In the palindromEval() function. I commented a note above it.
Code:
//name: Lab 7
//author: Joshua Norton
//description: This program is designed to take in a string from the user(punctuation and whitespace are BOTH accepted)
// and determine if that string is a palindrome or not.
#include <iostream>
//cctype is included for the ifpunct() and ifspace() functions
#include <cctype>
#include <string>
using namespace std;
string stringStrip(string userString);
bool palindromeEval(string originalString);
int main(void) {
int quit = 0;
bool mismatch = false;
string userString, originalString;
//menu loop
while(quit != 2) {
quit = 0;
//menu options
cout << "Please choose from the following options:" << endl << endl
<< "1) Evaluate a new string to see if it's a plaindrome." << endl
<< "2) Quit." << endl << endl;
cin >> quit;
switch(quit) {
case 1:
//prompt the user for a string
cout << "Please enter the string you would like to evaluate." << endl;
//I had some trouble with the getline function. I guess VS7 leaves a return character in the input buffer
//which causes the getline function to exit without ever prompting the user for input. I found quite a few
//posts regarding the subject and the concensous for handling it seemed to be the cin.ignore command
cin.ignore();
//intake the users string
getline(cin, userString);
originalString = stringStrip(userString);
if(palindromeEval(originalString)) {
//mismatch true
cout << endl << endl << "------------------------------------------------" << endl
<< "Sorry, \"" << userString << "\" is not a palindrome." << endl
<< "------------------------------------------------" << endl << endl;
} else {
//mismatch false
cout << endl << endl << "------------------------------------------------" << endl
<< "\"" << userString << "\" is a palindrome!" << endl
<< "------------------------------------------------" << endl << endl;
}
break;
case 2:
cout << "Quitting..." << endl;
break;
//validation for the menu
default:
cout << "You have entered an invalid option." << endl << endl;
break;
}
}
return 0;
}
//the stringStrip() function removes puncuation and whitespace from the original string then sets it all lowercase.
string stringStrip(string userString) {
string originalString;
size_t i, len;
len = userString.size();
//copy only chars that are not whitespace or punctuation
for(i = 0; i < len; i++) {
if((!isspace(userString[i])) && (!ispunct(userString[i]))) {
originalString += userString[i];
}
}
//set string to lowercase
for(i = 0; i < originalString.size(); i++) {
originalString[i] = tolower(originalString[i]);
}
return originalString;
}
//palindromeEval creates a reverse copy of the formatted string and compares the 2 strings to see if they are a palindrome
bool palindromeEval(string originalString) {
bool mismatch = false;
string reverseString;
size_t len = originalString.size();
//This loop right here used to read for(size_t i = len-1...
//create a reverse copy of the string
for(int i = len-1; i >= 0; i--) {
reverseString += originalString[i];
}
//compare the strings to see if they are a palindrome
if(originalString == reverseString) {
mismatch = false;
} else {
mismatch = true;
}
return mismatch;
}