Thread: Stupid syntax error?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    19

    Stupid syntax error?

    Hey, I'm revising C++ for a new job I'm trying to get. I'm having some trouble with some very basic stuff:
    Code:
    if((aRegularInt > -2147483647) && (aRegularInt < 2147483646){
           cout << "You have entered " << aRegularInt;
    }
    else{
           cout << "That is not a valid Integer!";
    }
    If I enter a value outside of those ranges it completely ignores the if statement and prints "You have entered 0". In other words it never reaches the else statement no matter what values I input!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Umm . . . (almost) no possible (standard) values that you could enter would cause the if statement to be false. It's true if the number is more than the lowest number possible and the number is less that the highest possible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by rickyoswaldiow
    Hey, I'm revising C++ for a new job I'm trying to get.
    I'll give you some advice to avoid wasting your time: revising C++ for a job application is a waste of time and rather useless for any job. Instead focus on making an actual application that does something using the language you have available already.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    There is an error in your code too. You are missing a bracket on the end of your logical if statement

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If I enter a value outside of those ranges it completely ignores the if statement and prints "You have entered 0". In other words it never reaches the else statement no matter what values I input!
    The problem is that an int can't store numbers larger or smaller than that on your platform, evidently. Try something like this instead:
    Code:
    #include <iostream>
    
    int main(void) {
        int x;
    
        std::cout << "Enter the percentage that you got: ";
        std::cin >> x;
    
        if(x < 0 || x > 100) {
            std::cout << "Wow, really?\n";
        }
        else {
            std::cout << "Good for you.\n";
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    jverkoey : The job is actually for a games developer, the company use C++. I have a portfolio of apps I've made in other languages and I'm making a game in Dark Basic Pro from scratch to show I can create all the media and compile it. I'm also making a few pointless C++ apps to show that I understand the basic concepts of programming and show how fast (or slow) I can pick up the language. Thanks for the advice though

    swgh : It's just a typo on my post, the syntax on the other computer is sound

    So I can't check on values outside of the range of the integers maximum ranges?
    The idea of the program is to show the range that an integer value can hold. The app explains the range of numbers that an integer can hold and then asks the user to input any valid integer value. If the value is outside the range of an integer then the "else" message is shown. Is it not possible to check if the user is entering a value within the range of that variable type with an IF statement?
    I'm guessing that it's the 'cin << aRegularInt' that's causing this since if a value outside of the int ranges is entered it will simply stay at 0? If this is the case, how can I check if a value is within the range of an integer?
    Last edited by rickyoswaldiow; 10-10-2006 at 03:22 PM.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    <climits> should have macros for INT_MAX and INT_MAX, as well as ranges for other types.

    Code:
    #include <iostream>
    #include <climits>
    
    int main()
    {
        std::cout << "Max int = " << INT_MAX << std::endl;
        std::cout << "Min int = " << INT_MIN << std::endl;
        std::cin.get();
    }
    As for checking user input, one thing is clear, you can't take the input as int, because you can't put more in it than it can hold. You should take it as a string and then in you can validate in any way you want.

    If you just want to check if user input was valid, you can use something like that. (Note that it can't tell exactly what the exact problem with the input was.)

    Code:
    #include <iostream>
    
    int main()
    {
        int num = 0;
        std::cout << "Enter an integer: ";
        if (!(std::cin >> num)) {
            std::cout << "Invalid input" << std::endl;
            std::cin.clear();
        }
        else
            std::cout << "Ok: " << num << std::endl;
        std::cin.ignore();
        std::cin.get();
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The idea of the program is to show the range that an integer value can hold.
    Try this.
    Code:
    #include <iostream>
    #include <limits>
    using namespace std;
    
    int main()
    {
       long long num;
       cout << "Please enter a integer between " << std::numeric_limits<int>::min()
       << " and " << std::numeric_limits<int>::max() << ": ";
       
       cin >> num;
       
       if (num < std::numeric_limits<int>::min() || num > std::numeric_limits<int>::max())
          cout << "The number entered (" << num << ") is outside the range of an int" << endl;
       else
          cout << num << " is a valid int" << endl;
       
       return 0;
    }

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Eh, Dev-C++ says: ISO C++ does not support `long long'. And if it did, the user could still enter a number that is out of the "long long" range, and you couldn't tell it from just invalid input.

    Apart from that, this code tells me always that I have entered a valid integer - even if I type random characters. I guess if cin fails, num will just have an uninitialized value.
    Last edited by anon; 10-10-2006 at 04:24 PM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    Ah brilliant
    That's exactly what I needed to know, I'll have to go try it out now on the other computer. Unfortunately this is my flatmates laptop, I don't have net access :/
    One question, you've included "using namespace std" yet you prefix some of your commands with std::. Just a typo or is there a reason for this?

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    See, when you read in a value beyond the max value it wraps around.

    Say you have 4 bits for your number, and it is unsigned.

    1111

    is the highest number, which is 15.

    one more and you have

    10000

    but since it is limited to 4 bits, you get

    0000

    But don't count on the wrap around value of a unsigned number, because how they are represented in memory is undefined.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    I understand.
    Anon: The code checks the length of a regular int against the user inputed long int.
    Last edited by rickyoswaldiow; 10-10-2006 at 04:44 PM.

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    A long int can't hold all values. A string could any value a user would put in without bashing the keyboard for several hours to get enough characters to fill up memory and swap.But, then you would have to manually parse through it to check to see how large it is. But that prevents wrap around saying it was a valid number but when it just wraped around so much it fell back in normal bounds.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Eh, Dev-C++ says: ISO C++ does not support `long long'.
    What switches are you using? I'm using Dev-C++, and it compiles fine.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One question, you've included "using namespace std" yet you prefix some of your commands with std::. Just a typo or is there a reason for this?
    More of a typo than anything. "using namespace std" was necessary since I used cout vs std::cout, and endl vs std::endl. But since numeric_limits is less commonly used, I added the std for emphasis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 10:24 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM