Thread: return value

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    return value

    Scanf's return value is the number of specifiers that you give it.

    how can i make my program accept 2 integers and quit if it is a non - integer value. using scanf

    I've tried
    Code:
     while ( scanf("%d %d", &num, &num_max) == 2)
    
    
    ----body of code
    
    ---return (0);
    
    }
    However that still made the program terminate wrong.


    Here is my code:
    Code:
    /* numberSQUARED.c shows a integer, its squared and cubbed sequence in a given length*/
    
    #include <stdio.h>
    #include <stdlib.h>  /* system needed */
    
    
    int main(void)
    
    {
    
    
        int num;
        int ndx;
        int num_max;
    
    
        printf("Enter a min number and a max.");
        scanf("%d %d", &num, &num_max);
    
    
    
        /* format the screen */
    
        printf("num SQUARED CUBBED\n\n");
    
        /* output the results */
    
        for (ndx = num; ndx <= num_max; ndx++)
           {
               printf("%d %6d %6d", ndx, ndx * ndx, ndx * ndx * ndx);
        printf("\n");
    
    
        }
    
    
    
        printf("\n");
        system("pause");
        return (0);
    
    
    
    
    
    }

  2. #2
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    I don't think that it can be done like that. scanf will ( I think ) automatically convery doubles/floats/etc to ints if the user inputs one (like rounding).

    however, I'm not sure. don't pay much attention to me

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    read this page for all you need to know about scanf()
    http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?scanf+3

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks but not very helpful man.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You seemed to have all the bits, except your code didn't contain your while loop.

    > Scanf's return value is the number of specifiers that you give it.
    No it isn't - it's the number of conversion-assignments.

    If you have
    while ( scanf("%d %d", &num, &num_max) == 2)

    and you type in 123 456
    Then the return result will be 2

    if you type in 123 quit
    then the return result will be 1
    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.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by kinghajj
    I don't think that it can be done like that. scanf will ( I think ) automatically convery doubles/floats/etc to ints if the user inputs one (like rounding).

    however, I'm not sure. don't pay much attention to me
    Then why bother posting?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Why don't you use if>else statements?

    On another note, if your variables are type int you can automatically convert whatever they put to int with somehting like this


    number=(int)(number2);

    Or better yet use atoi functions..


    Hope that helps..

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    You seemed to have all the bits, except your code didn't contain your while loop.
    I swear to god, last night i included this statement and it didn't work. I must have been tired or something. Thanks for your help. Here is the desired code.
    Code:
    /* numberSQUARED.c shows a integer, its squared and cubbed sequence in a given length*/
    
    #include <stdio.h>
    #include <stdlib.h>  /* system needed */
    
    
    int main(void)
    
    {
    
    
        int num, num_max, ndx; /* first num, maximum number and ndx counter */
    
    
        printf("Enter a min number and a max.");
    
        while (scanf("%d %d", &num, &num_max)== 2){
    
               /* format the screen */
    
                printf("num SQUARED CUBBED\n\n");
    
                /* output the results */
    
                for (ndx = num; ndx <= num_max; ndx++)
                    {
                         printf("%d %6d %6d", ndx, ndx * ndx, ndx * ndx * ndx);
                         printf("\n");
                    }
    
    
    
               printf("\n");
               system("pause");
               return (0);
    
    
         }  /* end of while loop */
    
    
    return (0);
    
    
    }
    > Scanf's return value is the number of specifiers that you give it.
    No it isn't - it's the number of conversion-assignments.
    Isn't the correct term ' conversion specifier' ? That is what I meant in my original post.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >>>Scanf's return value is the number of specifiers that you give it.
    >>No it isn't - it's the number of conversion-assignments.
    >Isn't the correct term ' conversion specifier' ? That is what I meant in my original post.
    The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM