Public Group

#### Archived

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

# C++ n00b needs help

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

## 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 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 on other sites
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)

woohoo it works!

##### Share on other sites
Your not suppose to assign variables inside an if statement.

why not?

##### Share on other sites
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 on other sites
Ok, thanks for the advice, I''ll try to keep it in mind in future

##### 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) )
...

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009808
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!