Thread: How to store a score?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question How to store a score?

    Hey there! I'm wonder of how to store a score I am doing a question game and needs to store coins for every question the user enters the correct answer. I want to do something like when you have completed the first question then the program will give you +3 in coins then you have completed next question then I want the program to add 3 more so it says you have a value of coins 6. But I do not know exactly how to do this the code I begun to write was this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    int answer;
    int coins = 3;
    
    //Have no questions yet if you wonder
    cout << "Question1";
    cin >> answer;
    
    if (answer == 1)
    {
       cout << "Correct!" << coins;
    }
    else if (answer != 1)
    {
       cout << "Wrong!";
    }
    return 0;
    }
    This code works for 1 question but I want it to work like add 3 for every question the user guesses correct. Like if user enters the correct answer 3 times then he/she has a value of coins 9.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You already have a coin variable in your program. I would suggest you use that. Initialize it to 0 to begin with. Each time the user gets the correct answer just increment that value by 3 ( coin += 3; ).

    On a side not, you could have just used an else instead of else if in your code sample above.

    Code:
    if (answer == 1)
    {
        coin += 3;
        cout << "Correct!  You now have " << coins << " coins.";
    }
    else
    {
       cout << "Wrong!";
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    You can use a counter. It's very simple. Initialize it at 0 and add 3 everytime the user answers correctly,

    Example:
    Code:
    #include <iostream>
    
    
    int main()
    {
        int answer;
        int coins_obtained = 0; //You initialize the counter
    
    
        std::cout<<"How much is 1+1?" <<std::endl <<std::endl;
        std::cin >> answer;
        std::cout<<std::endl;
    
    
        if (answer == 2)
        {
            std::cout<<"Correct!" <<std::endl <<std::endl;
            coins_obtained = coins_obtained + 1;  //Adds 1 to the previous value of coins
            std::cout<<"You own " <<coins_obtained <<" coin(s) at this point.";
        }
        else
        {
            std::cout<<"Wrong Answer!" <<std::endl <<std::endl;
            coins_obtained = coins_obtained; //Same number of coins - This step is not required, only added it so you understand it better
            std::cout<<"You own " <<coins_obtained <<" coin(s) at this point. ";
        }
    
    
        std::cin.ignore();
        std::cin.get();
    }
    Last edited by Khabz; 03-29-2013 at 11:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Score one for the little guy
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 07-19-2008, 02:51 AM
  2. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  3. How do you get the top score?
    By Fountain in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 03-12-2004, 09:28 AM
  4. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM