Help

This is a discussion on Help within the C Programming forums, part of the General Programming Boards category; I am writing this program as an assignment but I don't know what is wrong. I have to use gets() ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    Help

    I am writing this program as an assignment but I don't know what is wrong. I have to use gets() and getchar() to take the users name and 2 (x,y) and print on the screen each number between the values of of x and y. This is what I have so far but it has several errors.

    #include <stdio.h>

    void main(void)
    {
    /*Declare Variables*/
    int x,y;
    char name [50];

    /*Get users name*/
    name=getchar();

    /*Get values for x and y from user*/
    x=gets(int);
    y=gets(int);

    /*Loop through numbers and print*/
    while (x<=y)
    printf("\t",x);
    ++x:

    getchar(): /*wait for key stroke to exit*/

    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but I don't know what is wrong
    Your assignment is what's wrong. gets() is an old function that should be avoided.

    >>void main
    Is wrong, main returns an int.

    >>name=getchar();
    getchar() returns a single character, you're not using it correctly to get a string of input.

    >>x=gets(int);
    Although using gets() is wrong (as above), it doesn't work in this manner either.

    >>printf("\t",x);
    You are printing tabs only, not the actual number.

    I would suggest you lookup fgets() for getting a string from the user, that would be a darn sight easier than using getchar().

    Here's an example of a program to get the user name and display it. See what you can make of it. You can use it to also input numbers, which you will then need to convert to ints (a little more complicated, but not much )

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[BUFSIZ];
      
      printf ("Enter your name: ");
      if (fgets(buf, BUFSIZ, stdin) != NULL)
      {
        printf ("Hello %s", buf);
      }
      return 0;
    }
    Finally, please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Thanks for the information but as part of my assignment I have to use the gets() and getchar() fuctions even if they are old.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ....

    Thanks for the information but as part of my assignment I have to use the gets() and getchar() fuctions even if they are old.
    And why exactly?
    instead gets() use fgets(), it's better for you

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by ct26torr
    Thanks for the information but as part of my assignment I have to use the gets() and getchar() fuctions even if they are old.
    You're kiddin' aren't you... ??? gets is an old function and also a WRONG function.
    NEVER USE THIS FUNCTION !!!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, lame ass assignment then

    Try this
    Code:
    #include <stdio.h>
    
    char *GetString(char buf[], int buflen);
    
    int main(void)
    {
      char buf[BUFSIZ];
      
      printf ("Enter your name: ");
      printf ("Hello %s", GetString(buf, sizeof(buf)));
      
      return 0;
    }
    
    char *GetString(char buf[], int buflen)
    {
      int ch;
      char *p = buf;
    
      while ((ch = getchar()) != EOF && ch != '\n' && --buflen)
        *p++ = ch;
    
      *p = '\0';
      
      return buf;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    No I'm not kidding. It is the requiement for the assignment. I don't know why but it seems that is what I have to use.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, I've given the getchar() example. I'm sure you can work out the gets() implementation yourself.

    To convert the input obtained by gets() into a real number, you'll need something like strtol().

    Have fun
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Ok ao I have made a change and now my program seems to get stuck in a loop. It never increments to the next number like it is supposed to.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	/*Declare Variables*/
    	int x,y;
    	char name [50];
    
    	/*Get users name*/
    	printf("Enter your name:");
    	scanf("%s", &name);
    
    	printf("\nThis program will accept 2 numbers from you and print all the numbers"
    		"between them on the screen.\n");
    
    
    	/*Get values for x and y from user*/
    	printf("Enter an your starting number:");
    	scanf("%d", &x);
    	printf("Enter your ending number:");
    	scanf("%d", &y);
    
    	/*Loop through numbers and print*/
    	printf("\n Thank you %s,\n", name);
    	while (x<=y)
    		printf("\t%d",x);
    		++x;
    
    		
    getchar(); /*wait for key stroke to exit*/
    	
    
    }

  10. #10
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Where is it you are studying to get this sort of assignment?

  11. #11
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    Code:
    while (x<=y)
    		printf("\t%d",x);
    		++x;
    Should be..
    Code:
    while (x<=y) {
    		printf("\t%d",x);
    		++x;
    }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Ok ao I have made a change
    And now your using scanf() to get a string?! What happened to the "I must use gets()" thing? Like I said before, use fgets() to get a string. Maybe pay attention to what people are saying, eh?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I can't use fgets(). I was told the only other option I have is scanf(). My instructor for the class said gets() or scanf() only.

Popular pages Recent additions subscribe to a feed

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