-
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.
-
Then you need to ask for an age, and check the age that was given.
-
Yes, I did. That's my first part.
Code:
cout << "\nHow old are you ?: ";
cin >> age;
if ( age > 17)
cout << " You can vote. \n" ;
else
cout << " You can not vote. \n" ;
That's right before the counter part. Dosen't give me an error.
-
So make up your mind what you want to do:
1) Ask one person their age and determine voting status.
2) Print I love computers 5 times.
3) Other (please specify): ____________________________
-
This is all included in the output part. I need to include both.
output
if age greater than 17
display "You can vote."
else
display "You can't vote."
endif
counter is assigned 0
while counter is less than 5
display "\nI love computers!"
increment counter
endwhile
-
So the first bit gets an age (presumably) and checks whether the person is of voting age. The second bit prints "I love computers" five times.
Do you think that these bits are related in any way? If so, go to Student Health and have them adjust your medication level.
If you want to do something other than print "I love computers" five times after asking someone their age, then you need to state, as clearly as you can, what it is that you are trying to do.
-
Honestly, I do not know myself. That's all I have in terms of instructions. I think if the value is less than 5 it's supposed to print I love computers. I guess like a joke... someone who is younger than 5 can probably not use the computer.
So , I get the age first. Tell the user if he can or can't vote. In case he's younger than 5 it's supposed to say I love computers.
And I'll just ignore the insult.
-
There's a difference between printing "I love computers" five times and printing I love computers one time, if the age is less than five. Your pseudocode is for the first.
If you don't know what your assignment is, then I don't know how much more anyone (including you) can do about it.
-
all right. Just nevermind then.