My program is supposed to create hashes for our class. I have it working the only requirement I'm missing is that it is supposed to return invalid whenever a # is found so I added a line that I hoped would fix it but I still get the same result where only a few numbers truly return the INVALID INPUT What its supposed to do is say: hello hash is 21 but if it was 24hello then it should return INVALID INPUT
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
using namespace std;
string userInput;
int strNum = 0;
int hashOfStr = 0;
bool hasDigit = false;
int strScore(string str[], string s, int n, int index){
int score = 0;
for (int j = 0; j < s.length(); j++)
{
if(s[j]=='0'||s[j]=='1'||s[j]=='2'||s[j]=='3'||s[j]=='5'||s[j]=='6'||s[j]=='7'||s[j]=='8'||s[j]=='9')
{
hasDigit = true;
}
if(s[j]!=' '){
score += s[j] - 'a' + 1;
cout << s[j] << endl;
}
}
score *= index;
return score;
}
string getSentence()
{
cout << "Mod Hashing: " << endl;
cout << "Enter Sentence To Be Hashed(0 To Quit): ";
getline(cin, userInput);
for_each(userInput.begin(), userInput.end(), [](char & c) {
c = tolower(c);
});
cout << endl;
return userInput;
}
int main()
{
while(userInput != "0")
{
getSentence();
string str[] = {userInput};
int n = sizeof(str) / sizeof(str[0]);
string s = str[0];
if(hasDigit == false)
{
for(int i = 0; i < n; i++)
{
s = str[i];
strNum = strScore(str, s, n, i+1);
}
hashOfStr = strNum % 31;
cout << "Hash of: " << userInput << " is " << hashOfStr << endl << endl;
}
else if(hasDigit == true)
{
cout << "INVALID INPUT" << endl;
hasDigit = false;
}
}
return 0;
}