mercredi 25 mars 2020

C++ Magic 8 Ball Program

I have to make a program on C++ that simulates a Magic 8 ball, and I am close to finishing, but whenever I compile it is missing some of the output? It doesn't even show any errors or warning, but it's still missing some of the output. At first I had the functions as char types, but I changed it to string and they compile now, but are missing some text.


#include <iostream>             // to use cin and cout
#include <typeinfo>             // to be able to use operator typeid
#include <iomanip>
#include <time.h>

// Include here the libraries that your program needs to compile



using  namespace  std;

// Ignore this; it's a little function used for making tests
inline void _test(const char* expression, const char* file, int line)
{
    cerr << "test(" << expression << ") failed in file " << file;
    cerr << ", line " << line << "." << endl << endl;
}
// This goes along with the above function...don't worry about it
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

// Insert here the prototypes of the functions
int randNumGen(int highRange, int lowRange);

string fortuneTellerA(int randomNumber);

string fortuneTellerB(int randomNumber);

    int main()
 {
    // Declare a variable named randomNumber that holds whole numbers
    int randomNumber;

    // Declare a variable named lowRange that holds whole numbers and initializes it to 0
    int lowRange = 0;

    // Declare a variable named highRange that holds whole numbers and initializes it to 4
    int highRange = 4;

    // Seed the random number generator using expression 1) on my handout
    srand(static_cast<int> (time(NULL)));

    // Prompt the user to enter a question
    cout << "Ask the Magic 8 Ball a question: ";

    // Ignore the user input
    cin.ignore(100, '\n');

    // Call function randNumGen() to generate a random number and assign it to randomNumber
    randomNumber = randNumGen(highRange, lowRange);

    // Display title "Part A solution"
    cout << endl << "Part A solution" << endl << endl;

    // Display the message shown below
    //  "Answer: “, call function fortuneTellerA() to get the answer
    cout << "Answer: " << fortuneTellerA(randomNumber);

    // Display title "Part B solution"
    cout << "Part B solution" << endl << endl;

    //Displays the message shown below
    //  "Answer: “, call function fortuneTellerB() to get the answer
    cout << "Answer: " << fortuneTellerB(randomNumber) << endl << endl;

    system("pause");

    // Do NOT remove or modify the following statements
    cout << endl << "Testing your solution" << endl << endl;

    test(randomNumber >= 0 && randomNumber <= 4);           // Incorrect random number (out of range)

    test(fortuneTellerA(0) == "Yes");                       // Incorrect answer
    test(fortuneTellerA(1) == "Maybe");                     // Incorrect answer
    test(fortuneTellerA(2) == "No");                        // Incorrect answer
    test(fortuneTellerA(3) == "Ask again later");           // Incorrect answer
    test(fortuneTellerA(4) == "I don't know");              // Incorrect answer
    test(fortuneTellerA(14) == "I don't know");             // Incorrect answer

    test(fortuneTellerB(0) == "Yes");                       // Incorrect answer
    test(fortuneTellerB(1) == "Maybe");                     // Incorrect answer
    test(fortuneTellerB(2) == "No");                        // Incorrect answer
    test(fortuneTellerB(3) == "Ask again later");           // Incorrect answer
    test(fortuneTellerB(4) == "I don't know");              // Incorrect answer
    test(fortuneTellerB(14) == "I don't know");             // Incorrect answer

    system("pause");
    return 0;
}

//************************  Function definitions  *************************
// Read the handout carefully for detailed description of the functions that you have to implement

// This function generates a random number within a specified range.
// It receives two whole numbers : the first one is the upper boundary and
// the second one is the lower boundary used to generate the random number.
// Returns the random number generated using expression 2) on my handout

int randNumGen(int highRange, int lowRange)
{
    return (rand() % (highRange - lowRange + 1)) + lowRange;
}


// Thisfunction uses multi - branch if - else statements to determine the answer to be 
// returned based on the number received.
// It receives the random number(whole number) and returns the corresponding answer
// based on the table shown on my handout
//
// Important: in this solution make your function directly return the answer in each
// branch of the multi - branch if - else statements.
string fortuneTellerA(int randomNumber)
{
    if (randomNumber == 0)
        cout << "Yes";
    else if (randomNumber == 1)
        cout << "Maybe";
    else if (randomNumber == 2)
        cout << "No";
    else if (randomNumber == 3)
        cout << "Ask again later";
    else if (randomNumber == 4)
        cout << "I don't know";

    return 0;
}


// Thisfunction uses switch statements to determine the answer to be 
// returned based on the number received.
// It receives the random number(whole number) and returns the corresponding answer
// based on the table shown on my handout
//
// Important: in this solution declare a local variable that holds text and assign
// the corresponding answer in each case of the switch statement. Upon exiting the
// switch return the value in the local variable.

string fortuneTellerB(int randomNumber)
{
    switch (randomNumber)
    {
    case 1:
        cout << "Yes";
        break;
    case 2:
        cout << "Maybe";
        break;
    case 3:
        cout << "No";
        break;
    case 4:
        cout << "Ask again later";
        break;
    case 5:
        cout << "I don't Know";
        break;
    }

    return 0;
}



Aucun commentaire:

Enregistrer un commentaire