Thread: Stupid syntax error?

  1. #16
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    so to declare a long int in Borland C++ 5.5 I use the syntax 'long int <name>'? and is a short 'short int <name>'?

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >A long int can't hold all values.
    Yeah, that's true. If you enter a 20 digit integer, it's not going to work. A universal solution would probably require reading it as a string, then using a series of if statements.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >so to declare a long int in Borland C++ 5.5 I use the syntax 'long int <name>'? and is a short 'short int <name>'?
    I believe it would be __int64 (two underscores).

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    See, when you read in a value beyond the max value it wraps around.
    It doesn't seem to be so with cin. If cin >> num fails, it seems to leave num as it is.

    Code:
    int num = 42;
    cout << "Please don't enter a valid integer: ";
    cin >> num;
    cout << num; //prints 42
    Anyway, here's what I would try:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    enum {NOT_INTEGER = 0, OUT_OF_RANGE, OK};
    
    int CheckRange(const std::string & a)
    {    
        //If anybody knows a simpler way, please tell so!
        
        //test 1: is it fully convertible
        int num;
        std::stringstream test(a);
        if (test >> num && test.eof()) return OK;
        
        /*test 2a: minus sign should be at pos 0 if anywhere*/
        size_t pos = a.rfind('-');
        if (pos && pos != std::string::npos) return NOT_INTEGER;
        
        /*test 2b: if minus sign is at pos 0, string must be
        longer than 1 character*/
        if (pos == 0 && a.size() == 1) return NOT_INTEGER;
        
        /*test 3: find other non-digits*/
        if (a.find_first_not_of("-1234567890") == std::string::npos)
            return OUT_OF_RANGE;
        else
            return NOT_INTEGER;
    }
    
    int main()
    {
        std::string s;
        while (1) {
            std::cout << "Enter an integer: ";
            std::cin >> s;
            switch (CheckRange(s)){
                case OK:
                    std::cout << "That's a valid integer." << std::endl;
                    break;
                case OUT_OF_RANGE:
                    std::cout << "Numeric but out of range." << std::endl;
                    break;
                case NOT_INTEGER:
                    std::cout << "That's not an integer." << std::endl;
                    break;
            }
        }
    }

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a crude method. It doesn't check for nonnumeric input though.
    Code:
    #include <iostream>
    #include <limits>
    #include <cstring>
    #include <sstream>
    using namespace std;
    
    int main()
    {
       string s_num;
       cout << "Please enter a integer between " << std::numeric_limits<int>::min()
       << " and " << std::numeric_limits<int>::max() << ": ";
       cin >> s_num;
    
       ostringstream oss;
       oss << std::numeric_limits<int>::max();
       string int_max = oss.str();
       if (s_num[0] == '-')
       {
          s_num = s_num.substr(1);
       }
       if (s_num.length() == 10)
       {
          if (s_num > int_max)
             cout << "Outside range." << endl;
          else if (s_num <= int_max)
             cout << "Valid integer." << endl;
       }
       else if (s_num.length() < 10)
          cout << "Valid integer." << endl;
       else
          cout << "Outside range." << endl;
    
       return 0;
    }

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    Okay great, I get it. I'll just stick with using a long int for the input and checking it against numeric_limits<int>. The program doesn't need to be totally foolproof and I've only been learning c++ for two days
    Maybe I'll make it foolproof after I've worked on arrays and functions, thanks for the help!

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by rickyoswaldiow
    Okay great, I get it. I'll just stick with using a long int for the input and checking it against numeric_limits<int>. The program doesn't need to be totally foolproof and I've only been learning c++ for two days
    Maybe I'll make it foolproof after I've worked on arrays and functions, thanks for the help!
    If by a long int you mean long, be warned: very often it would be just as large as a regular int, and you'd gain nothing.

    If you mean __int64 and your compiler accepts it: why bother to check if the input is in range of int? I mean, you'd still have to validate the input otherwise (the user might enter non-numbers anyway). But why would you be interested to restrict your user to 32 bits, if you can use and are using 64-bit integers?

    You could just as easily demonstrate your understanding of checking ranges with other examples (e.g precentages as suggested above). It might also be more practical to learn what to do if user enters "fourty-two" where an int is expected.

  8. #23
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    anon: it's "forty-two"

    Try this instead, if you want to hold larger numbers. Now if the user happens to enter a number which just won't fit into the RAM...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

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