A program to compare these words and find the one with the longest length, and the one with the largest value
Go to solution
Solved by Mojo-Jojo,
I fixed those 2 now it's doesn't take the longest as the greatest but still the greatest value ain't right
You're subtracting '0' which is not equal to 96.
Here's my code which I tested working. I edited the varnames for more clarity too.
#include<iostream>#include<string>using namespace std;int main(){ int iWordCount,iCurrentValue,iHighestValue = 0; string highestValueString,longestString,currentString; cin>>iWordCount; for(int i=0; i<iWordCount; i++) { cin>>currentString; iCurrentValue = 0; for(int j=0;j<currentString.size();j++) { iCurrentValue += currentString[j] - '`'; } if(iCurrentValue>iHighestValue) { highestValueString.assign(currentString); iHighestValue = iCurrentValue; } if(currentString.size()>longestString.size()) { longestString.assign(currentString); } } cout<<highestValueString<<endl<<longestString; return 0;}
Edit: Subtracting 'a' returns a value too small by 1. Subtract '`' instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now