Need help understanding how to store things in C++

This is a discussion on Need help understanding how to store things in C++ within the C++ Programming forums, part of the General Programming Boards category; Alright first off, here's my code: Code: #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; void greeting ...

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

    Need help understanding how to store things in C++

    Alright first off, here's my code:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void greeting ();
    int goodresponses ();
    int badresponses ();
    int x =  rand() % 10 + 1;
    int y =  rand() % 10 + 1;
    int ctr=0;
    int rightanswer=1;
    int totalanswer=1;
    int wronganswer=1;
    int answer;
    int guess;
    char decision;
    int main ()
    {
    srand(time(0));
    greeting ();
    prob:
    int arithmetic;
            int x = rand() % 10 + 1;
            int y = rand() % 10 + 1;
            while (ctr<=3 && guess != answer) {
            arithmetic = rand() % 4 + 1;
            switch(arithmetic)
            {
            case 1:
                    cout << "What is " << x << " + " << y << "?" << endl;
                    int answer= x + y;
                    break;
            case 2:
                    cout << "What is " << x << " - " << y << "?" << endl;
                    int answer= x - y;
                    break;
            case 3:
                    cout << "What is " << x << " * " << y << "?" << endl;
                    int answer= x * y;
                    break;
            case 4:
                    cout << "What is " << x << " / " << y << "?" << endl;
                    int answer=(x*y)/x;
                    break;
            }
    cin >> guess;
            if (guess == answer){
                    goodresponses();
                    rightanswer++;
                    totalanswer++;
            }
            else {
                    badresponses();
                    cin >> guess;
                    if (guess != answer){
                    badresponses();}
            else {
                    goodresponses();
            }
                    wronganswer++;
                    totalanswer++;
            }
            cout << "Another problem (y/n)?" << endl;
            cin >> decision;
            if (decision=='y'){
                    goto prob;
            }
            else {
                    goto esc;
            }
    }
    esc:
    cout << "Here is your score:" << endl;
    float finalscore= rightanswer/totalanswer;
    cout << rightanswer << " right," << wronganswer<< " wrong==="<< finalscore;
    
    return 0;
    }
    
    void greeting () {
            cout << "***Welcome to Math-o-matic!***" << endl;
    }
    int goodresponses () {
            int goodresponses = rand() % 4 + 1;
            switch(goodresponses)
            {
                    case 1:
                            cout << "Very good!" <<endl;
                            break;
                    case 2:
                            cout << "Excellent!" <<endl;
                            break;
                    case 3:
                            cout << "Nice work!" <<endl;
                            break;
                    case 4:
                            cout << "Way to go!" << endl;
                            break;
            }
    } 
    int badresponses () {
            int badresponses = rand() % 4 + 1;
            switch(badresponses)
            {
                    case 1:
                            cout << "No. Please try again." <<endl;
                            break;
                    case 2:
                            cout << "Wrong. Try again." << endl;
                            break;
                    case 3:
                            cout << "Don't give up!" << endl;
                            break;
                    case 4:
                            cout << "No. Keep trying." << endl;
                            break;
            }
    }
    I have to store all four possibile types of answers into one variable, so I wanted to store that all into int answer, the problem is that what I have doesn't work. I thought it would because it's in a random generator so only one of those four will be picked, but apparantly my program wants to assign all of those things to int answer and so the program doesn't compile. Is there a command I have to type somewhere to clear int answer of what was inside of it before the loop goes through again? That's the only solution I can think of but I haven't learned anything like that in C++ yet that I know of.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> int answer= x + y;
    You are declaring a new variable there, is that what you want?

    >> the program doesn't compile.
    If you have compile errors, always post them as well.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    >> int answer= x + y;
    You are declaring a new variable there, is that what you want?

    Ah, there was the problem, thanks alot. I'm always making these minor stupid mistakes. Hopefully from here it'll be smooth sailing. Thanks again.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14
    Using goto isn't very good idea i think...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Replies: 12
    Last Post: 03-27-2008, 10:41 AM
  3. 2 things at once?
    By bradszy in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2008, 12:19 PM
  4. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01:27 PM
  5. Replies: 8
    Last Post: 05-13-2003, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21