newb: help with bool functions

This is a discussion on newb: help with bool functions within the C Programming forums, part of the General Programming Boards category; hey everyone i am working on a tic tac toe game were i randomly generate a board when the user ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    46

    newb: help with bool functions

    hey everyone i am working on a tic tac toe game were i randomly generate a board when the user inputs a non negative number. so first i have to validate that the user actaully inputed a number and not a letter or a negative number. so far i cant get the program to recognize if the input is negative. but it will recognize if the input is a letter though.
    here is my code
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdbool.h>
    #include <ctype.h>
    void input(int* num1);
    
    
    int main(void)
    {
       int num1;   
       input(&num1);    
      
        return 0;
    }
    
    void input(int* num1)
    {
       bool success = false;
       int scanres;
          
    
       printf("\nPlease enter a non-negative number seed: ");
       scanres = scanf("%d", &num1);
      
       if(scanres < 0)
         printf("\nYou entered a negative number.");
       else if (scanres == 1) 
          success = true;
       else if (scanres == 0)
          printf("\nNon-int data.");
       else       
          printf("\nEnd of data stream.");
      
    
       return ;
    }
    even though i have the if(scanres < 0)
    for some reason it wont recogive that its negative and any negative number passes as true.
    please help...the project is due wensday

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Check num1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    ya i tried that

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Betcha didn't try this
    Code:
    if ( scanres == EOF ) {
        printf("\nEnd of data stream.");
    } else
    if ( scanres == 0 ) {
        printf("\nNon-int data.");
    } else {    // it's 1
        if ( num1 < 0 ) {
            printf("\nYou entered a negative number.");
        } else {
            success = true;
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    i just tried that and it still doesnt recogize that num1 is negative.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    post your code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    num1 is not an int.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well spotted - the whole input function is off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    how do i fix

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Every time you want to think of the address of your number, it's just "num1", and every time you want the value of your number, it's "*num1".

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    46
    i am still confused

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    num1 is not a number. It is the address of where a number lives. You cannot use num1 as a number and expect it to work.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > scanres = scanf("&#37;d", &num1);
    num1 is already a pointer to an int, so having & isn't necessary.

    Likewise, when you test it (in my code), you need *num1

    I'm surprised you didn't get any warnings with this code, which compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. NewB with C++ - Functions & Classes
    By Rider in forum C++ Programming
    Replies: 19
    Last Post: 11-23-2005, 10:32 AM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21