0

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

Roomba
  • 25
  • 3

2 Answers2

1

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
santo
  • 418
  • 1
  • 3
  • 13
  • there are more ways, but yes thats the 3 most common ones – 463035818_is_not_an_ai Feb 18 '20 at 16:43
  • btw I would make `++x` the default and if needed use `x++` – 463035818_is_not_an_ai Feb 18 '20 at 16:44
  • amount_of_correct_answers++ returns amount_of_correct_answers and add 1. ++amount_of_correct_answers add 1 and returns amount_of_correct_answers. In this case it's the same, but if you do something like int var = ++amount_of_correct_answers; int var = amount_of_correct_answers++; you could see something different – santo Feb 18 '20 at 16:45
  • @Roomba Concerning `amount_of_correct_answers++;`: [Increment/decrement operators](https://en.cppreference.com/w/c/language/operator_incdec) – Scheff's Cat Feb 18 '20 at 16:45
  • the first one worked like a charm, but i will try the other ones too just to get the gist of it – Roomba Feb 18 '20 at 16:54
0

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;
dbush
  • 205,898
  • 23
  • 218
  • 273