banner



How To Use Scanf With Struct

  1. #1

    Elite is offline

    Registered User


    Question input from scanf into structure, how?

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

    Lawmaking:

    #include <stdio.h>  struct point2D { 	float x; 	bladder y; };  int main() { 	point2D pa,atomic number 82;  	printf("Please enter the first point: "); 	scanf("%f" "%f", &pa.x, &pa.y); 	printf("Please enter the 2d indicate: ");  	return(0); }
    When i enter the first point like so five 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. #ii

    quzah is offline

    ATH0 quzah's Avatar


    You could always exercise a forum search for the bulletin, or peradventure look at what the first hit on a Google search gives yous.

    Quzah.

    Hope is the outset step on the route to thwarting.


  3. #3

    computerfreaks is offline

    Registered User computerfreaks's Avatar


    you need to declare insances of the structure. You have correctly created the construction, but need instances.

    And then....

    Code:

    struct point2d pa; struct point2d lead;
    yous can so remove the line
    Also change the

    Code:

    scanf("%f" "%f".......
    to

    Code:

    scanf("%f %f"......
    Terminal edited by computerfreaks; 03-04-2005 at 08:eighteen AM.


  4. #four

    quzah is offline

    ATH0 quzah's Avatar


    Really, no you don't have to. 2 string literals volition be concatenated into a single literal at compile time. Furthermore, they take alleged an example 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,lead;                      
    In C they need to just add the keyword struct to the beginning of that line.

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

    Quzah.

    Promise is the starting time pace on the road to disappointment.


  5. #5

    computerfreaks is offline

    Registered User computerfreaks's Avatar


    I stand corrected..... however if this is c++ and so surely the bulletin should exist posted in the c++ forum??

    So are you maxim that

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

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

    therefore the code should exist (if C ):

    Lawmaking:

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


  6. #6

    quzah is offline

    ATH0 quzah's Avatar


    Sure, it should be compiled as C. However, since they didn't say they were getting any other errors, such as:

    suchas.c: In office `master':
    suchas.c:11: error: `point2D' undeclared (kickoff apply in this role)
    suchas.c:eleven: error: (Each undeclared identifier is reported only once
    suchas.c:xi: error: for each function information technology appears in.)
    suchas.c:11: error: parse error before "pa"
    suchas.c:14: fault: `pa' undeclared (first use in this function)

    And so it'southward obvious they're compiling as C++ and not C. But again, that has nix to do with their floating bespeak problem.

    Quzah.

    Hope is the get-go stride on the road to disappointment.


  7. #seven

    ronenk is offline

    Registered User


    this form seem clearer to me.

    Lawmaking:

    #include <stdio.h>  typedef struct  { 	float x; 	float y; }point2D;  int main() { 	point2D pa;  	printf("Please enter the first point: "); 	scanf("%f" "%f", &pa.ten, &pa.y); 	printf("Please enter the second point: "); 	 	system("pause");  	return(0); }
    yet, it gives same runtime error .


  8. #8

    Dave_Sinkula is offline

    Just Lurking Dave_Sinkula's Avatar


    Quote Originally Posted by ronenk

    notwithstanding, it gives same runtime error .

    Wrong ring-aid:
    https://cboard.cprogramming.com/showthread.php?p=322171

    [Which is the same thing y'all'll discover if you follow quzah's link.]

    7. It is easier to write an incorrect program than empathise a right one.
    twoscore. At that place are two ways to write error-costless programs; only the third i works.*


  9. #9

    Salem is offline

    and the hat of int overfl Salem's Avatar


    > scanf("%f""%f", &var1, &var2);
    > is accepted?
    Yes, the compiler will automatically join "how-do-you-do " "world" into "howdy world" for you.

    Non a lot of use here, but dead handy if y'all're trying to create some assistance text and yous don't want your source code to await a mess.


How To Use Scanf With Struct,

Source: https://cboard.cprogramming.com/c-programming/62606-input-scanf-into-structure-how.html

Posted by: ratliffpeammeak.blogspot.com

0 Response to "How To Use Scanf With Struct"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel