-
counters
Hey, guys. I have to follow a pseudocode tha includes loops etc. I got the loops so far, but I am having troubles with the counters.
counter is assigned 0
while counter is less than 5
display "\nI love computers!"
increment counter
endwhile
Code:
counter = 0
{
while
(counter < 5)
}
cout << "\nI love computers!";
{
increment counter
endwhile
}
I read up on it. But I don't really see how it works here.
-
The word "increment" means "add one to".
-
Yes, I know, but it's a variable. Like int counter 0 to initialize it?
-
So add one to it. Generally speaking, we use "+" to add things together, and "=" as you've seen assigns a value to a variable.
-
counter+; would come to my mind now. This stupid book dosen't tell me how to initialize the counter. Since it's a variable , I thought int counter = 0.
-
That's the initialization, int counter = 0. That's not going to help with the increment. counter is fine, + is fine -- so, what do you want to add to counter?
-
It's asking the user for his age. Then it decides if he/she can or can't vote.
Like.....
if age greater than 17
display "You can vote."
else
display "You can't vote."
endif
That's the first part, which i already have. So I think its counter+; I saw it also might be ++ ..depending on how you need to do it.
-
Right. So counter adding one is counter + 1; since you want to assign that back to counter, you get counter = counter + 1.
Now that we've done that, there is an operator "++" that adds one to a variable all in one shot.
-
Ok , so that's basically ok? For that particular part here...
Code:
counter = 0
{
while
(counter < 5)
}
cout << "\nI love computers!";
{
counter +;
endwhile
}
-
Your braces have gone awry. You managed to completely ignore my last post, so you should maybe read it this time. The only line you had correct (int counter = 0;) you managed to destroy.
-
That should be it.
Code:
counter = 0;
while (counter < 5)
{
cout << " I love computers ! \n" ;
count + 1;
}
-
Quote:
Originally Posted by
XodoX
That should be it.
Code:
counter = 0;
while (counter < 5)
{
cout << " I love computers ! \n" ;
count + 1;
}
Green = good; Red = bad. You need to assign a new value to counter as you go through the loop.
-
ops sorry, I got counter. Don't know why I copied it. But no matter what I type in it always says I love computers.
-
Did you expect it to do something else?
And did you assign a new value to counter -- adding one is not enough, you have to put the answer somewhere.
-
Well, it's only supposed to say this if it's less than 5.
And no, I probably didn't. This is so confusing. I didn't think I have to do it since it's just supposed to say yes you can vote or no you can't, and if it's less than 5 just I love computers.