Question

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 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()
      • Let flash.txt contain the following lines:
4
child
small fry
happy
tickled pink
mock
poke fun at
dough puncher
baker

Sample Run #1 (bold, underlined text is what the user types):

File? flash.txt

child? small fry
Yay

Sample Run #2 (bold, underlined text is what the user types):

File? flash.txt

child? small          fry

Try again
child? still don't know

Try again
child? definitely don't know

Sorry, it's small fry

Sample Run #3 (bold, underlined text is what the user types):

File? flash.txt

child?     small FRY     
Yay
1 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;

///enum to store user state of answer
enum UserState {
answering=0,correct=1,incorrect=2
};

///class to store file question ans answer
class QuestionAnswer{
string* questionList; ///list of questions in file
string* answerList; ///list of answers relative to questionlist
int total; /// total no of questions in file
int current; /// used for initialize
public:
QuestionAnswer(){
current=0;
}
///constructor
QuestionAnswer(int tot){
current=0; ///sset curr to 0
total=tot; ///initialze totla to tot
questionList=new string[tot]; ///allocate memory spaces for tot no of Questions
answerList=new string[tot]; ///allocate memory spaces for tot no of Answers
}
/// fun to add Q and A in the questionList and answerList
void setQuestion(string Q,string A){
questionList[current]=Q;
answerList[current]=A;
current++; /// increment current
}
int getTotal(){
return total;
}
///to display All Q=> A
void displayQA(){
for(int i=0;i<total;i++){
cout<<"\nQ."<<i+1<<" "<<questionList[i]<<endl;
cout<<"A."<<i+1<<" "<<answerList[i]<<endl;
}
}
///returns the Q at postion index
string getQuestion(int index){
return questionList[index];
}
///returns the answer of Q at postion index
string getAnswer(int index){
return answerList[index];
}
};


///to read File
///returs NULL if file error
///else return QuestionAnswer* storing all Q=>A
QuestionAnswer* readFile(string fname,QuestionAnswer* QA){
fstream fin;
fin.open(fname.c_str()); ///open file in read mode /// c_str() convert string to const char*
if(!fin){ ///if error
cout<<"\nFailed to open file ";
return NULL;
}
///read file line by line

string line;
string question,answer;
if(!fin.eof()){
getline(fin,line); ///first line is total question present in file
int tot=atoi(line.c_str()); ///convert char* to int

QA=new QuestionAnswer(tot); ///call constructor of QA to allocate memory
///loop through tot no of question
for(int i=0;i<tot && fin.eof()==false;i++){
getline(fin,question); /// get Qusetion
getline(fin,answer); ///get Answer
QA->setQuestion(question,answer); ///store in QA

}
}
fin.close(); ///close file
return QA; ///after success return QA
}

///function to remove preceding and trailing unnecessary spaces
/// if input= hello bye
///output =hello bye
///NOTE -> not removes spaces between words
string removeSpace(string input){
string ans=""; //to store ans string
int i;
/// for all chars in input string
for(i=0;i<input.size() ;i++){
if(input[i]==' ') ///if space then continue
continue;

///found first letter so break
break;
}
///flag check if it is last letter
bool flag=true;
for(;i<input.size();i++){ /// iterate
///check if it is space
if(input[i]==' '){
flag=false;
///check if there is any other letter after this space
for(int j=i+1;j<input.size();j++){
if(input[j]!=' '){ ///if NOT space => letter
flag=true; /// there is any other letter after this space
break;
}

}
}
if(flag) /// space is not trailing one
ans=ans+input[i];
else ///means there is no letter after this space ==>this is trailing space
return ans;
}
return ans;
}

///fun to ask question to user one by one
///ask Q, get answer from user then compare if result is true then ask next question else exits
void askQuestion(QuestionAnswer *QA,UserState state=answering){
///asks question one by one
for(int i=0;i<QA->getTotal();i++){

string Q=QA->getQuestion(i);
string A=QA->getAnswer(i);
string userAnswer="";

int attempt=1;
while(state==answering)
{
cout<<endl<<Q<<"? ";
getline(cin,userAnswer);
///cout<<"\nUR ans =*"<<userAnswer<<"*";
///remove leading white spaces and trailing white spaces
userAnswer=removeSpace(userAnswer);
///Now compare the userAns to actual ans
if (userAnswer == A){ ///answer matches
cout<<"\nYay ";
state=correct;
}
else{ ///answer is wrong
///check attempt
if(attempt >=3){ ///attempt was last
state=incorrect;
cout<<"\nSorry ,its "<<A;
break;
}
else{ ///attempt is ,second or third
cout<<"\nTry Again ";
}
}
attempt++; ///increment attempt
} ///end while
if(state==incorrect) ///question wrong for 3 times
return;
state=answering;
} ///end for

}


int main()
{

UserState state;
//Prompt the user for a file that contains the questions
QuestionAnswer* QA;
string fname;
cout<<"\nFile ? ";
getline(cin,fname);

QA=readFile(fname,QA);
if(QA==NULL){
return 0;
}
//QA->displayQA();
askQuestion(QA,state=answering);
return 0;
}

--------------------------

input.txt

4
child
small fry
happy
tickled pink
mock
poke fun at
dough puncher
baker

------------------------

I C:\Users\owner\Desktop\PERSISTENT\TEST\Practice\FlashCard\bin\Debug\Flash Card.exe -ox File ? input.txt child? small fry Tr

Add a comment
Know the answer?
Add Answer to:
Please help and follow instructions, c++ , let me know if there is any questions Description:...
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
  • 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)...

  • ​​​​​​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 Help, I will rate either way because of the effort. Description: help in c++, this...

    Please Help, I will rate either way because of the effort. Description: help in c++, this program should have 3 files GuessWho.cpp, Person.cpp, Person.h This program will be a Guess Who game. The program should Read the first line of people.txt, Use the contents to set the members of your Person object, Print the Guess Who People heading, Print the Person that you read earlier Prompt the user for a feature (name, haircolor, hairtype, gender, glasses, eyecolor, or hat) Print...

  • Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

    Help please, write code in c++. The assignment is about making a hangman game. Instructions: 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...

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

  • Please help in c++, follow the instructions, please. try to use pass by reference and pass...

    Please help in c++, follow the instructions, please. try to use pass by reference and pass by value if you could Description: Write a program that will input wind speed and temperature and output the wind-chill factor. W = 35.74 + 0.6215 ∗ T − 35.75 ∗ V 0.16 + 0.4275 ∗ T ∗ V 0.16 Your program should use at least 2 functions in addition to main. One that uses pass by reference parameters and prompts the user for...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Description: help in c++, this program should have 3 files, TestCat.cpp, Cat.cpp, Cat.h. Write a Cat...

    Description: help in c++, this program should have 3 files, TestCat.cpp, Cat.cpp, Cat.h. Write a Cat class to maintain the information about a Cat: name, age, color, furLength, weight. Note that name, color, and furLength are all strings. Age is an integer and Weight is a floating-point numbers. Then write a test program for that class. The test program should read in values for each feature of the cat, set those, and then print them using the Cat’s print method....

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

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