Roomba
Roomba

Reputation: 53

Addition to an integer variable which already has a set value

Its my second day of learning how to code. I chose C++ as my first language and decided to make a cpp project. This project has 4 question and 2 answers for each question (Yes and No). In the end it must cout you a result which depends on how many answers were correct. Everything i did was working except 1 thing. So for example if you answered at least 3 of the questions incorrectly you will receive cout << "You are stupid"; And if the amount of incorrect answer is lower than 3 then you will receive a cout << "You are smart";

As i mentioned before, i did everything right except one thing. In order to keep track of how many questions were correct/incorrect ive set a variable:

int amount_of_correct_answers;
amount_of_correct_answers = 0;

Then i made it so that if the answer is correct, it will add 1 to this variable

if(answer == true_answer)
{
 amount_of_correct_answers + 1;
}
else
{
 amount_of_correct_answers + 0;
 }

So in the end of the test you see the result(if you are stupid or smart). My question is: How do i add/substract from a variable? How do i add 1 to a variable thats set as 0 if the answer is correct? Because the code i wrote above didnt work. I think i am on the right track and my problem is syntax because i have no idea how to add/substract to/from a variable with a set value.

P.S. Keep in mind that i am very new to coding as i mentioned before, so please explain it in simple words or an example. Thank you

Upvotes: 0

Views: 655

Answers (2)

santo
santo

Reputation: 417

If you've got a variable called amount_of_correct_answers you can increment/decrement it in 3 ways:

amount_of_correct_answers = amount_of_correct_answers + 1;
amount_of_correct_answers+=1;
amount_of_correct_answers++; // you could use also ++amount_of_correct_answers in this case and have the same result

Upvotes: 1

dbush
dbush

Reputation: 223689

All this does:

amount_of_correct_answers + 1;

Is add 1 to the value of amount_of_correct_answers and discard the result. You need to assign the value back to the variable:

amount_of_correct_answers = amount_of_correct_answers + 1;

Upvotes: 0

Related Questions