Archived

This topic is now archived and is closed to further replies.

C++ n00b needs help

This topic is 5132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I just started learning C++ and doing the basic things like random number guessing games and stuff, I''m working through the basics then I''ll attempt to do the Tetris in an hour thing. I''m working through a C++ book and there''s an end of chapter excercise asking me to.. "Write a program to read in a series of positive integers and display the total number entered, the number of time a number is immediately followed by a smaller number and the number of times a number is immediately followed by a larger one" Here''s what I''ve written... #include <iostream> void main() { //define variables int current, last, greater, smaller, total; //set-up variables current = -1; last = 0; greater = 0; smaller = 0; total = 0; //begin process while (current != 0) { cout << "Please enter a positive integer or 0 to Quit. >"; cin >> current; endl; endl; //if first time round make the last number the current one if (last = 0) { last = current; } //add one to the number of integers, if not exiting if (current > 0) { ++total; } //if current int is greater than last, add one to greater if (current > last) { ++greater; } //if current int is smaller than last, add one to smaller if (current < last) { ++smaller; } //make the last number the current one last = current; } //output cout << "The number of integers you entered is " << total << endl << endl; cout << "# of times a number was followed by a greater one is " << greater << endl << endl; cout << "# of times a number was followed by a smaller one is " << smaller << endl << endl; cout << "Press to Quit. "; cin.get(); cin.get(); } But what happens is the "greater" number goes up by one each time the while loop runs through and the "smaller" number doesn''t go up. The total works fine though. If anyone could point out where I''m going wrong that''d be great. Thanks.

Share this post


Link to post
Share on other sites
since every number you enter is probably greater than 0, you may want to replace if(last = 0) by if(last == 0).

A general tip for these = vs == problems is to turn the conditionals around:

if(NULL == *x)

and

if(0 == last)

because if you now forget an =, the compiler is going to warn you about it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks! adding in the == made it "almost" work, i think it''s including the 0 for quitting into the numbers, but it''s almost there now.

/prawnstar69 (too lazy to log-in)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your not suppose to assign variables inside an if statement.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can but it''s poor programming practice. I hope you know the difference between the "=" and "==". The == is a comparison operator which returns either true or false, while = is the assignment operator. You can use either of these in your if statement, however assigning variables in if statements can usually end up badly if you don''t know what you''re doing. Assign the variable before the if statement instead, and only do comparisons there.

Share this post


Link to post
Share on other sites
Still, many people write stuff like this

BOOL bItHappened = FALSE;
if(bItHappened = MakeItHappen())
{
DoSomethingBecauseOfIt
}

DoSomethingElseWith(bItHappened)

But that''s just a shorthand for writing
bItHappened = MakeItHappen();
if(bItHappened) (or if(bItHappened == TRUE) )
...


Share this post


Link to post
Share on other sites