Beginner needs help CS114

This is a discussion on Beginner needs help CS114 within the C++ Programming forums, part of the General Programming Boards category; Basic while loop program enter in numbers determines wether odd or even, positive or negative. My big problem is that ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    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;
    }

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    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;
    }
    Last edited by MacNilly; 06-18-2006 at 03:52 AM.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    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.

    // 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.
    Last edited by Mario F.; 06-18-2006 at 03:45 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    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

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    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:
    // initialize variables in 1 line, saves space looks cleaner
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21