Thread: Defining Integers Etc.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Defining Integers Etc.

    Hello,

    I am sure you heard a billion questions regarding homework and i do not intend on people doing it for me but i am STUCK. I know its a simple problem but i have not been able to figure it out here is my assignment.

    Write a simple program that will

    - define an integer variable myint

    - define a character variable mychar

    - Define a floating point variable myfloat

    - The program should use printf/scanf pairs to describe the type of data to be input, then input the data values in the order defined above (int, char, float).
    Code:
    /* Defines the Variables Int Char and Float */
    
    #include <stdio.h>
    
    int main(void)
    {
        /* The three variables we are going to identify*/
        
         int myint, mychar, myfloat ;
       
        /*Identifies a number*/
       
        printf("Enter a number: ");
        scanf("%d", &myint);
       
        /*Identifies a character*/
       
        printf("Enter a character: ");
        flushall();     /* Flushes the memory for the next input*/
        scanf("%c", &mychar);
       
        /* Identifies a float*/
        
        printf("Enter a float: ");
        scanf("%f", &myfloat);
    
    }
    This is what i got but for some reason when i do my flushall(); statement it gives me flushall() "undeclared"
    Last edited by dayved; 03-12-2010 at 07:56 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why are you declaring all variables as type int?
    One should be type int, one should be type float and the other one should be type char.

    There is no need to use flushall. Remove that line.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Code:
      #include <stdio.h>
    
    int main(void)
    {
        /* The three variables we are going to identify*/
    
         int myint;
         int mychar[2];
         float myfloat ;
    
        /*Identifies a number*/
    
        printf("Enter a number: ");
        scanf("%d", &myint);
    
        /*Identifies a character*/
    
        printf("Enter a character: ");
        getchar();
        scanf("%c", &mychar);
    
        /* Identifies a float*/
    
        printf("Enter a float: ");
        scanf("%f", &myfloat);
    
        printf("%d === %c === %f\n",myint,mychar,myfloat);
    }
    The problem happening while reading the char .Because usually scanf will read the input upto '\n'.so when you read the integer the '\n' will remain in the buffer.When you try to read a character that '\n' comes in to play .To avoid that we can use getchar() to take that '\n' character . then you can read the character using scanf .

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    There is no standard flushall() function, but guessing by its name, it is probably useful here. The problem is that after entering an integer, the user hits enter. The scanf("%c") will grab the newline, not allowing the user to enter a character (this is the source of the common-to-new-programmers problem of “my getchar() is being skipped”). flushall() presumably will “clear” standard input, removing the newline.

    Such a call isn't needed before %f because %f will eat leading whitespace.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    printf("Enter a number: ");
    scanf("%d", &myint);

    You're not checking if scanf was succesfull.

    What if i enter "bad code" and then enter? myint wont receive a number, character will be 'b' and float will fail aswell.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Quote Originally Posted by karthigayan View Post
    Code:
      #include <stdio.h>
    
    int main(void)
    {
        /* The three variables we are going to identify*/
    
         int myint;
         int mychar[2];
         float myfloat ;
    
        /*Identifies a number*/
    
        printf("Enter a number: ");
        scanf("%d", &myint);
    
        /*Identifies a character*/
    
        printf("Enter a character: ");
        getchar();
        scanf("%c", &mychar);
    
        /* Identifies a float*/
    
        printf("Enter a float: ");
        scanf("%f", &myfloat);
    
        printf("%d === %c === %f\n",myint,mychar,myfloat);
    }
    The problem happening while reading the char .Because usually scanf will read the input upto '\n'.so when you read the integer the '\n' will remain in the buffer.When you try to read a character that '\n' comes in to play .To avoid that we can use getchar() to take that '\n' character . then you can read the character using scanf .
    Code:
    printf("Enter a character: ");
        getchar();
        scanf("%c", &mychar);
    This will result in compiling error. You're passing a pointer to pointer to scanf.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Tool View Post
    Code:
    printf("Enter a character: ");
        getchar();
        scanf("%c", &mychar);
    This will result in compiling error. You're passing a pointer to pointer to scanf.
    only if compiler is smart... On regular compiler it just will crash at runtime... or some other unpredictable result...
    %c expects pointer to char, so it is bad idea to pass anything else, even pointer to int...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Thanks for the help!

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Well i did well on that but when i print out the variables i get a different character then i input. This is the code i used. Weirdd

    Code:
      #include <stdio.h>
    
    int main(void)
    {
        /* The three variables we are going to identify*/
    
         int myint;
         int mychar[2];
         float myfloat ;
    
        /*Identifies a number*/
    
        printf("Enter a number: ");
        scanf("%d", &myint);
    
        /*Identifies a character*/
    
        printf("Enter a character: ");
        getchar();
        scanf("%c", &mychar);
    
        /* Identifies a float*/
    
        printf("Enter a float: ");
        scanf("%f", &myfloat);
    
        printf("myint = %d\n",myint );
        printf("mychar = %c\n",mychar) ;
        printf("myfloat = %f",myfloat);
        
        getch();
    }

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I think it's weird that you haven't fixed the problems pointed out to you, but you expect the code to work anyway.

    getchar() discards a character. scanf("%c", &mychar) reads a subsequent character from the input. mychar also needs to be of type char, not an array of two integers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Quote Originally Posted by grumpy View Post
    I think it's weird that you haven't fixed the problems pointed out to you, but you expect the code to work anyway.

    getchar() discards a character. scanf("%c", &mychar) reads a subsequent character from the input. mychar also needs to be of type char, not an array of two integers.
    So should i remove the pointer getchar()? I was told to put that in instead of flushing the previous input..

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To answer your question: maybe. It depends on what assumptions you can make about the previous input. If you assume a rational user, you may not need the getchar() call. With a more typical user, calling getchar() may be either insufficient or simply a completely wrong response. Try it and see, with a range of inputs.

    Just stringing together a bunch of random things you've been told, without really understanding how they relate to what you're trying to do, is a good way to get code that behaves in strange manners.

    The type of mychar is still a significant concern. And getchar() is a function call (or maybe a invocation of a macro) not a pointer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    - define a character variable mychar
    I still don't see a char variable in there. Looks like you're stuck at step 2.
    I can't fathom where you got the idea that you need arrays yet.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    I can't fathom where you got the idea that you need arrays yet.
    I see such things fairly often with novices who incorrectly believe a pointer and an array are the same thing.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by grumpy View Post
    I see such things fairly often with novices who incorrectly believe a pointer and an array are the same thing.
    But, his assignment never even asked for a pointer or an array so it baffles the mind why he is defining one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM