Skip to content

Commit

Permalink
Refactor with capitalize function
Browse files Browse the repository at this point in the history
  • Loading branch information
kbuffardi committed Feb 21, 2022
1 parent 3c4d001 commit 2a15ce5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// function prototypes:
std::string get_hint(std::string,std::string);
void capitalize(std::string &);

// Wordler game!
int main(){
Expand All @@ -22,24 +23,23 @@ int main(){
std::cout << "Guess your five-letter word:\n_____\n";

do{
// require user to enter another guess if their word isn't 5 letters long
do{
std::cin >> guess;
}while( guess.length() != 5 );

// capitalize guess for easy comparisons
for(int i=0; i<guess.length(); i++){
guess[i] = toupper(guess[i]);
}
capitalize(guess);
guesses++;
hint = get_hint(guess,secret);
hint = capitalize(get_hint(guess,secret));

if( hint == secret ){
std::cout << "Congrats, you got it in " << guesses << " guesses!\n";
}
else{
std::cout << hint << " Guess again: ";
}
}while( hint != secret );
}while( guess != secret );


return 0;
Expand All @@ -54,4 +54,11 @@ std::string get_hint(std::string match, std::string word){
}
}
return word;
}

// capitalizes a word (to UPPER CASE)
void capitalize(std::string & word){
for(int i=0; i<word.length(); i++){
word[i] = toupper(word[i]);
}
}

0 comments on commit 2a15ce5

Please sign in to comment.