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...
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...
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.
Error Analysis Questions: 1. Assuming the magnesium ribbon wasn't sufficiently polished to remove a coating of...
Error Analysis Questions: 1. Assuming the magnesium ribbon wasn't sufficiently polished to remove a coating of magnesium oxie on the surface. How would this affect the volume of hydrogen gas produced? Would this error cause the gas constant to be overestimated, underestimated, or remain unaffected? Please explain your reasoning. 2. Assume the water doesn't properly drain from the eudiometer tube therby leaving droplets on the sides of the glass. Would this error cause the gas constant to be overestimated, underestimated,...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT