Question

In: Computer Science

Simple Flashcard game There is an error with the trimWSFuntion - it should remove the leading...

Simple Flashcard game

There is an error with the trimWSFuntion - it should remove the leading and trailing whitespaces and then return the string. It will intake "line two" but will only output 'o'. I need help to know why the whole string is not output.

begin txt file :

786

lineone

line two

begin program :


#include
#include
#include
#include
using namespace std;
string trimFunction(string);
void guessFunction(string, string);
string lowercaseFuntion(string);


int main()
{

   string firstLine;
   string secondLine;
   string trimLine;
   ifstream myFile;
   string read_file;

   cout << "File? ";
   cin >> read_file;
   cin.ignore(100, '\n');
   cout << endl;
   myFile.open(read_file.c_str());
   getline(myFile, firstLine);
   getline(myFile, secondLine);
   getline(myFile, trimLine);

   string trimmedLine = trimFunction(trimLine);
   guessFunction(trimmedLine, secondLine);
   myFile.close();
}


string trimFunction(string u)
{
   char c;
   string f = "";

   int i = 0;
   c = u.at(i);
   while (!isspace(c) && i < u.length())
   {
       f = u.substr(i, u.length());
       i++;
   }
   u = f;
   int j = (u.length() - 1);
   c = u.at(j);
   while (!isspace(c) && j > 0)
   {
       f = u.substr(0, j + 1);
       j--;
   }
   return f;
}


void guessFunction(string w, string v)
{
   string guess = "";
   int i = 1;

   while (guess != w && i <= 3)
   {

       cout << v << "? " << endl;
       getline(cin, guess);

       guess = trimFunction(guess);
       guess = lowercaseFuntion(guess);

       if (i == 3)
       {
           cout << "Sorry, it's " << w << endl;
       }

       else if (guess == w)
       {
           cout << "Yay" << endl;
       }
       else
       {
           cout << "Try again" << endl;
       }
       i++;
   }
}


string lowercaseFuntion(string t)
{

   for (int j = 0; t[j]; j++)
   {
       t[j] = tolower(t[j]);
   }
   return t;
}

Solutions

Expert Solution


Related Solutions

PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
Simple code for a game on C coding.
Simple code for a game on C coding.
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
The game of Pig is a simple two-player dice game in which the first player to...
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. How to play: Players take turns rolling one six-sided dice and following these rules: If the player rolls 2 through 6, then he/she can either a. “Roll Again” or b. “Hold” At this point, the sum of all rolls is added to the player’s score, and it becomes the other player’s turn. If the player rolls 1 before...
Write a simple code for a simple connect the dots game in plain C coding.
Write a simple code for a simple connect the dots game in plain C coding.
Under the MBCA, what are the steps that should be taken by a shareholder to remove...
Under the MBCA, what are the steps that should be taken by a shareholder to remove a director? Your paper should address in detail such issues as authority to act, notice, meeting and voting requirements.
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT