# Beginner needs help CS114

Printable View

• 06-18-2006
chrisa777
Beginner needs help CS114
Basic while loop program enter in numbers determines wether odd or even, positive or negative.

My big problem is that when I input Cntl+Z to end the program it adds +1 to both my negative and even values. Is there a way to avoid this? thanks

Code:

```void main() {         int input,                     int positive,negative,odd,even;             positive=0;         negative=0;         odd=0;         even=0;         while (!cin.eof())         {         cout <<"enter value:\n";         cin >>input;             if ((input%2==0))     even = even ++;         if ((input%2!=0))         odd = odd ++;             if (input > 0)     positive = positive ++;         if (input < 0)         negative = negative ++;                 }         cout <<"positive"<<positive<<endl;         cout <<"negative"<<negative<<endl;         cout <<"even"<<even<<endl;         cout <<"odd"<<odd<<endl; }```
• 06-18-2006
MacNilly
Code:

```#include <iostream> using namespace std; int main() // main returns int,  not void {         int number; // number is more descriptive than just "input"     // initialize variables in 1 line, saves space looks cleaner     int positive = 0, negative = 0, odd = 0, even = 0;     // we don't want to count the Ctrl-Z character as a number, so     // I structured the input loop a bit differently (and correctly)     // user inputs a number   cout <<"enter a number (ctrl-z to quit): ";   cin >> number;     // test if it's ctrl-Z     // if it is, skip this loop         while (!cin.eof())         {             if ((number % 2) == 0)             even++; // the right way to increment this number             else // if not even, it was odd - no need to do another test                 odd++;         if (number > 0) // same idea as above             positive++;             else                 negative++;         // user enters next number           cout <<"enter a number (ctrl-z to quit): ";       cin >> number;       // which gets tested again at the top, thereby avoiding       // counting ctrl-z as a valid number.         }         cout <<"positive = "<<positive<<endl;         cout <<"negative = "<<negative<<endl;         cout <<"even = "<<even<<endl;         cout <<"odd = "<<odd<<endl; }```
• 06-18-2006
Mario F.
Simply, don't test for eof...

Code:

```while (cin >> number) {     /* ... */ }```
Any non numeric value will stop the loop (eof included). Your current loop misbehaves if you enter alpha characters, for instance. Try it and get your fingers ready for a ctrl+C.

Quote:

// initialize variables in 1 line, saves space looks cleaner
int positive = 0, negative = 0, odd = 0, even = 0;
No, no, no.
- "space" is of no concern.
- It may look clearner to you, but not to others.
- It may even don't look to anyone if the type being declared is a compound type like a pointer and even more so if it is an array.
- Classes, strings, hexadecimal numerics, long integrals or floats, make it hard to declare them on the same line without the code looking like a complete mesh.
- It even becomes impractical if there is the need to comment one of those variables.
- And certainly it has the big disadvantage of "hiding" object names on a long line, making it harder to realize where a certain object has been declared/defined.

That is mostly a matter of a style. And coding style has no rules. In fact what is considered bad practice is to force one particular coding style on others. But, I'm afraid, that particular style has more disadvantages than advantages. Don't advise it.
• 06-18-2006
MacNilly
Quote:

Originally Posted by Mario F.
In fact what is considered bad practice is to force one particular coding style on others.

It may be considered bad practice by some, but it is also definately common practice in professional programming. Sometimes you have to change your coding style depending on the buisnesses or teams coding guidelines.

Of course it's a matter of personal preference, I think that you could declare a few ints on one line and initialize all to 0 on another line. OTOH, you could use one line each for each variable.

/hijack off
• 06-18-2006
Mario F.
No MacNilly. I was referring to what considerations should be given to a certain style before adopting it. Just because we are offered with many ways of doing something, and just because the word is "style is a matter of preference", it doesn't mean that all coding styles are equally acceptable.

The working environment often forces a style based on company culture and tradition coupled with the necessity of having a common style for all projects. But you have to be aware that it doesn't necessarily mean the choices are good ones. Stories of companies that adopt bad policies for their code style are all too common. But the operative word is consistency. And despite coding style "having no rules", coding style does have rules. Here are they:

- Easy to debug
- Easy to maintain
- Consistency

Of course, a few ints can be defined on the same line. Nothing stopping anyone from doing it. But let's imagine strings... and now arrays, and pointers, and long integrals, and... or maybe not just a few ints, but imagine a lot of ints...

Suddeny you are placing a lot of exclusions to what you previously thought a good coding practice. Suddenly you are left with just ints and chars, small at that, and not too many... So... does this really qualify as a good style? I'm sure you agree, no.

The answer is then, do it as you see fit as long as you:

Make it easy to debug
Make it easy to maintain
Do it consistently.

The answer is definitely not:
Quote:

// initialize variables in 1 line, saves space looks cleaner
• 06-18-2006
CornedBee
I typically avoid having more than one name in a declaration. Thus it would look like this for me:
Code:

```int positive = 0; int negative = 0; int odd = 0; int even = 0;```
If you need initialized variables, initializing them at the definition instead of just defining them and then assigning values later is a good idea, for various reasons. One of them is that it makes more sense once you move on to objects.
• 06-18-2006
Wraithan
Quote:

Originally Posted by MacNilly
Code:

```// initialize variables in 1 line, saves space looks cleaner     int positive = 0, negative = 0, odd = 0, even = 0;```

We have a saying at work when it comes to stuff like this, harddrive space is cheap, time is not. We are all allowed to program in what ever style standard we want, but we all pretty much conform to the code that has already been written.

Saving space in your source code isn't as important as keeping it readable. Knowing what type a variable is at a glance, is more valuable than stacking them on the same like (if I followed this practice, I know there are some places where there would be 20+ variables on one line.)