Thread: puts() and gets() function problems

  1. #1
    Unregistered
    Guest

    Question puts() and gets() function problems

    I'm trying to read input from the keyboard without using scanf, and print to the screen without using printf. I'm not having much luck. Any sugguestions.

    thanx

  2. #2
    Unregistered
    Guest
    forgot to insert code. Lets try this again

    #include<stdio.h>
    #define MIN -10
    #define MAX 10

    int max(int val1, int val2);
    int min(int val1, int val2);

    int main()
    {
    int num1;
    int num2;
    int maxval;
    int minval;

    puts("Enter two numbers between -10 and 10.");
    gets(num1);
    gets(num2);

    while ((num1 > MAX) || (num1 < MIN) || (num2 > MAX) || (num2 < MIN));
    {
    puts("Invalid input, please enter a number between -10 and 10!");
    gets(num1);
    gets(num2);
    }

    maxval = max(num1, num2);
    puts("The maximum number is : ", maxval);

    minval = min(num1, num2);
    puts("The minimum number is : ", minval);

    return 0;
    }

    int max(int val1, int val2)
    {
    return((val1 > val2) ?val1 : val2);
    }

    int min(int val1, int val2)
    {
    return((val1 < val2) ?val1 : val2);
    }



    #include<stdio.h>
    #define MIN -10
    #define MAX 10

    int max(int val1, int val2);
    int min(int val1, int val2);

    int main()
    {
    int num1;
    int num2;
    int maxval;
    int minval;

    puts("Enter two numbers between -10 and 10.");
    gets(num1);
    gets(num2);

    while ((num1 > MAX) || (num1 < MIN) || (num2 > MAX) || (num2 < MIN));
    {
    puts("Invalid input, please enter a number between -10 and 10!");
    gets(num1);
    gets(num2);
    }

    maxval = max(num1, num2);
    puts("The maximum number is : ", maxval);

    minval = min(num1, num2);
    puts("The minimum number is : ", minval);

    return 0;
    }

    int max(int val1, int val2)
    {
    return((val1 > val2) ?val1 : val2);
    }

    int min(int val1, int val2)
    {
    return((val1 < val2) ?val1 : val2);
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    gets() stands for get string not get int.

    so use gets() or better still fgets() to get a string from stdin into a char array. Pass that array to atoi() to get an integer.You cannot gets() an int the way you have tried.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fread()
    fwrite()

    It doesn't get much better than that.

    Quzah.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    #include<stdio.h>
    #define MIN -10 
    #define MAX 10 
    
    int max(int val1, int val2);
    int min(int val1, int val2);
    
    int main() 
    { 
    int num1; 
    int num2; 
    int maxval; 
    int minval; 
    
    puts("Enter two numbers between -10 and 10."); 
    fscanf(stdin, "%d %d", &num1, &num2);
    /*you had syntax error ; at end of while */
    while(num1 > MAX || num1 < MIN ||num2 > MAX || num2 < MIN)
    { 
    puts("Invalid input, please enter a number between -10 and 10!"); 
    fscanf(stdin, "%d %d", &num1, &num2);
    }
    
    maxval = max(num1, num2); 
    fprintf(stderr,"\nThe maximum number is :%d ", maxval);
    /* you had no format specifier for*/
    /* maxval and minval in statments */
    minval = min(num1, num2); 
    fprintf(stderr,"\nThe minimum number is :%d ", minval);
    
    return 0; 
    } 
    
    int max(int val1, int val2) 
    {
    
      return  val1 > val2? val1:val2;
     }
    
    int min(int val1, int val2) 
    {
    
    return val1 < val2? val1:val2;
    
    }
    Last edited by bigtamscot; 10-22-2001 at 07:50 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > Pass that array to atoi() to get an integer.

    Or, you could do the fgets and sscanf combo to get an int. It would look something like this:
    Code:
    char line[50];
    int num;
    
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &num);
    I prefer this. But, it is your choice.

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    fgets and sscanf is a good choice. In addition you can also validate the return value of sscanf to see if the user input is appropriate for the data type.

Popular pages Recent additions subscribe to a feed