Basic question on integer number input in c++

Discussion in 'Programmer's Corner' started by mentaaal, Feb 20, 2008.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys as part of my c++ course, I have been asked to do the following:
    The code i have written works, although if the user desides to input a floating point number, the program will go into a loop. I was just wandering could some kind soul please explain this to me.

    The code below contains the question as well:

    //14. Write a program to allow the user to enter an integer value, but ensure
    //that only an even number is entered.


    #include <iostream>
    using namespace std;

    int main()
    {
    cout << "Please enter an even integer value.";
    int number, temp;
    do
    {
    cin >> temp;
    if (temp%2 == 0)
    number = temp;
    else
    cout << "I am sorry but you have to enter an even integer number. Please enter an integer number.";
    }
    while (temp%2 !=0);


    return 0;
    }
     
  2. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    I managed to get the program to work with the following edit:
    int main()
    {
    cout << "Please enter an integer value.";
    int number, temp;
    float temp2;
    do
    {
    cin >> temp2;
    temp = temp2;

    if (temp%2 == 0)
    number = temp;
    else
    cout << "I am sorry but you have to enter an integer number. Please enter an integer number.";
    }
    while (temp%2 !=0);


    return 0;
    }

    Although, this program wont validate against a floating point number beginning with an even number... not really sure how to do that just yet
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Let's look at the first version you provided.
    The loop didn't start up again for me, using your code. When prompted for input, I typed 2.3<CR>. Using the debugger and single-stepping through your code, temp gets set to 2 (the integer part of 2.3), temp & 2 == 0 is true, and number gets set to 2. The expression in the while expression was false, so the program exited.

    I don't understand why you saw behavior that was different from this. Can you use your debugger to single-step through your program and see what's happening at each step? That would be helpful and maybe enlightening.

    Mark
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    This one also works for me, although it generates a warning because of the assignment of a float value to an int variable. You can get rid of the warning by using a cast in the assignment. IOW, by doing this:
    Code ( (Unknown Language)):
    1.  temp = (int)temp2;
    In any case, running your code in the debugger, here's what happens:
    After the prompt, I enter 2.3<CR>
    temp2 is set to 2.30000 (as shown in the debugger)
    temp is set to 2
    temp % 2 == 0, so number is set to 2
    In the while expression, temp % 2 != 0 is false, so the loop exits, and the program exits.
     
  5. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi mark, once again, thanks for the quick response! Well sorry i should have been more specific, when i enter a floating point number beginning with an odd digit, such as 3.2 then the program goes into a loop.
    Apologies!
    And thanks for the next reply you gave. I am just to new to this and what you have said is completely new to me although thanks for the suggestion ;-)
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    That's what it was supposed to do! The idea was for the loop to keep iterating as long as the user persisted in entering odd numbers. Of course, 3.2 is not an integer, so it doesn't fall into either category, but when the fractional part was truncated, you were left with an odd integer, causing the loop to start up again.
    Mark
     
  7. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    No no, i mean it goes into an infinite loop, without waiting for me to enter an input, it just keeps repeating itself over and over again. See image i have enclosed.
    [​IMG][​IMG]
     
  8. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Greg,
    Looks like there's some difference between the code our compilers generate. Without knowing which compiler you're using, I can't say what's going on with yours. It would be informative to use a debugger to see what gets put into temp2 each time the loop starts up. And from there, what gets put into temp. Somehow or other, an odd number is getting stored in temp, causing the loop to restart.
    Mark
     
  9. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    No problem mark, thanks for the help anyway. I tried doing as you suggested and debugging it and i got the message i have attached.
    Again, please bare with me, I dont know how to rectify this problem and debug it. [​IMG]
     
  10. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    What compiler are you using?
     
  11. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Microsoft visual c++. I cant remember the exact details but i will find out when i get home again. I would say its just some blunder i have made. I will try the code again in a new project and see if there are any options about debugging.
     
  12. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I used MS Visual C++ 6.0 for your code. Make sure that you set the configuration so that you're building a debug version. You can set this in the Project Menu --> Settings. In the Project Settings dialog, change the Settings For: selection to Win32 Debug (or else All Configurations). You don't want Win32 Release, which doesn't generate any debug information.

    After that, build your project. To start debugging, press F10, and continue pressing it to single-step through your code. There are various windows that you can look at the values of variables, and all sorts of other useful information.
    Mark
     
  13. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi again, well i got the debugging thing going, but i didnt see anything indicating where the problem lies. I had a friend here looking at it with me, he is studying computer science and he cant find the problem either. I noticed it going from line to line and whenever it goes past the cin line, the cmd window the program is running in gets brought to the front and i enter in a value. At this point I noticed that both variarbles are containing the value: -858993460
    If i type in a floating point number like 3.2 the program loops as you know but when the debugger goes back to the cin line, the temp variable is now whatever i last put in. If that value was a floating point number like 3.2 then the value of that variable is obviously 3 but the program just doesnt wait for user input. If i typed in an integer like 3 the cin does popup again. I am sorry i know you are well aware of this already but i cant really see any differences in the debugger that i can say to you.

    Its a strange one!
     
  14. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    Making the typecasting that way in C++ is not safe. In C, it is the only way of making typecasting. In C++ you should use the static_cast function instead:
    Code ( (Unknown Language)):
    1.  
    2. temp = static_cast<int> (temp2)
    3.  
     
  15. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Thanks for the advice cumesoftware, but in my defence i only first heard of the work typecasting yesterday :) I noticed its in my course notes but we only get to that at the end of the semester! Our course is creeeeeeping along rather slowly!
     
  16. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Cumesoft,
    As far as I can tell, the newer-style cast operators weren't available in Visual C++ 6.0, which is what I was using. Using the old-style cast generates no errors or warnings for me, even at the highest warning level, level 4.
    Mark
     
  17. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Greg,
    You said you were using MS Visual C++, but you forgot to mention which version. The version I'm using is Visual C++, v6.0.

    Where we are now is that ostensibly the same program is producing different behavior: I'm not able to reproduce the behavior that you report. The code that I'm working with is shown below. Please take a look and see if there's any difference between it, and yours. You said that your version would go into an infinite loop if you entered an input value of 3.2.
    Code ( (Unknown Language)):
    1.  
    2. #include "iostream.h"
    3.  
    4. int main()
    5. {
    6.     cout << "Please enter an even integer value.";
    7.     int number, temp;
    8.     float temp2;
    9.     do
    10.     {
    11.         cin  >> temp2;
    12.         temp = (int)temp2;
    13.         if (temp % 2 == 0)
    14.             number = temp;
    15.         else
    16.         cout << "I am sorry but you have to enter an even integer number. Please enter an integer number.";
    17.     }
    18.     while (temp%2 !=0);
    19.  
    20. return 0;
    21. }
    22.  
    Mark
     
  18. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    This typecasting was implemented along with C++, so it is a C++ standard. If it was not implemented it is Microsoft's fault. Microsoft is not prone to following standards.
     
  19. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys thanks for all your thoughts on this question.
    Ok i took the code from the forum in the form of a copy/paste operation and stuck it in the computers in college. I think and am not too sure but i think its the same as the program you are using (Visual C++, v6.0. ) and i got exactly the same result! I did the same thin again on my computer at home (copied and pasted from this thread) and got the same result again.

    The program i am using is detailed:

    Microsoft Visual Studio 2005
    Version 8.0.50727.867 (vsvista.050727-8600)
    Microsoft .NET Framework
    Version 2.0.50727

    Installed Edition: VC Express

    Microsoft Visual C++ 2005 76542-000-0000011-00125
    Microsoft Visual C++ 2005

    Cheers!
     
  20. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    144
    Could you not look at using the latest version: Visual C++ 2008? (Also if you are a student look at the DreamSpark programme: https://downloads.channel8.msdn.com/)

    Dave
     
Loading...