1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Other My basic c++ program (not) working as intended

Discussion in 'Software' started by Edge102030, 11 Feb 2011.

  1. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    Hi guys it's been a while but i'm back again :D. I recently rekindled my interest in C++ and while reading through a tutorial on the basics of C++ decided i would make a program that would tell you if the number you enter is odd or even, however when i tested it it told me that 5 is even...:duh:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int a, c;
    	float b;
    	cout << "Enter a number and i will define it as odd or even" << endl;
    	cin >> a;
    	b=a/2;
    	c = (int) b;
    	if (c!=b) {
    		cout << "The number is odd" << endl;
    	}
    	else {
    		cout << "The number is even" << endl;
    	}
    	getchar();
    	return 0;
    }
    
    The basic idea here is that you enter a number into the integer variable a and then b is defined as half of that value. If the number is odd then b will be a float and if the number is even then it will be an integer. From there c is defined as b (integer), this means that if b happens to be a float then anything after the decimal point will be lost and c will not be equal to b. If c is not equal to b then the program should tell you that you entered an odd number, if it is equal to b then it should tell you it is an even number.
     
  2. Krikkit

    Krikkit All glory to the hypnotoad! Super Moderator

    Joined:
    21 Jan 2003
    Posts:
    23,929
    Likes Received:
    657
    Without firing up c++ and testing it I would guesstimate that (int) 2.5 = 3. :)

    An easier way to do it would be:

    Code:
    if (a%2 == 0) {cout << "Even.\n";} else {cout << "Odd.\n";};
    
     
  3. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    Would you mind explaining what modulo '%' does? I recognise the operator but don't actually understand what that does. That certainly seems a much more efficient way of doing it though, using only one variable.
     
  4. Krikkit

    Krikkit All glory to the hypnotoad! Super Moderator

    Joined:
    21 Jan 2003
    Posts:
    23,929
    Likes Received:
    657
    Mod is basically divide until you get a remainder, the output is whatever's left.

    e.g. 5%2 = 1, i.e. 2 divides into 5 n times, and 1 is the remainder. :)
     
    Edge102030 likes this.
  5. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    Although ninja'ed by Krikkit here's my version... :)
    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
    	int a;
    
    	while (true)
    	{
    		std::cout << "Enter a value: ";
    		std::cin >> a;
    
    		if (std::cin.fail())
    		{
    			std::cin.clear();
    			break;
    		}
    
    		if (a % 2)
    			std::cout << "The value was odd.\n";
    		else
    			std::cout << "The value was even.\n";
    	}
    
    	return 0;
    }
    
     
    Edge102030 likes this.
  6. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    Ahh i see thanks very much :) +rep. Takes me back to my primary school maths lol

    Edit: thanks to azrael- too, i managed to get my own code working as well:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	int c;
    	float a, b;
    	cout << "Enter a number and i will define it as odd or even" << endl;
    	cin >> a;
    	b = a / 2;
    	c = (int) b;
    	if (c!=b) {
    		cout << "The number is odd" << endl;
    	}
    	else {
    		cout << "The number is even" << endl;
    	}
    	getchar();
    	return 0;
    }
    
    After commenting most of the code and printing b after declaring it as a / 2 i realised that a being an integer stopped b from ever becoming a float through this method of definition
     
  7. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    You could cook this down to

    Code:
    std::cout << ((a % 2) ? "Odd." : "Even") << std::endl;
    
    if you really wanted. :)

    One liners Cheesecake
     
    lp rob1 likes this.
  8. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    Using debug, I can tell you that :

    Enter 5

    b = 2

    because the value that your entering is an int, when it divides it, it doesn't give any decimal places, even though your putting it into a float.

    *edit*
    ninja'd :D


    you all had to be smarty pants didn't you.

    I was trying to remember the if with colon statement, but couldn't be bothered, :D
     
    Edge102030 likes this.
  9. Edge102030

    Edge102030 Son, i am disappoint.

    Joined:
    21 Aug 2009
    Posts:
    568
    Likes Received:
    28
    Haha :D rep for trying Cerb ;)
     
  10. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    I am so smart, I am so smart! S M R T ! I mean S M A R T ! :D
     
    Cerberus90 likes this.
  11. Krikkit

    Krikkit All glory to the hypnotoad! Super Moderator

    Joined:
    21 Jan 2003
    Posts:
    23,929
    Likes Received:
    657
    I do love a nice one-liner, I'm not efficient enough with my methods to get them going most of the time. All too often I do a bit of code, look up something I'm stuck on and find a 10 line chunk done in a couple of lines. :p
     
  12. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    Well, to be brutally honest, while one-liners can look cool they can be equally hard to debug. Especially the longer, more convoluted ones.

    I *do* love terniary comparisons, though. :)
     
  13. Technologist

    Technologist What's a Dremel?

    Joined:
    1 Feb 2011
    Posts:
    24
    Likes Received:
    0
    I've made a more general version for your code.

     
  14. Daedelus

    Daedelus What's a Dremel?

    Joined:
    7 May 2009
    Posts:
    253
    Likes Received:
    12
    It's good practice to always initialise variables otherwise they are "undefined".

    int a = 0;
    int b = 0;
     
  15. Technologist

    Technologist What's a Dremel?

    Joined:
    1 Feb 2011
    Posts:
    24
    Likes Received:
    0
    Yes, it is, but here this won't be a problem, because both variables are initialized later.
     
  16. Daedelus

    Daedelus What's a Dremel?

    Joined:
    7 May 2009
    Posts:
    253
    Likes Received:
    12
    I mean you should initialise them when they are created.

    Consider this code:

    Code:
    float pi;
    
    pi = 3.17;
    
    float rad = 5;
    float area = pi * rad * rad;

    Some time down the line another developer comes along and tries to do this:


    Code:
    float pi;
    
    float my_rad = 10;
    float my_area = pi * my_rad * my_rad;
    
    pi = 3.17;
    
    float rad = 5;
    float area = pi * rad * rad;

    This will cause a run time error.
     
  17. Technologist

    Technologist What's a Dremel?

    Joined:
    1 Feb 2011
    Posts:
    24
    Likes Received:
    0
    Of course, I know what you mean and variables/objects must be initialized, but not necesarrily at creation time. If you do the following:

    Code:
    int a = 5;
    int b = 6;
    int c = 7;
    a = b + c;
    
    then there is absolutely no point in initializing a and giving it the value of 5, the only important to note about initialization is to not try to use an uninitialized value, but this doesn't need we must initialize the values at the creation of the variable/object, because that's not necessary.
     
  18. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208

    I wouldn't say that's either devs fault.

    Second one should have read the code properly and put the extra lines in below the initialisation.

    But the first dev could have used :

    #define pi 3.14159265
     
  19. Daedelus

    Daedelus What's a Dremel?

    Joined:
    7 May 2009
    Posts:
    253
    Likes Received:
    12
    In that case, you wouldn't declare "a" until you wanted to assign a value to it:

    Code:
    int b = 6;
    int c = 7;
    int a = b + c;
    
    My point really is that there is no need to declare a variable and not assign a value to it.
     
  20. Technologist

    Technologist What's a Dremel?

    Joined:
    1 Feb 2011
    Posts:
    24
    Likes Received:
    0
    Deadelus, that's not true. Consider this code:

    Code:
    class DeadelusIsWrong {
    
    private:
    int a; //a is a member of DeadelusIsWrong and it shouldn't be initialized when declared
    
    public:
    int getA()
    {
        return a;
    }
    
    /*When the DeadelusIsWrong object is created, a is initialized, so the
    error won't happen. I'm not saying you shouldn't initialize your variables when you create them,
    I'm saying it's WRONG to state that you must always initialize a variable/member
    when you declare it. This class was written to prove what I said earlier*/
    DeadelusIsWrong(int a)
    {
    this.a = a; 
    }
    }
    As you can see, a is a member of the class and it is declared before knowing it's value. There is no point of initializing a here and you don't have an option to declare a member after your constructor was executed.
     

Share This Page