input from scanf into structure, how?

This is a discussion on input from scanf into structure, how? within the C Programming forums, part of the General Programming Boards category; Hi, I am making a simple program that asks the user to input 2d points and the program will perform ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    Question input from scanf into structure, how?

    Hi, I am making a simple program that asks the user to input 2d points and the program will perform various functions.
    This is what I have so far

    Code:
    #include <stdio.h>
    
    struct point2D
    {
    	float x;
    	float y;
    };
    
    int main()
    {
    	point2D pa,pb;
    
    	printf("Please enter the first point: ");
    	scanf("%f" "%f", &pa.x, &pa.y);
    	printf("Please enter the second point: ");
    
    	return(0);
    }
    When i enter the first point like so 5 32
    i get a runtime error and it says floating point is not loaded.
    Am I using the structure wrong or is it the scanf?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always do a forum search for the message, or perhaps look at what the first hit on a Google search gives you.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    you need to declare insances of the structure. You have correctly created the structure, but need instances.

    So....

    Code:
    struct point2d pa;
    struct point2d pb;
    you can then remove the line

    Code:
    point2D pa,pb;

    Also change the

    Code:
    scanf("%f" "%f".......
    to

    Code:
    scanf("%f %f"......
    Last edited by computerfreaks; 03-04-2005 at 07:18 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, no you don't have to. Two string literals will be concatenated into a single literal at compile time. Furthermore, they have declared an instance of their structure, or at least if they're compiling as C++ they have:
    Code:
    struct point2D
    {
    	float x;
    	float y;
    };
    
    int main()
    {
    	point2D pa,pb;
    In C they need to simply add the keyword struct to the start of that line.

    Neither of these issues are really what they're posting about however.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    I stand corrected..... however if this is c++ then surely the message should be posted in the c++ forum??

    So are you saying that

    scanf("%f""%f", &var1, &var2);

    is accepted? ok... just tested and it is, but still, it seems like bad practice to put this...

    therefore the code should be (if C ):

    Code:
    #include <stdio.h>
    
    struct point2D
    {
    	float x;
    	float y;
    };
    
    int main()
    {
    	struct point2D pa,pb;
    
    	printf("Please enter the first point: ");
    	scanf("%f" "%f", &pa.x, &pa.y);
    	printf("Please enter the second point: ");
    	
    	system("pause");
    
    	return(0);
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, it should be compiled as C. However, since they didn't say they were getting any other errors, such as:
    suchas.c: In function `main':
    suchas.c:11: error: `point2D' undeclared (first use in this function)
    suchas.c:11: error: (Each undeclared identifier is reported only once
    suchas.c:11: error: for each function it appears in.)
    suchas.c:11: error: parse error before "pa"
    suchas.c:14: error: `pa' undeclared (first use in this function)
    Then it's obvious they're compiling as C++ and not C. But again, that has nothing to do with their floating point problem.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    this form seem clearer to me.
    Code:
    #include <stdio.h>
    
    typedef struct 
    {
    	float x;
    	float y;
    }point2D;
    
    int main()
    {
    	point2D pa;
    
    	printf("Please enter the first point: ");
    	scanf("%f" "%f", &pa.x, &pa.y);
    	printf("Please enter the second point: ");
    	
    	system("pause");
    
    	return(0);
    }
    still, it gives same runtime error .

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by ronenk
    still, it gives same runtime error .
    Wrong band-aid:
    http://cboard.cprogramming.com/showthread.php?p=322171

    [Which is the same thing you'll find if you follow quzah's link.]
    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.*

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > scanf("%f""%f", &var1, &var2);
    > is accepted?
    Yes, the compiler will automatically join "hello " "world" into "hello world" for you.

    Not a lot of use here, but dead handy if you're trying to create some help text and you don't want your source code to look a mess.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 03:54 PM
  2. Reading a string from an input file into a structure
    By Maxwell25 in forum C++ Programming
    Replies: 15
    Last Post: 05-01-2003, 10:30 AM
  3. Input stream directly to structure elements?
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2003, 11:43 AM
  4. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 08:21 PM

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