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...

  • Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that...

    Programming Assignment Objective Write a Java program that utilizes multiple classes. Write a Java program that utilizes inheritance in a practical manner. Problem: Quiz Bowl Your high school quiz bowl team has been losing its edge and needs to find a method to improve. Knowing that you are a savvy programmer, your coach asks you to write a program that the team members can use to hone their skills. Quiz Bowl questions come in three varieties: True/False Multiple Choice (variable...

  • In this lab you will implement tickets-for-seat-management system. The basic idea is that your program reads...

    In this lab you will implement tickets-for-seat-management system. The basic idea is that your program reads an event number and seat number request from a user, and determines if the seat is available (and marks it taken if it was), or is unavailable (so asking the user to choose again). As part of that exercise, you’ll be working with a file of “canned” requests to exercise your program. That is, instead of typing the requests in to your program, the...

  • 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...

  • 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...

  • 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,...

  • 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...

  • JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow...

    JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

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