Page 1 of 6 123456 LastLast
Results 1 to 15 of 88

Thread: C++ Help

  1. #1
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default C++ Help

    I need help in C++. I'm trying to figure out how to detect if a number entered by the user is an integer.

    BTW we can't have equations in IF statements.

    I don't know how to go about this or what to do from here.

    Code:
    //Chris Taylor
    //2/6/06 (0010/0001/0110)
    //Terminal 2
    //Integer Recognition Program
    
    //Standard header files
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <string.h>
    #include <iomanip.h>
    
    
    int main()
    {
     ofstream outfile ("IntegerProgram.txt"); //Creates textfile
     cout << "This is the semi-godlike Integer Detection Program. It will allow you to enter a number, then determine if  the entered number is an intege.\n";
    
    
     float integer;
     int remainder;
    
     cout << "Please enter your number: ";
     cin >> integer;
    
     remainder = integer/2;
    
     cout << remainder;
    
     getch();
     return 0;
     }
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  2. #2
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    At first glance, I believe that doing a mod by 1 will work. That will return any remainders you may have, then you can compare the remainder against 0 to see if there is one.

    Code:
    cout << "Please enter your number: ";
     cin >> integer;
    
     remainder = integer%1;
    if(remainder == 0)
    {
    // do integer stuff
    }
    else
    {
    // do non-integer stuff
    }
    Figaro Castle

  3. #3
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Actually, my teacher said I need to typecast a non integer in order to use modulus, what the hell is that? And how do I do this.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  4. #4
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Typcasting is "forcing" data of one type into another. For example, below I am forcing a float of 1.5 to an int of 1.
    Code:
    float number1 = 1.5;
    int number2 = (int)number1;
    And if you did the opposite, typecast the number 1 (an int) to a float, you would get 1.0.

    I'm pretty sure you could do operator overloading to typecast entire objects, but it's been years since I've worked with C++.

    And apparently you can't mod a float. I've researched modulus a little more on the web. Here's a thread in another fourm I found on the issue:
    http://www.gamedev.net/community/for...opic_id=306374

    Like I said, it's been a while since I've used C++ so I'm still a bit rusty. Hope this helps.
    Figaro Castle

  5. #5
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    It's not working, god I suck. I should reread the entire book.

    Code:
    int main()
    {
     ofstream outfile ("IntegerProgram.txt"); //Creates textfile
     cout << "This is the semi-godlike Integer Detection Program. It will allow you to enter a number, then determine if  the entered number is an intege.\n";
    
    
    float integer;
    int remainder = (int)integer;
    
    cout << "Please enter your number: ";
     cin >> integer;
    
     remainder = integer%1;
    
     if(remainder == 0)
     {
      cout << "Yes, " << integer << "is an integer.";
     }
     else
     {
      cout << "I'm sorry, " << integer << "is not an integer.";
     }
    
    
     getch();
     return 0;
     }
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  6. #6
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Well you could always use a brute force approach.

    1) Treat the input as a string.
    2) Go through the input character by character and verify that each character is only 0-9 or a ".".
    3) If there is a ".", check that there is only one ".".
    4) If there is no decimal and it's a valid number then you have an integer.
    5) If there is a decimal, see if there is any number other than zero following it.
    6) If there are only zeros, then you have an integer, otherwise it's a float.

    It's not as pretty but it would work.
    Figaro Castle

  7. #7
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    I would personally read the number in as a string and parse the digits to see if they were all numeric and if it had a decimal point (which would mean it was a float not an int). Then one could simply cast it to an int.

    EDIT: Ah Mulley you beat me.

  8. #8
    ORANGE Dr Unne's Avatar
    Join Date
    Dec 1999
    Posts
    7,394
    Articles
    1
    Contributions
    • Former Administrator
    • Former Developer
    • Former Tech Admin

    Default

    How about testing if x - (int)x == 0.0 .

    Quote Originally Posted by Flying Mullet
    Well you could always use a brute force approach.

    1) Treat the input as a string.
    2) Go through the input character by character and verify that each character is only 0-9 or a ".".
    3) If there is a ".", check that there is only one ".".
    4) If there is no decimal and it's a valid number then you have an integer.
    5) If there is a decimal, see if there is any number other than zero following it.
    6) If there are only zeros, then you have an integer, otherwise it's a float.

    It's not as pretty but it would work.
    I think it depends on the definition of "is an integer". If you input 1.0, to store it into an int variable it would have to be cast as an int from a float or double at some point. 1.0 is technically a floating point number, mathematically equal to 1 but maybe not computationally equal. Strictly speaking I wouldn't call 1.0 an integer.

    Using your definition above though, 1e2 would also be an integer.

    My C is more than a bit rusty (and spoiled from Perl) but I think you can enter integers in straight hexidecimal in C, such as 0x100. Or in binary, 0b1001.

    You can also have + or - at the beginning of an integer.

  9. #9
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    Quote Originally Posted by Dr Unne
    How about testing if x - (int)x == 0.0 .
    Great idea.

    I'm not sure if scanf() accepts all number formats inclusively. You would have to specifically scan for either hex or decimal with the format specifier. If I remember correctly, you have to use something like strtol() to be able to input direct hex/octal numbers as well as numbers with leading + and -.

  10. #10
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Quote Originally Posted by Samuraid
    Great idea.
    Yes, very elegant.
    Figaro Castle

  11. #11
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Quote Originally Posted by Dr Unne
    How about testing if x - (int)x == 0.0 .
    That was brutally efficient, if I wasn't so sure you'd stab me I'd give you a hug.

    You helped a friend and I out immensely.
    Last edited by Cruise Control; 02-09-2006 at 03:06 AM.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  12. #12
    Golden Boy Mr. Wertz's Avatar
    Join Date
    Apr 2005
    Location
    Where ever I wanna live!
    Posts
    20

    Default

    Yes, you helped me alot. Lots of props to you.

  13. #13
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Can someone give me the coordinates to make a Zanarkand Abes Logo. It's my optional shape for a graphics program.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  14. #14
    Posts Occur in Real Time edczxcvbnm's Avatar
    Join Date
    Aug 2000
    Location
    The World
    Posts
    7,920

    Default

    Why don't you print out the logo get some graph paper and plot it out yourself? That is a simple solution.

    Note: I have no played FFX thus can't even begin to help with coordinates but I am also very art impared.

  15. #15
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Me too. I don't know where to start.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •