How do I create a program where it reads in text from the user and then outputs the shortest and longest words and how many characters these words contain?
So far, I can only create a program that counts the number of words in the text.
int count_words {};
string word;
cout << "Type a text:" << endl;
while (cin >> word)
{
count_words++;
}
cout << "The text contains " << count_words << " words." << endl;
Can the loop be manipulated so that it determines the shortest and longest words?
Simply declare a couple of string variables, and then inside the while loop you can assign word to those variables when word.size() is larger/smaller than the size() of those variable, eg:
size_t count_words = 0;
string word, longest_word, shortest_word;
cout << "Type a text:" << endl;
while (cin >> word)
{
++count_words;
if (word.size() > longest_word.size())
longest_word = word;
if (shortest_word.empty() || word.size() < shortest_word.size())
shortest_word = word;
}
cout << "The text contains " << count_words << " word(s)." << endl;
if (count_words > 0) {
cout << "The shortest word is " << shortest_word << "." << endl;
cout << "It has " << shortest_word.size() << " character(s)." << endl;
cout << "The longest word is " << longest_word << "." << endl;
cout << "It has " << longest_word.size() << " character(s)." << endl;
}
Online Demo
Alternatively:
string word;
cout << "Type a text:" << endl;
if (cin >> word) {
size_t count_words = 1;
string longest_word = word, shortest_word = word;
while (cin >> word) {
++count_words;
if (word.size() > longest_word.size())
longest_word = word;
if (word.size() < shortest_word.size())
shortest_word = word;
}
cout << "The text contains " << count_words << " word(s)." << endl;
cout << "The shortest word is " << shortest_word << "." << endl;
cout << "It has " << shortest_word.size() << " character(s)." << endl;
cout << "The longest word is " << longest_word << "." << endl;
cout << "It has " << longest_word.size() << " character(s)." << endl;
}
else {
cout << "No text was entered." << endl;
}
Online Demo
Related
Im writing a wordle game and am trying to get the output to work, but i have no clue as to what im doing wrong here. The basic version of the code is 5 letter words are taken from a word file and stored in an array, then a random word is picked from there to be the answer to the wordle. Then the user is prompted for input and once they input a word, it verifies the length, as well as if it is in the array of words. then another function takes the guessed word and compares it against the winning word and outputs the guessed word with colors to show what letters are in the right spot, like real wordle. The issue arises when i try to print everything out, i tried to use a while loop to ask for input but i couldnt get it to work so i decided to test it by making a function that asks for the input, then i called it six times, each time giving it a different variable to store the result in, then print out the first one on the first guess, the first and second on the second guess, and so on. But it only prints out the current guess. It might be something very obvious but i have spent so much time coding i have no idea. any help is greatly appreciated.
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
const string CORRECT = "\033[7;32m";
const string CLOSE = "\033[7;33m";
const string INCORRECT = "\033[7;37m";
const string END = "\033[0m";
int verifyExists(string word, string verifyArr[2315]) {
if (word.size() == 5)
{
for (int i = 0; i < 2315; i++)
{
if (word == verifyArr[i])
{
return 1;
}
}
return 2;
} else {
return 3;
}
}
string buildResult(string plyrGuess, string word)
{
string result[5];
string color;
for (int i = 0; i < 5; i++){
if (plyrGuess[i] != word[i])
{
color = INCORRECT + plyrGuess[i] + END;
result[i] = color;
}
if ((plyrGuess[i] != word[i]) && (plyrGuess[i] == word[0] || plyrGuess[i] == word[1] || plyrGuess[i] == w\
ord[2] || plyrGuess[i] == word[3] || plyrGuess[i] == word[4]))
{
color = CLOSE + plyrGuess[i] + END;
result[i] = color;
}
if (plyrGuess[i] == word[i])
{
color = CORRECT + plyrGuess[i] + END;
result[i] = color;
}
}
string done;
for (int i = 0; i < 5; i++)
{
cout << result[i];
}
getline(cin, done);
return done;
}
string askInput(string array[2315])
{
string guessWord;
cout << "What word would you like to guess?" << endl;
getline(cin, guessWord);
while (verifyExists(guessWord, array) == 2)
{
cout << "The word: " << guessWord << " is not in the word list" << endl;
getline(cin, guessWord);
}
while (verifyExists(guessWord, array) == 3)
{
cout << "You must enter a word that is 5 letters in length: " << endl;
getline(cin, guessWord);
}
return guessWord;
}
void playGame(string winWord, string arr[2315]) {
cout << "Ok. I am thinking of a word with 5 letters." << endl;
string guess1 = askInput(arr);
string done1 = buildResult(guess1, winWord);
cout << done1 << "\n" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
string guess2 = askInput(arr);
string done2 = buildResult(guess2, winWord);
cout << done1 << endl;
cout << done2 << endl;
cout << "_____" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
string guess3 = askInput(arr);
string done3 = buildResult(guess3, winWord);
cout << done1 << endl;
cout << done2 << endl;
cout << done3 << endl;
cout << "_____" << endl;
cout << "_____" << endl;
cout << "_____" << endl;
string guess4 = askInput(arr);
string done4 = buildResult(guess4, winWord);
cout << done1 << endl;
cout << done2 << endl;
cout << done3 << endl;
cout << done4 << endl;
cout << "_____" << endl;
cout << "_____" << endl;
string guess5 = askInput(arr);
string done5 = buildResult(guess5, winWord);
cout << done1 << endl;
cout << done2 << endl;
cout << done3 << endl;
cout << done4 << endl;
cout << "_____" << endl;
string guess6 = askInput(arr);
string done6 = buildResult(guess6, winWord);
cout << done1 << endl;
cout << done2 << endl;
cout << done3 << endl;
cout << done4 << endl;
cout << done5 << endl;
cout << done6 << endl;
}
int main() {
string wordArray[2315];
ifstream myfile ("proj1_data.txt");
cout << " Welcome to UMBC Wordle" << endl;
if (myfile.is_open())
{
string word;
int loop = 0;
while (getline(myfile, word))
{
wordArray[loop++] = word;
}
cout << " " << endl;
cout << " Your file was imported!" << endl;
cout << " 2315 Words imported" << endl;
cout << " " << endl;
myfile.close();
}
srand(time(0));
string chosenWord = wordArray[rand() % 2315];
playGame(chosenWord, wordArray);
return 0;
}
I dont get any errors when i compile it so i know that nothings "wrong" with the code, the problem is in the playGame function, i included the whole code in case that helps
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Link to Code: https://onlinegdb.com/B1DsFDa8D
Hello, my while loops aren't responding to their set conditions once the value is updated. the code just keeps going until it reaches the end. If you run the code you'll see what I'm talking about(in fact it is necessary that you do so). When the value of the parameter is updated the value does add up and changes. When I print out the value of it at the end of the code it does in fact register as the number that should be assigned to it throughout the loop but the loop just doesn't do anything when it should be stopped. Basically my problem might be an infinite loop. the only thing that counters it is a "return 0;" at the end of the loop.
My professor gave specific instructions: Create a hangman game. The game usually involves one player guessing letters to a secret word. Bad guesses cause the picture of a hangman to be drawn one segment at a time. Once there are 7 bad guesses, the hangman picture has been drawn and the player guessing loses the game. In your game, seven bad answers to any of 16 multiple choice questions will result in a lost game.
Program 1 Writes 16 questions to a file called “infile.txt”
Program 2 reads “infile.txt” and uses the questions for the hangman game
Use a boolean value-returning function called "is_hung". This function takes an integer parameter called "num_errors". This parameter is tested using an "if-else" statement to determine how much of the hangman to display to the screen based on the number of wrong answers.
The entire hangman can be displayed by the code segment below:
{
cout << "\t \t \t" << " O " << endl;
cout << "\t \t \t" << "/|\\" << endl;
cout << "\t \t \t" << " | " << endl;
cout << "\t \t \t" << "/ \\" << endl;
cout << " YOU ARE HUNG" endl;
return false;
}
1 incorrect answer displays the head
2 incorrect answers displays the left arm
3 incorrect answers displays the right arm
4 incorrect answers displays the top half of the body
5 incorrect answers displays the bottom half of the body
6 incorrect answers displays the left leg
7 incorrect answers displays the right leg
My Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool is_hung(int, bool);
int main()
{
string question1;
string answer1;
string answer2;
string answer3;
string answer4;
string question2;
string answer5;
string answer6;
string answer7;
string answer8;
string question3;
string answer9;
string answer10;
string answer11;
string answer12;
string question4;
string answer13;
string answer14;
string answer15;
string answer16;
string question5;
string answer17;
string answer18;
string answer19;
string answer20;
string question6;
string answer21;
string answer22;
string answer23;
string answer24;
string question7;
string answer25;
string answer26;
string answer27;
string answer28;
string question8;
string answer29;
string answer30;
string answer31;
string answer32;
ifstream reader;
reader.open("infile.txt");
int num_right = 0;
int num_error = 0;
bool power = true;
cout << power << endl;
string user_answer1 = "";
string user_answer2 = "";
string user_answer3 = "";
string user_answer4 = "";
string user_answer5 = "";
string user_answer6 = "";
string user_answer7 = "";
string user_answer8 = "";
while (power == true) {
while (num_error < 7) //should stop the while loop when number of incorrect answers are equal to 7
{
//1
getline(reader, question1); //get line from infile.txt
getline(reader, answer1);
getline(reader, answer2);
getline(reader, answer3);
getline(reader, answer4);
cout << question1 << endl; //print out line from file
cout << answer1 << endl;
cout << answer2 << endl;
cout << answer3 << endl;
cout << answer4 << endl;
cout << "Enter Answer: ";
cin >> user_answer1; //have the user input answer
if (user_answer1 == "B") //if statement to determine correct answer
{
cout << "correct" << endl;
num_right++; //adds 1 to num_right
cout << "" << endl;
}
else //if user_answer is not equal answer/wrong answer
{
cout << "incorrect" << endl;
num_error++; //add 1 to num_error
power = is_hung(num_error, power); //get function
}
//2
getline(reader, question2);
getline(reader, answer5);
getline(reader, answer6);
getline(reader, answer7);
getline(reader, answer8);
cout << question2 << endl;
cout << answer5 << endl;
cout << answer6 << endl;
cout << answer7 << endl;
cout << answer8 << endl;
cout << "Enter Answer: ";
cin >> user_answer2;
if (user_answer2 == "B") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//3
getline(reader, question3);
getline(reader, answer9);
getline(reader, answer10);
getline(reader, answer11);
getline(reader, answer12);
cout << question3 << endl;
cout << answer9 << endl;
cout << answer10 << endl;
cout << answer11 << endl;
cout << answer12 << endl;
cout << "Enter Answer: ";
cin >> user_answer3;
if (user_answer3 == "C") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//4
getline(reader, question4);
getline(reader, answer13);
getline(reader, answer14);
getline(reader, answer15);
getline(reader, answer16);
cout << question4 << endl;
cout << answer13 << endl;
cout << answer14 << endl;
cout << answer15 << endl;
cout << answer16 << endl;
cout << "Enter Answer: ";
cin >> user_answer4;
if (user_answer4 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//5
getline(reader, question5);
getline(reader, answer17);
getline(reader, answer18);
getline(reader, answer19);
getline(reader, answer20);
cout << question5 << endl;
cout << answer17 << endl;
cout << answer18 << endl;
cout << answer19 << endl;
cout << answer20 << endl;
cout << "Enter Answer: ";
cin >> user_answer5;
if (user_answer5 == "A") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//6
getline(reader, question6);
getline(reader, answer21);
getline(reader, answer22);
getline(reader, answer23);
getline(reader, answer24);
cout << question6 << endl;
cout << answer21 << endl;
cout << answer22 << endl;
cout << answer23 << endl;
cout << answer24 << endl;
cout << "Enter Answer: ";
cin >> user_answer6;
if (user_answer6 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//7
getline(reader, question7);
getline(reader, answer25);
getline(reader, answer26);
getline(reader, answer27);
getline(reader, answer28);
cout << question7 << endl;
cout << answer25 << endl;
cout << answer26 << endl;
cout << answer27 << endl;
cout << answer28 << endl;
cout << "Enter Answer: ";
cin >> user_answer7;
if (user_answer7 == "A") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
cout << power << endl;
//8
getline(reader, question8);
getline(reader, answer29);
getline(reader, answer30);
getline(reader, answer31);
getline(reader, answer32);
cout << question8 << endl;
cout << answer29 << endl;
cout << answer30 << endl;
cout << answer31 << endl;
cout << answer32 << endl;
cout << "Enter Answer: ";
cin >> user_answer8;
if (user_answer8 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
cout << "" << endl;
if (num_error > 7) {
cout << "You got " << num_right << " out of 8. You lose." << endl;
}
else if (num_error < 7) {
cout << "You got " << num_right << " out of 8. You Win." << endl;
}
reader.close();
return 0;
}
}
reader.close();
cout << "" << endl;
if (num_error > 7) {
cout << "You got " << num_right << " out of 16. You lose." << endl;
}
else if (num_error < 7) {
cout << "You got " << num_right << " out of 16. You Win." << endl;
}
return 0;
}
bool is_hung(int num_errors2, bool power2)
{
if (num_errors2 == 1) {
cout << "\t \t \t"
<< " O " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 2) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 3) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/ \\" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 4) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 5) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 6) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "\t \t \t"
<< "/ " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 7) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "\t \t \t"
<< "/ \\" << endl;
cout << " YOU ARE HUNG" << endl;
cout << "" << endl;
power2 = false;
return power2;
}
}
There is a ridiculous amount of wasteful, repetitive code, which makes it extremely difficult to read and debug, and significantly increases the risk of making mistakes.
This code can be greatly reduced by making use of fewer variables, better loops, and a little arithmetic, eg:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool is_hung(int);
int main()
{
string question;
string answer;
string user_answer;
const string correct_answers = “BBCDADADBDABCABB”;
int num_right = 0;
int num_error = 0;
ifstream reader("infile.txt");
for(int i = 0; i < 16; ++i)
{
getline(reader, question); // get line from infile.txt
cout << question << endl; // print out line from file
for (int j = 0; j < 4; ++j)
{
getline(reader, answer);
cout << answer << endl;
}
cout << "Enter Answer: ";
cin >> user_answer; // have the user input answer
if (user_answer == correct_answers[i]) // if statement to determine correct answer
{
cout << "correct" << endl;
num_right++; // adds 1 to num_right
cout << endl;
}
else // if user_answer is not equal answer/wrong answer
{
cout << "incorrect" << endl;
num_error++; // add 1 to num_error
if (is_hung(num_error)) // get function
break;
}
}
reader.close();
cout << endl;
if (num_error == 7)
{
cout << "You got " << num_right << " out of 16. You lose." << endl;
}
else
{
cout << "You got " << num_right << " out of 16. You Win." << endl;
}
return 0;
}
bool is_hung(int num_errors)
{
cout << "\t \t \t" << " O ";
if (num_errors >= 2)
{
cout << endl;
cout << "\t \t \t" << "/";
}
if (num_errors >= 3)
{
cout << (num_errors >= 4 ? ‘|’ : ‘ ‘) << “\\”;
}
if (num_errors >= 5)
{
cout << endl;
cout << "\t \t \t" << " | ";
}
if (num_errors >= 6)
{
cout << endl;
cout << "\t \t \t" << "/";
}
if (num_errors >= 7)
{
cout << " \\" << endl;
cout << " YOU ARE HUNG";
}
cout << endl;
return (num_errors < 7);
}
Don’t you think that is much easier to work with?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct football_game
{
string visit_team;
int home_score;
int visit_score;
};
void printMenu();
int main()
{
int i, totalValues = 0;
ifstream inputFile;
string temp = "";
inputFile.open("games.txt");
if (!inputFile)
{
cout << "Error opening Input file!" << endl;
exit(101);
}
inputFile >> totalValues;
getline(inputFile, temp);
cout << " *** Football Game Scores *** " << endl << endl;
cout << " * Total Number of teams : " << totalValues << endl << endl;
football_game* records = new football_game[totalValues];
// while (!inputFile.eof())
// {// == NULL) {
for (i = 0; i < totalValues; i++)
{
getline(inputFile, records[i].visit_team);
cout << records[i].visit_team << endl;
inputFile >> records[i].home_score >> records[i].visit_score;
cout << records[i].home_score << " " << records[i].visit_score << endl;
getline(inputFile, temp);
}
//}
cout << endl;
int choice = 0;
int avg_home_Score = 0;
int avg_visit_Score = 0;
printMenu(); // prints menu
cout << "Please Enter a choice from the Menu : ";
cin >> choice;
cout << endl << endl;
while (true)
{
switch (choice)
{
case 1:
cout << " Score Table " << endl;
cout << " ***********************" << endl << endl;
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
for (int i = 0; i < totalValues; i++)
{
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|' << setw(7)
<< right << records[i].visit_score << " " << '|' << endl;
}
cout << endl << endl << endl;
break;
case 2:
{
string team_name;
cout << "Enter the Team Name : ";
cin >> team_name;
for (int i = 0; i < totalValues; i++)
{
if (records[i].visit_team == team_name)
{
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|'
<< setw(7) << right << records[i].visit_score << " " << '|'
<< endl;
}
}
cout << endl;
break;
}
case 3:
{
for (int i = 0; i < totalValues; i++)
avg_home_Score += records[i].home_score;
cout << "Average home_score: " << (avg_home_Score / totalValues) << endl << endl;
break;
}
case 4:
{
for (int i = 0; i < totalValues; i++)
avg_visit_Score += records[i].visit_score;
cout << "Average visit_score: " << (avg_visit_Score / totalValues) << endl << endl;
break;
}
default:
{
cout << "Please enter valid input !!" << endl;
break;
}
}
printMenu();
cin >> choice;
}
return 0;
}
void printMenu()
{
cout << " Menu Options " << endl;
cout << " ================ " << endl;
cout << " 1. Print Information of all Games[Table Form] " << endl;
cout << " 2. Print Information of a Specific Game " << endl;
cout << " 3. Print Average points scored by the Home Team during season" << endl;
cout << " 4. Print Average points scored against the Home Team" << endl << endl << endl;
}
Here is the input file i am using
games.txt
5
SD Mines
21 17
Northern State
10 3
BYU
10 21
Creighton
14 7
Sam Houston State
14 24
When i am using the 2nd option (Print Information of a Specific Game) from the output screen,
it ask me to enter the team name and when i enter the team-name.
For example: SD Mines it gives me an error, but when I enter the team-name with no space like: BYU it works fine for me.
cin >> team_name;
Takes the input only upto space.
You might want to use cin.getline() for taking space separated strings as input.
A small program demonstrating the same :
#include <iostream>
#include <string>
int main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Name is : , " << name << "!\n";
return 0;
}
std::cin ignores whitespaces by default.
To include spaces in your input try :
getline(cin, team_name);
This would pick up all the characters in a line until you press enter. This is available in
#include<string>
You need to flush the std::cin buffer after reading the choice:
#include <limits>
//...
cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Refer to this question for detailed explanation.
Also, if you want to read strings with spaces from the standard input, replace this:
cin >> team_name;
with this:
getline(cin, team_name);
as already mentioned in other answers. No need to flush std::cin this time, since you have already read the full line.
Finally, remove extra newlines from your games.txt:
5
SD Mines
21 17
Northern State
...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi so i have started c++ 3 days ago, read some tutorials etc. I wanted to make my own hangman game since it seemed as an easy task for a begginer but i stumbled upon an issue. Everything is working well ecept i cant seem to find a way to make 1 letter strings swap with underscores until underscores change to the missing word. So basically when you compile it you can guess the whole word only.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess;
int wrong=0;
int main (){
string copy = word;
cout << "----------------------Hello! Welcome to the HANGMAN game!---------- ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;
cin >> word;
//space
for (int x=0; x<30; x++){
cout << endl;
}
//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('_');}
cout << underscore << endl;
//MAIN WHILE
while(wrong<12){
cin >> guess;
//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << usedguess << endl;
wrong ++;
}
}
if(underscore == word){
cout << "You win!" << endl;
break;
}
if(wrong==1){
cout << "I" << endl;
}
else if(wrong==2){
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==3){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==4){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==5){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==6){
cout << "I===" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==7){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==8){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I |" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==9){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==10){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==11){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I /" << endl;
cout << "I" << endl;
}
else if(wrong==12){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I / /"<< endl;
cout << "I YOU ARE DEAD" << endl;
cout << "Game over bro! The word was: " << word <<endl;
break;
}
}
}
To compare the strings of word and guess, you can iterate over the characters in a for-loop, and check if there is a match
string word = "hangman";
string guess = "mansomething";
string underscore = string(word.size(), '_'); // init a string with underscores equal to the length of 'word'
// iterate over the characters in word and guess
for (size_t i = 0, iend = min(word.size(), guess.size()); i < iend; i++) {
if (word[i] == guess[i])
underscore[i] = word[i]; // if the characters match at position i, update the underscore.
}
cout << underscore << endl;
Afterwards, underscore contains the following
_an____
I need help getting declared string function to change white space of input file to a specific character.
if (infile.fail())
{
cout << "The file doesn't exist";
exit(-1);
}
else
{
numBooks = readFile (infile, magSub, 260);
for (i=0; i<numBooks; i++)
{
cout << "Last Name: " << magSub[i].lastName << endl;
cout << "First Name: " << magSub[i].firstName << endl;
cout << "Street Address: " << magSub[i].address << endl;
cout << "City: " << magSub[i].city << endl;
cout << "State or Province: " << magSub[i].state << endl;
cout << "Country: " << magSub[i].country << endl << endl;
cout << "Zip or Postal Code: " << magSub[i].zip << endl;
cout << "Expiration Date: " << magSub[i].expDate << endl;
cout << "Subscriber Number: " << magSub[i].subNum << endl << endl;
}
writeFile(outfile, magSub, numBooks);
}
}
void fillSpace (string &expDate)
{
for (int i=0; expDate.length(); i++)
{
if (isspace(expDate[i]))
expDate[i] = '0';
}
}
I have the function declared above main. I know I need to call the function but I can't get it to change the white spaces.
In your code for fillSpace, you are not checking for the end of string condition. You should use i<expDate.length() for checking the end of string.
You have missed the check condition in for loop of fillSpace function.
for (int i=0; i < expDate.length(); i++)
And for calling the function
you have to declare a string which will store the string from the magSub[i].expDate.
and then pass that string to the function fillSpace.
After that you will get the string with replaced char space with '0'.
cout << "Expiration Date: " << magSub[i].expDate << endl;
please use the following code:
string temp = magSub[i].expDate; // copy the string to the temp string/char array
fillSpace (temp); // Missing Line for function call
cout << "Expiration Date: " << temp << endl; // replace line with
Hope
this will Help you.