Question

This program is part 1 of a larger program. Eventually, it will be a complete Flashcard...

  • This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game.
  • For this part, the program will
    • Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the program
      • The first line is the number of questions (just throw this line away this week)
      • The second line is question 1
      • The third line is answer 1
      • The fourth line is question 2
      • The fifth line is answer 2 and so on,
    • Read the first question/answer from the file
      • Note you can use getline(yourInputStream, yourString) to read an entire line
    • Prompt the user for the answer to the question
      • The prompt is the question from the file, followed by “? “
      • The user gets 3 tries to get it correct
      • Make an enum to remember whether the user is answering, correct, or incorrect
        • Initially the user is answering
        • If answered incorrectly 3 times, then the user is incorrect
        • If answered correctly, then the user is correct
      • Checking if the answer is correct includes some string manipulation
        • Take the line that the user entered, remove all leading whitespace, remove all trailing whitespace, and convert everything to lowercase
        • By doing this, you can simply compare the user’s answer to the correct answer with an ==
        • Note: isspace(c) returns true if c is a whitespace character like a space or tab, false otherwise
      • Use functions as appropriate – you should have more than just main()
  • This program is part 2 of the Flashcard program. You should be building on last week’s Flash.cpp.
  • For this part, the program will
    • Use parallel arrays to read all of the questions from the file
      • You may assume that the maximum number of questions/answer pairs will be 10
      • Note that when you read the first integer, the read only consumes the number. The newline character still needs to be read, so you’ll still need to do a getline() after reading the number of cards before reading the questions/answers.
    • Loop through the questions twice. The first time present each question and prompt for the answer. Give the user up to 3 chances like the first part of this assignment.
      • You’ll need to remember which questions are answered correctly (I suggest using another parallel array)
      • After each question has been asked, loop again through the questions. But this time, only ask the questions that were answered incorrectly on the first pass.
    • After looping through the questions 2 times, print a list of answers that were never entered correctly (one per line). Print “Study:” before the list of answers (even if the user got them all correct, because it is always a good reminder to Study)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;


string getRandWord();
void checkLetter(char&, string&, int&, string&);


int main() {

  
string RandomWord = getRandWord();
string hiddenWord = "";


unsigned long int _length_ = RandomWord.length();
int chances = int(_length_)+1;
char newLetter;
hiddenWord.append((_length_),'_');

cout << "The Word is "<< _length_ <<" letters long." << endl;

  
while(chances > 0){
  
cin >> newLetter;
checkLetter(newLetter, RandomWord, chances, hiddenWord);

  
if(hiddenWord == RandomWord){
cout << "Congratulations! You won the game." << endl;
break;
}
  
chances--;
}

return 0;
}

void checkLetter(char &newLetter, string &randstr, int &strlen, string &hiddenWord){

string::iterator it;

for(auto it = randstr.begin(); it != randstr.end();++it){

if(*it == newLetter){
  
hiddenWord.at(it - randstr.begin()) = newLetter;
}
}
cout << hiddenWord << endl;
}

string getRandWord(){
string filePath = "/Users/nedimkanat/XCODE/testcpp/testcpp/";

enum sizes {
COUNTER = 0,
ARRAY_SIZE = 5
};


srand((unsigned)time(NULL));

  
int randint = rand() % ARRAY_SIZE;
string str;

  
vector<string> arr;


ifstream file(filePath+"words.txt");
int counter = COUNTER;

  
if (file.is_open()){
while (getline(file,str) && counter < ARRAY_SIZE){
arr.push_back(str);
counter++;
}
file.close();
} else {
cout << "File is not open" << endl;
}


if(arr.empty()){
cout << "CANCER" << endl;
}
return arr.at(randint);
}

Add a comment
Know the answer?
Add Answer to:
This program is part 1 of a larger program. Eventually, it will be a complete Flashcard...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help and follow instructions, c++ , let me know if there is any questions Description:...

    Please help and follow instructions, c++ , let me know if there is any questions Description: This program is part 1 of a larger program. Eventually, it will be a complete Flashcard game. For this part, the program will Prompt the user for a file that contains the questions – use getline(cin, fname) instead of cin >> fname to read the file name from the user. This will keep you in sync with the user’s input for later in the...

  • Help c++ Description: This program is part 1 of a larger program. Eventually, it will be...

    Help c++ Description: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This will help you prepare for next week Print the final messages of...

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

  • ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...

    ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file that contains the maze. Read it into a two-dimensional array Remember you can use inputStream.get(c) to read the next character from the inputStream. This will read whitespace and non-whitespace characters Don’t forget to read the newline character at the end of each line Print the maze to the screen from your array You should include a ‘*’ in your maze to indicate where the...

  • please I need help with the question: Problem: Write a C++ program that reads each character...

    please I need help with the question: Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits. (*The main difference between “cin >>...

  • Below is the picture for the direction of my program and also the part where I...

    Below is the picture for the direction of my program and also the part where I have problems with I've already finished it and my code works correctly. But what I need to do is to "read the file name from the command line arguments" instead of the standard input (cin) that I did for step 1. I'm not sure how to do that. 2.13 PROGRAM 1: Calculating Coefficient Of Lift For this PROGRAM you will calculate the coefficient of...

  • Instructions Basically, you will modify the mathq program to make it satisfy a different set of...

    Instructions Basically, you will modify the mathq program to make it satisfy a different set of requirements, and fix what is not working in it. Specifically, imagine that the client for whom we created the mathq program for would now like some changes. To be exact: Part 1: They want it to provide addition and subtraction problems as well as multiplication and division. They still want the choice to be random, as before, but now the problem is randomly multiplication,...

  • Java Programming Part 1 File encryption is the science of writing the contents of a file...

    Java Programming Part 1 File encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT