C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 03:12 PM
computermajor12 computermajor12 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 computermajor12 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 0
First time using C

It's my first time using C and I'm doing an assignment which I have a structure that holds information and a couple of functions that use that information:
My structure is for a person and each person has a name, age, and gpa.
All three (name,age,gpa) are defined by user input, which is one of my functions : fill_person.
I'm having trouble running it because its saying that the name, age and gpa are not defined but it's in my function to have them defined by user input.
Here's my code:
Code:
  1 #include<stdio.h>
  2 struct Person
  3 {
  4     char name[100];
  5     int age;
  6     float gpa;
  7 
  8 };
  9     void fill_person(struct Person* per)
 10     {
 11       printf("Enter name of student:");
 12       fgets("%c",100, &name);
 13       printf("Enter age of student:");
 14       scanf("%d", &age);
 15       printf("Enter GPA of student:");
 16       scanf("%f", &gpa);
 17 
 18     }
 19     void show_person(struct Person* per)
 20    {
 21       fgets("%c", per->name);
 22       printf("%d", per->age);
 23       printf("%f", per->gpa);
 24    }
 25 
 26 
 27 
 28 int main()
 29 {
 30 }


This is the error I'm getting:
Code:
assignment12.c: In function 'fill_person':
assignment12.c:12: error: 'name' undeclared (first use in this function)
assignment12.c:12: error: (Each undeclared identifier is reported only once
assignment12.c:12: error: for each function it appears in.)
assignment12.c:14: error: 'age' undeclared (first use in this function)
assignment12.c:16: error: 'gpa' undeclared (first use in this function)
assignment12.c: In function 'show_person':
assignment12.c:21: warning: passing argument 2 of 'fgets' makes integer from pointer without a cast
/usr/include/stdio.h:604: note: expected 'int' but argument is of type 'char *'
assignment12.c:21: error: too few arguments to function 'fgets'

I don't get how I'm getting this error because I'm having all three variables in my structure that are being used in the function be defined by user input..
Any help with what is wrong with program??

Reply With Quote
  #2  
Old November 5th, 2012, 03:15 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Inside fill_person(), you want to use the pointer like you did inside show_person.

per->name, for instance, or &per->age

Reply With Quote
  #3  
Old November 5th, 2012, 04:25 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
Quote:
Originally Posted by bdb
&per->age


Except you don't want that ampersand there.
Edit: I thought you wrote &per->name there sorry about that

The problem is that name, age, and gpa aren't declared in fill_person. Those things are declared in your Person struct. Your compiler thinks you're trying to scanf into variables local to the fill_person function. You need to pass a pointer to a Person, which contains those fields, to your function fill_person. As bdb was saying, you need to use the member select operator "->" to say where you're getting those fields from, which in this case is your pointer per.

Your compiler can't, or rather shouldn't, simply guess that you mean per->name or per->gpa as opposed to some local variable called name or gpa when you just say name or gpa and the & operator which you're misusing doesn't magically accomplish this distinction. If you had local variables called name, age, and gpa like you did in your person struct then it would make sense to do the scanfs the way you're doing, but that would require extra code to then transfer that data to the struct you're passing to your function on top of those extra declarations.

Use it like this:

(a pointer to a struct with the fields you want to scanf into)
->
(the field of that struct you want to scanf into)

instead of &(the field of that struct you want to scanf into) which makes no sense.

Last edited by jakotheshadows : November 5th, 2012 at 06:20 PM.

Reply With Quote
  #4  
Old November 5th, 2012, 04:31 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
I'm also not sure what you're trying to do with fgets, perhaps you should explain what you think it does. I also suggest you read this: fgets

Reply With Quote
  #5  
Old November 5th, 2012, 04:38 PM
computermajor12 computermajor12 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 computermajor12 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 0
Thank you... Now that I fixed that error when I try to compile it I still get this error:
Code:
assignment12.c: In function 'fill_person':
assignment12.c:12: warning: passing argument 2 of 'fgets' makes integer from pointer without a cast
/usr/include/stdio.h:604: note: expected 'int' but argument is of type 'char *'
assignment12.c:12: error: too few arguments to function 'fgets'
assignment12.c: In function 'show_person':
assignment12.c:21: warning: passing argument 2 of 'fgets' makes integer from pointer without a cast
/usr/include/stdio.h:604: note: expected 'int' but argument is of type 'char *'
assignment12.c:21: error: too few arguments to function 'fgets'

clearly all my error is in my function fill_person and show_person but I'm not whats wrong with them

fgets:
My teacher told us to use fgets, and after looking it up I thought that it was what you used for input when using a char variable.

Reply With Quote
  #6  
Old November 5th, 2012, 04:44 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
First of all, you're reading in several characters into an array of characters. Second, pay attention to the number of arguments fgets takes, and what they are supposed to be. Read the link I gave you and report back on what you learn.

Hint: the order of the arguments matters.

Reply With Quote
  #7  
Old November 5th, 2012, 05:10 PM
computermajor12 computermajor12 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 computermajor12 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 0
fgets

Quote:
Originally Posted by jakotheshadows
First of all, you're reading in several characters into an array of characters. Second, pay attention to the number of arguments fgets takes, and what they are supposed to be. Read the link I gave you and report back on what you learn.

Hint: the order of the arguments matters.


So I read the fgets reference you posted and from what I understand fgets passes 3 arguments and for me its char name, 100, and per.
so I changed my code:
Code:
  1 #include<stdio.h>
  2 struct Person
  3 {
  4     char name[100];
  5     int age;
  6     float gpa;
  7 
  8 };
  9     void fill_person(struct Person* per)
 10     {
 11       printf("Enter name of student:");
 12       fgets("%c",100, per->name);
 13       printf("Enter age of student:");
 14       scanf("%d", per->age);
 15       printf("Enter GPA of student:");
 16       scanf("%f", per->gpa);
 17 
 18     }
 19     void show_person(struct Person* per)
 20    {
 21       fgets("%c",100, per->name);
 22       printf("%d", per->age);
 23       printf("%f", per->gpa);
 24    }
 25 
 26 
 27 
 28 int main()
 29 {
 30 }

this code still will not compile for me

Reply With Quote
  #8  
Old November 5th, 2012, 05:57 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
Quote:
Originally Posted by computermajor12
So I read the fgets reference you posted and from what I understand fgets passes 3 arguments and for me its char name, 100, and per.


No. You're right about the first two, but wrong about the third. You're also not doing what you say you're doing in your code.

You say you're passing name as the first argument, which is what you need to be doing as long as you understand the previous point I've made about how to access name and how that applies to passing it to a function, but you aren't doing that in your code. What you're passing is "%c" which is a string literal and is completely wrong.

You seem to think for some reason that fgets uses format specifiers like %c, and that a string literal like "anythinginquotes" will do anything you want it to do, but it doesn't.

Your 2nd argument is correct, keep that part as is.

You apparently completely skipped the reading part on the third argument. Reread that and don't make any more assumptions. If there is something in the reading part that you don't understand let us know. I'm only going to meet you half way on this it is your homework and you have to read the manuals on the functions you're using if you don't understand how they work.

Reply With Quote
  #9  
Old November 5th, 2012, 06:23 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
I misread bdb's post with regard to the &. You do need & in front of per->age and per->gpa for your scanfs like he said.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > First time using C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap