Hey All,

I'm not sure where to start with this, but I need to write a C++ program that implements the 'Three Strikes' game, where the user tries to guess a word, being given a number of chances to guess letters. Any ideas? Thanks in advance.

See ya on the flipside,

Desh2350

Recommended Answers

All 5 Replies

your assignment is very easy. i could write it in less than 5 minutes.

please excerpt some sort of effort...

peace out yo.

Well, here's my effort:

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main (int score) 
{ 
    char answer; 
    char correct;
    int i = 0; 
    int strike = 0; 
    score = 0; 
    string Questions[46]; 
    string optionA[46]; 
    string optionB[46]; 
    string optionC[46]; 
    string optionD[46]; 
    string question; 
    ifstream questIn; 
    ifstream aIn; 
    ifstream bIn; 
    ifstream cIn; 
    ifstream dIn; 
	
    questIn.open("Questions.txt"); 
    aIn.open("OptionA.txt"); 
    bIn.open("OptionB.txt"); 
    cIn.open("OptionC.txt"); 
    dIn.open("OptionD.txt"); 
  
    while(questIn && i < 45) 
    { 
        i++; 
        getline(questIn,Questions[i]); 
    } 
    i = 0; 
    while(aIn && i < 45) 
    { 
        i++; 
        getline(aIn,optionA[i]); 
    } 
    i = 0; 
    while(bIn && i < 45) 
    { 
        i++; 
        getline(bIn,optionB[i]); 
    } 
    i = 0; 
    while(cIn && i < 45) 
    { 
        i++; 
        getline(cIn,optionC[i]); 
    } 
    i = 0; 
    while(dIn && i < 45) 
    { 
        i++; 
        getline(dIn,optionD[i]); 
    } 
    do 
    { 
        i =  rand() % 45; 
        cout << "Questions[i]" << endl 
             << endl 
             << "optionA[i]" << "      " << "optionB[i]" << endl 
             << "optionC[i]" << "      " << "optionD[i]" << endl 
             << endl 
             << "What is your answer?" << endl 
             << endl; 
        cin >> answer; 
  
        if( i = 5 || 11 || 21 || 25 || 33 || 38 || 43 ) 
            correct = 'a' || 'A'; 
        else if( i = 2 || 3 || 7 || 9 || 20 || 22 || 24 || 30 || 36 || 39 || 41 || 42 || 45 ) 
            correct = 'b' || 'B'; 
        else if( i = 1 || 4 || 10 || 12 || 14 || 16 || 17 || 23 || 26 || 29 || 31 || 40 || 46 ) 
            correct = 'c' || 'C'; 
        else if( i = 6 || 15 || 18 || 27 || 32 || 35 || 37 ) 
            correct = 'd' || 'D'; 
        else if( i = 8 || 13 || 28 || 34 ) 
            correct = 't' || 'T'; 
        else 
            correct = 'f' || 'F'; 
        if( answer = correct ) 
        { 
            score = score + 1; 
            cout << "That's right, you get a point." << endl 
                 << endl; 
        } 
        if( answer != correct) 
        { 
            strike = strike + 1; 
            cout << "That's not the right answer. You get a strike." << endl 
                 << endl; 
        } 
  
  
  
    }while(strike < 3);     
  
  
    questIn.close(); 
    aIn.close(); 
    bIn.close(); 
    cIn.close(); 
    dIn.close(); 
  
    return score; 
}

somehow I need to have the program check whether the answers are correct or not and if they're incorrect, then output the error message. Thanks in advance.

There are still a lot of unknowns with your program, such as the nature of your Q/A as well as the format of your text files.

somehow I need to have the program check whether the answers are correct or not and if they're incorrect, then output the error message. Thanks in advance.

Here is one approach I would take with this assignment... You can make your text files with the question on one line, and acceptable answers on the following line:

How many protons in Uranium
92
What control surface controls the 'pitch' of an airplane
elevator, elevator-trim, trim tab
Where do horses go when they are sick
the horsepital, the glue factory
How many beers are in a 6 pack
6, half dozen, half-dozen, not enough

Since you now have a predictable format of the text file, we can make safe assumptions that the first line read will be the question and line following with be a list of acceptable answers. we can now load the text file accordingly into the data structure of ye' choice.

string text_file[69][2]
enum{question, answer};
int counter = 0;

while(infile)
{
     getline(infile, text_file[counter][question]);
     getline(infile, text_file[counter][answer]);
     counter++
}

Now we can pick questions at random..

void random_question(string text_file[69][2])
{
     int rand = rand()%(counter+1);
     string answer;

     //display question
     cout << text_file[rand][question] << '?' << endl;

     //prompt for answer
     cout << "Enter ye' answer: ";

     //get answer
     cin >> answer;

     //check answer
     //(simply calling the string class member function find() 
     //and making sure that it returns a value that is not equal to 'npos'
     //an 'npos' indication means there is no match (if any other value, assume match)
     if(text_file[rand][answer].find(answer) != string::npos)
     {
          cout << "\nYe' are correct.";
     }
     else
     {
          cout << "\n\aYe' are WRONG!!!!!!";
     }
}

You might want further explanation on how I checked to see if the answer was correct. I basically used two concepts: I called the find() function from <string>, which also includes simple examples on how to find stuff, using value 'npos.' Pretty straight forward.

Not only is this incorrect logic, it is unecessary:

//This
if( i = 5 || 11 || 21 || 25 || 33 || 38 || 43 ) 

//Should be this
if( i=5 || i=11 || i=21 || i=25 || i=33 || i=38 || i=43 )

But with the strategy I showed you in my previous examples, you can eliminate the need for all these huge if/else blocks.

Aside from making sure you have a pretty comprehensive list of possible answers for each question, ye' must also take into consideration capitalization and punctuation issues, which could result in an incorrect answer when in fact the answer could be correct.

please change the name of the var in line #4 of the random_question() function, as it has already been previously reserved in the enum{} block.

Thanks for the help. The user needs to basically guess the price of the car. They get three shots at it and have to place the numbers given to them in the correct slot. If they fail after three times to place the number in the correct slot, then they lose. That's what I'm supposed to aim for, as per the prompt. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.