Not sure where to begin

This is a discussion on Not sure where to begin within the C Programming forums, part of the General Programming Boards category; This is what I have in my .c file Code: #include <stdio.h> #include "intgetnum.h" #define MAXNUM 2 /* prototype */ ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    Not sure where to begin

    This is what I have in my .c file
    Code:
    #include <stdio.h> 
    #include "intgetnum.h"
    #define MAXNUM 2
    /* prototype */
    int getnum();
    int main()
    {
      int retval = 1;
      while (retval != NULL)
        {
          retval = getnum();
        }
      printf("The number was %d\n", retval);
      return 0;
    }
    this is what is in my header files
    Code:
    /* Declarations */
    int getnum()
    {
      int ch;
      int null;
      int pos = 0;
      int nflag = 0;
      int number = 0;
    
      if (pos == 0)
      {
        printf("Enter two numbers\n", pos++);
        scanf("%d", &number);
      }
      if (ch == '+')
        {
          pos++;
        }
      if (ch == '-')
        {
          nflag=1;
        }
      if (ch >= 48 && ch <= 57)
        {
          number *= 10;
          pos++;
        }
    
      if (nflag == 1)
        {
          number *= -1;
        }
    
      if (ch = ' ' || ch == '\n')
      {
      
      }
      else
        {
          return null;
        }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    header files are not for functions.
    add it all to one file

    getnum only returns if ch != ' ' and ch != '\n' (ch is uninitialized for all your tests on it)
    and it returns an uninitialized value

    you ask the user to enter 2 numbers but are only trying to capture 1 of them

    what is the program supposed to do?...so we have a better idea of what your trying to accomplish
    Last edited by sl4nted; 12-07-2006 at 04:45 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by sl4nted
    header files are not for functions.
    add it all to one file

    getnum only returns if ch != ' ' and ch != '\n' (ch is uninitialized for all your tests on it)
    and it returns an uninitialized value

    you ask the user to enter 2 numbers but are only trying to capture 1 of them

    what is the program supposed to do?...so we have a better idea of what your trying to accomplish
    It's suppose to just echo back the two numbers the user enters. For practice, and this is how I was told to do it, to put the functions into the header file.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    This assigns ch to ' ':
    Code:
    if (ch = ' ' || ch == '\n')
    It should be:
    Code:
    if (ch == ' ' || ch == '\n')
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    this is how I was told to do it, to put the functions into the header file.

    who told you that, I doubt they meant the actual function, maybe the prototype

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by sl4nted
    this is how I was told to do it, to put the functions into the header file.

    who told you that, I doubt they meant the actual function, maybe the prototype
    My instructor, did you test it?

    Also, ch != is not what i want, I want the user to enter a space and it will just ask the user for either the 1st number or the second number, whichever they entered the space or enter, until the enter the correct value
    Last edited by wonderpoop; 12-07-2006 at 05:25 PM.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    nope, and I didn't say it wouldn't work, I said thats not what header files are for.

    whats all the other stuff for if you just want to echo the 2 integers?

    scanf("%d", &number);
    will only get 1 of them

    edit::
    again I didn't say ch != is what you want I said
    "getnum only returns if ch != ' ' and ch != '\n' "
    please read a bit more carefully the answers are there

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by sl4nted
    nope, and I didn't say it wouldn't work, I said thats not what header files are for.

    whats all the other stuff for if you just want to echo the 2 integers?

    scanf("%d", &number);
    will only get 1 of them

    edit::
    again I didn't say ch != is what you want I said
    "getnum only returns if ch != ' ' and ch != '\n' "
    please read a bit more carefully the answers are there
    I fixed that part, it runs, it's just that my value keeps coming back as 0

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    That's because your only return statement is in the else block. Because there is no other return statement, C assumes you want to return 0 (I think).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    null is not initialized
    int null = 1; for example

    and you need a return outside of that else block, because if ch is space or enter, it doesn't return ::edit echoes

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by sl4nted
    null is not initialized
    int null = 1; for example

    and you need a return outside of that else block, because if ch is space or enter, it doesn't return ::edit echoes
    Cool thanks, i'm almost there, now I know it's the null, I assigned 1 to null but now it just loops with no value hmmm

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    because retval is never NULL

    getnum only returns 1
    since you've set
    null = 1

    null is not the same as NULL

    Code:
    while (retval != NULL)
    Code:
    return null;

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    Edited

    Code:
    #include <stdio.h> 
    #include "intgetnum.h"
    #define MAXNUM 2
    /* prototype */
    int getnum();
    int main()
    {
      int retval = 1;
      while (retval != NULL)
        {
          retval = getnum();
        }
      printf("The number was %d\n", retval);
      return 0;
    }
    Code:
    /* Declarations */
    int getnum()
    {
      int pos = 0;
      int nflag = 0;
      int number = 0;
      int num1, num2;
      char ch;
      ch=getchar();
        
      printf("Enter your first numbers\n");
      scanf("%d", &num1);
      printf("Enter your second number\n");
      scanf("%d", &num1);
    
      if (pos == 0)
      {
        pos++;  
      }
      if (ch == '+')
        {
          pos++;
        }
      if (ch == '-')
        {
          nflag=1;
        }
      if (ch >= 48 && ch <= 57)
        {
          number *= 10;
          ch = 48;
          pos++;
          ch=getchar();
        }
    
      if (nflag == 1)
        {
          number *= -1;
        }
    
      if (ch == ' ' || ch == '\n')
        {
        }
        else
    
        return NULL;
    
    }
    I still get a infinate loop, unless i press a letter a few times, then I get a return value of 0

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
        else
    
        return NULL;
    even without the { }
    that is still considered part of the else. if no braces are supplied the first line after goes with it

    so
    Code:
    else 
    ;
    return NULL;
    whould be sufficient for now, since I'm assuming you'll put somthing with that else somtime
    otherwise take it out completely

    also NULL is not an int, so in this case its probably not the thing you want to return.

    edit:: the FAQ has an article about NULL check it out
    Last edited by sl4nted; 12-07-2006 at 06:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me how to begin please...
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 10:18 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Resource syntax error...
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 06-22-2006, 05:23 PM
  4. Where to begin, graphically speaking?
    By Sennet in forum Game Programming
    Replies: 14
    Last Post: 01-22-2006, 01:28 AM
  5. How would one begin?
    By Mr_Acclude in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2005, 09:08 PM

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