Thread: Subprogram/Function Help

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Subprogram/Function Help

    Hi, first time poster here, so I hope everything I post and type goes well. Anyway here goes...

    Okay, I'm writing a program that will randomly generate a math problem, ask the user to enter an answer, if they get it right, it gives them one of four randomly generated positive responses. If they get it wrong, they are given one of four randomly generated negative responses, and are then asked to try again two more times, upon two more failures they are given the answer. Finally, whether they get it right or wrong, the user is asked if they want another math problem or not, if yes, another is randomly generated. If no, they are told how many they got correct and how many they got wrong.

    So here's the code I have written so far:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    void greeting ();
    int goodresponses ();
    int badresponses ();
    int x = rand() %9 + 1;
    int y = rand() %9 + 1;
    int arithmetic (int x, int y);
    int ctr=1;
    int guess;
    char decision;
    int answer1= x + y;
    int answer2= x - y;
    int answer3= x * y;
    int answer4= (x * y)/x;
    int main ()
    {
    srand (time(0));
    greeting ();
    
    int arithmetic = rand() % 4 + 1;
    	if (arithmetic == 1) {
    		cout << "What is " << x << " + " << y << "?" << endl;
     }
    	else if (arithmetic == 2) {
            	cout << "What is " << x << " - " <<y << "?" << endl;
     }
    	else if (arithmetic == 3) {
            	cout << "What is " << x << " * " << y << "?" << endl;
     }
    	else if (arithmetic == 4) {
            	cout << "What is " << x*y << " / " << x << "?" << endl;
     }
    cin >> guess;
    	if (guess == answer1){
            	cout << goodresponses() << endl;
    }
    	else if (guess == answer2){
            	cout << goodresponses() << endl;
    }
    	else if (guess == answer3){
            	cout << goodresponses() << endl;
    }
    	else if (guess == answer4){
            	cout << goodresponses() << endl;
    }
    else {
            cout << badresponses() << endl;
    }
    while (ctr<=3 && guess != answer1||answer2||answer3||answer4){
    cin >> guess;
    ctr++;
    
    }
    return 0;
    }
    
    
    
    
    	void greeting () {
            	cout << "***Welcome to Math-o-matic!***" << endl;
    }
    int goodresponses () {
            int goodresponses = rand() % 4 + 1;
    		if (goodresponses == 1)  {
            		cout << "Very good!" << endl;
     }
    		else if (goodresponses == 2) {
            		cout << "Excellent!" << endl;
     }
    		else if (goodresponses == 3) {
            		cout << "Nice work!" << endl;
     }
    		else if (goodresponses == 4) {
            		cout << "Way to go!" << endl;
     }
    }
    int badresponses () {
    	int badresponses = rand() % 4 + 1;
    		if (badresponses == 1) {
            		cout << "No. Please try again." << endl;
     }
    		else if (badresponses == 2) {
            		cout << "Wrong. Try Again." << endl;
     }
    		else if (badresponses == 3) {
            		cout << "Don't give up!" << endl;
     }
    		else if (badresponses == 4) {
            		cout << "No. Keep trying." << endl;
     }
    		else if (badresponses == 4) {
            		cout << "No. Keep trying." << endl;
     }
    }
    I realize it's very rough around the edges, but I've hit a wall and my mind is fried over this program. Any comments, tips, or help would be greatly appreciated. And hopefully one day I will be able to return the favor and help you out as well. Thanks again.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I don't feel like guessing what's wrong with your program so perhaps you can tell us what's wrong ?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    I have a few problems, first when the user enters a right or wrong guess, it outputs garbage values underneathe the good or negative response, but I think that's because I have them set as ints and not strings, I'll fix that and I'm sure the garbage values will disappear.

    The unsolved problems I'm having are that when the user enters the wrong answer, they're supposed to be given two more chances and then told what the answer is, the only problem is I'm not sure how to tell them the right answer when it's all randomly generated. And I think my count controlled loop is screwed up too.

    The other problem is when they get it right or wrong I'm to ask them if they want another problem, I'll just use an if statement at that part, but if they say yes, how do I start the randomly generated problems up again? Do I wrap that whole thing in a while loop somehow? And if they say no, I'm supposed to tell them how many they got right, so I'm guessing somehow I need to set up a counter for that but I'm not sure on it either.

    That's about all the problems I'm having with this program that I can think of.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) Your rand calls aren't entirely random. See here.
    2) You need to include time.h for the time function.
    3) OK indentation, but it could be better.
    4) After you cin guess, your if staments should be:
    Code:
    if (guess == answer1 && arithmetic == 1){
            	cout << goodresponses() << endl;
    }
    //Do this for the other ones too.
    5) How to use the while loop:
    Code:
    int arithmetic;
    while (ctr<=3 && guess != answer1||answer2||answer3||answer4){
        arithmetic = rand() % 4 + 1;
     if (arithmetic == 1) {
      cout << "What is " << x << " + " << y << "?" << endl;
        }
     else if (arithmetic == 2) {
             cout << "What is " << x << " - " <<y << "?" << endl;
        }
     else if (arithmetic == 3) {
             cout << "What is " << x << " * " << y << "?" << endl;
        }
     else if (arithmetic == 4) {
             cout << "What is " << x*y << " / " << x << "?" << endl;
        }
        cin >> guess;
     if (guess == answer1){
             cout << goodresponses() << endl;
        }
     else if (guess == answer2){
             cout << goodresponses() << endl;
        }
     else if (guess == answer3){
             cout << goodresponses() << endl;
        }
     else if (guess == answer4){
             cout << goodresponses() << endl;
        }
        else {
                cout << badresponses() << endl;
        }
        ctr++;
    }
    6) This declares a function:
    Code:
    int arithmetic (int x, int y);
    Yet this function is never defined and you have a variable of the same name.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    You can make this cleaner by using switch/cases.
    Code:
    switch(varible)
    {
        case #: (arguement)
        break;
    }

Popular pages Recent additions subscribe to a feed