GetUserInfo function - c

I have an assignment where I have to print a users age and name based on their input.
Here is the code:
#include <stdio.h>
#include <string.h>
void GetUserInfo(int* userAge, char userName[]) {
printf("Enter your age: \n");
scanf("%d", userAge);
printf("Enter your name: \n");
scanf("%s", userName);
return;
}
int main(void) {
int userAge = 0;
char userName[30] = "";
//insert code here
printf("%s is %d years old.\n", userName, userAge);
return 0;
}
I can't change any code except for the //insert code here.
I'm pretty sure that I have to call the function but I can't figure out what parameters to pass the function. I tried
void GetUserInfo(int* userAge, char userName[]);
but as you can probably figure out it passes an empty string for a name and 0 as the age due to the initializations in the main().

The parameters you need for the function are a pointer to int and a pointer to char.
GetUserInfo(&userAge, userName);
The "&" operator gives you the address of the userAge variable that can be used as a pointer.
The compiler will take care of passing the start of the userName array of chars as second parameter.

Related

Why is the 2nd while-do loop not running in my function

I am working on CS50 and learning C. I am getting myself confused about utilizing while-do loops to ensure that the user puts in an integer. I am trying to create a function that prompts for your age and then your friend's age. However, after the first while-do loop returns false, shouldn't it move to the 2nd while-do loop in my get_ages() function? I would love some help on this.
#include <cs50.h>
#include <stdio.h>
int get_ages();
int main(void)
{
printf("%i\n", get_ages());
}
//Get your age function
int get_ages(int your_age, int friend_age)
{
do
{
your_age = get_int("What is your age: ");
return your_age;
}
while (your_age < 1);
do
{
friend_age = get_int("What is your friends age: ");
return friend_age;
}
while (friend_age < 1);
}
Your declaration int get_ages(); and your definition of the function int get_ages(int your_age, int friend_age) do not match. In main() you call get_ages() without arguments. If you want to pass data to caller via an argument (sometimes referred to as an out parameter) then you need to pass in the address of said variables:
int your_age;
int friend_age;
get_ages(&your_age, &friend_age);
The declaration would then be void get_ages(int *your_age, int &friend_age) you in the function you would set the value like this *your_age = get_int(...) etc.
return terminates a function and pass the optional value to caller. This means you will only ever execute a single return statement within a function.
In c, you can only return 1 value, so you need to return a struct or array if you have multiple things you want to return.
I suggest you change the function to just return a single value, pass in a prompt, then call the function twice:
#include <cs50.h>
#include <stdio.h>
int get_age(const char *prompt) {
int age;
do {
age = get_int(prompt);
} while (age < 1);
return age;
}
int main(void) {
printf("%i\n", get_age("What is your age? "));
printf("%i\n", get_age("What is your friend's age? "));
}

taking input using gets with arrow operator not working? What's the Error?

What's the Error in this ?
I tried taking input using arrow operator.
#include <stdio.h>
typedef struct{
char name[100];
int salary;
}emp;
void inp(emp *e){
printf("enter name : ");
gets(e->name);
printf("enter salary : ");
scanf("%d", e->salary);
}
int main() {
emp *e1,*e2;
inp(e1);
inp(e2);
printf("%s , %d\n", e1->name,e1->salary);
printf("%s , %d\n", e2->name,e2->salary);
return 0;
}
I tried putting & and even giving inp function as emp but it doesn't work.
It just asking me 1st employee name. dat's it!
Not even printing Enter salary.
What changes should I make?
Your struc have been created wrong. So i changed your code like this to make it clear. I also did use getchar() function so you can call fget() function multiplie times. I guess it does work like you wish.
#include <stdio.h>
struct Emp{
char name[100];
int salary;
};
void func(struct Emp *e){
printf("enter name : ");
fgets(e->name, sizeof(e->name), stdin);
printf("enter salary : ");
scanf("%d", &e->salary);
getchar();
}
int main() {
struct Emp e1,e2;
func(&e1);
func(&e2);
printf("\n%s , %d", e1.name,e1.salary);
printf("\n%s , %d", e2.name,e2.salary);
return 0;
}
emp *e1,*e2;
creates two pointers that point to nothing... They are just uninitialized pointers. They are not pointing to any valid memory. So when you dereference them using -> your program is likely to crash.
You need to allocate memory using, e.g. malloc
Like
e1 = malloc(sizeof *e1);
you should choose another better name for your function first, inp is already a function name in c, see this: https://cboard.cprogramming.com/c-programming/21255-ansi-c-function-inp-outp-inp-inpw.html

Passing a structure to a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So I am having a problem when compiling this program, I just can't get it to work, I mean if I put the inputstudent() code inside the main(), it is much easier but I have to place the code in a function, inputstudent() and call in from the main(). I know it sounds very easy but I can't get it.
#include <stdio.h>
#include <stdlib.h>
struct student
{
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent();
void inputstudent();
int main(){
struct student s;
inputstudent(s);
displaystudent(s);
return 0;
}
void inputstudent(struct student s){
printf("Enter the surname: ");
scanf("%s",s.surname);
printf("Enter the other name: ");
scanf("%s",s.oname);
printf("Enter the age: ");
scanf("%d",&s.age);
printf("Enter the address: ");
scanf("%s",s.address);
}
void displaystudent(struct student s)
{
printf("Surname: %s \n",s.surname);
printf("Oname: %s \n",s.oname);
printf("Age: %d \n",s.age);
printf("Address: %s",s.address);
}
In C parameters are passed by value, so any modifications made to a parameter inside the function will be local modifications.
Lets have a look at following code snippet which is basically a very simple version of what you're trying to do:
void GetNumber(int number)
{
printf("Type a number:\n");
scanf("%d", &number); // modifies the local variable `number`?
}
...
int n = 0;
GetNumber(n);
Now what is the value of n right after the call to GetNumber?
Well it's not the number the user has typed, but it's still 0, that is the value n contained prior to the call to GetNumber.
What you need is this:
void GetNumber(int *pnumber)
{
printf("Type a number:\n");
scanf("%d", pnumber); // modifies the value pointed by the pointer pnumber
}
...
int n = 0;
GetNumber(&n); // &n is the memory address of the variable n
You need to read the chapter dealing with pointers in your C textbook.
Other less important problem
Your prototypes
void displaystudent();
void inputstudent();
don't match the corresponding functions.
You are passing your struct by value, that's why the function is not modifying it. You should change your function that is intended to modify the struct to take a struct pointer as argument:
#include <stdio.h>
#include <stdlib.h>
struct student {
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent(struct student s);
void inputstudent(struct student *s);
int main() {
struct student s;
inputstudent(&s);
displaystudent(s);
return 0;
}
void inputstudent(struct student *s) {
printf("Enter the surname: ");
scanf("%s", s->surname);
printf("Enter the other name: ");
scanf("%s", s->oname);
printf("Enter the age: ");
scanf("%d", &s->age);
printf("Enter the address: ");
scanf("%s", s->address);
}
void displaystudent(struct student s) {
printf("Surname: %s \n", s.surname);
printf("Oname: %s \n", s.oname);
printf("Age: %d \n", s.age);
printf("Address: %s", s.address);
}
As mentioned by Michael Walz, use pointers in order to modify structure in function calls. Moreover your function signature and definition does not match that's why compiler is complaining:
#include <stdio.h>
#include <stdlib.h>
struct student {
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent(struct student* pStudent);
void inputstudent(struct student* pStudent);
int main() {
struct student aStudent;
inputstudent(&aStudent);
displaystudent(&aStudent);
return 0;
}
void inputstudent(struct student* pStudent){
printf("Enter the surname: ");
scanf("%s", pStudent->surname);
printf("Enter the other name: ");
scanf("%s", pStudent->oname);
printf("Enter the age: ");
scanf("%d", &pStudent->age);
printf("Enter the address: ");
scanf("%s", pStudent->address);
}
void displaystudent(struct student* pStudent)
{
printf("Surname: %s \n", pStudent->surname);
printf("Oname: %s \n", pStudent->oname);
printf("Age: %d \n", pStudent->age);
printf("Address: %s", pStudent->address);
}
You seem to want the changes that you make to the structure variable s inside inputstudent() to be reflected back to the original variable. In that case you need to pass the address of the variable to the function instead of its value.
If you pass the value of s to the function instead of its address, a new copy of s would be made inside inputstudent() and the values would be read into this copy while the s in main() remains unchanged.
To solve this you give inputstudent() a pointer to the s in main() and make inputstudent() use this address while reading the data. In this way the changes made for the variable in inputstudent() will be reflected back to the s in main().
Call the function like
inputstudent(&s);
And to access members of a structure variable using a pointer to it, you use the -> operator instead of the . operator.
void inputstudent(struct student *s){
printf("Enter the surname: ");
scanf("%s",s->surname);
printf("Enter the other name: ");
scanf("%s",s->oname);
printf("Enter the age: ");
scanf("%d",&s->age);
printf("Enter the address: ");
scanf("%s",s->address);
}
Also, an address possibly involves white spaces in which scanf() won't do. You could use fgets() for that.
There are basically two causes for your problem.
Function declarations don't have any parameters.
The structure is passed by value instead of reference to inputstudent function.
You can solve both of them by changing the function prototypes in both declaration and definition to
void displaystudent(struct student s);
void inputstudent(struct student &s);
You can't use struct student s as parameter in inputstudent(). It is just a value copy.
You should use pointer as parameter.
As:
void inputstudent(struct student* s)
{...}
int main(){
struct student s;
inputstudent(&s);
displaystudent(s);
return 0;
}

How to accept character array input into a structure in C?

I've got this structure, a simple one that holds student name and marks. When I'm trying to read user input into the name(char array), I get a warning indicating something on the lines of :
format %s expects char *, but has char*[20]
I know this is because char arrays cannot be assigned in C, so strcpy has to be used. This question on SO has a good reasoning. However,how do I fix the warning in my program? Don't think I can use strcpy here.
#include <stdio.h>
typedef struct _student
{
char name[20];
unsigned int marks;
} student;
void read_list(student list[], int SIZE);
void print_list(student list[], int SIZE);
int main()
{
const int SIZE=3;
student list[SIZE];
//function to enter student info.
read_list(list, SIZE);
//function to print student info
print_list(list, SIZE);
return 0;
}
void read_list(student list[], int SIZE)
{
int i;
char nm[20];
for (i=0;i<SIZE;i++)
{
printf("\n Please enter name for student %d\n", i);
scanf("%s",&list[i].name);
printf("\n Please enter marks for student %d\n", i);
scanf("%u", &list[i].marks);
}
}
void print_list(student list[], int SIZE)
{
int i;
printf("\t STUDENT NAME STUDENT MARKS\t \n");
for(i=0;i<SIZE;i++)
{
printf("\t %s \t %u\n", list[i].name, list[i].marks);
}
}
The program does give a correct output, but the warning remains.
Try this code:
for (i=0;i<SIZE;i++)
{
printf("\n Please enter name for student %d\n", i);
scanf("%s",list[i].name);
printf("\n Please enter marks for student %d\n", i);
scanf("%u", &list[i].marks);
}
This is because & used in scanf statement is to get the address.
In your case you use array name i.e. name and array name itself is providing the address. Remember array name gives the base address of an array.
change scanf("%s",&list[i].name); to scanf("%s",list[i].name);. Delete &. Because basically array name represents base address. No need to mention address of array for scanning the string.
Remove the & in the scanf that scan in a string using %s to eliminate the warning. So change
scanf("%s",&list[i].name);
to
scanf("%s",list[i].name);
This is because the name of the char array decays to a pointer to its first element
line 33:
scanf("%s",&list[i].name);//wrong
scanf("%s",list[i].name);//right
The name of an array is synonym for the location of the initial element , thus in your code, variable 'name' is the address of name[0]. You don't need to use & on 'name' to get the address of the array.Just use 'name' itself.

Function error 'expected expression before char'?

I have created the following program which allows a user to guess a word 3 times before ending the program. I'm using a function to read the users input. When I compile the program I get the error 'expected expression before char'. Some feedback would be great thanks!
#include <stdio.h>
#include <string.h>
void get_user_input(char *guess[10]);
void get_user_input(char *guess[10])
{
printf("Please guess the word: \n");
scanf("%s", guess);
}
int main(void)
{
const char secret[10] = "pink";
char guess[10];
int i;
for (i=0; i < 3; i++)
{
get_user_input(char *guess[10]);
if (strcmp(secret, guess)==0)
{
printf("Your guess was correct");
return 0;
}
else
{
printf("Your guess was incorrect. Please try again\n");
}
}
return 0;
}
You have an extra char here:
for (i=0; i < 3; i++)
{
get_user_input(char *guess[10]);
Just get rid of it. You just need to pass the variable in.
get_user_input(guess);
EDIT :
The other problem seems to be this function:
void get_user_input(char *guess[10]);
change it to this:
void get_user_input(char *guess)
{
printf("Please guess the word: \n");
scanf("%s", guess);
}
and it should work. However, be aware that you run the risk of overrunning your guess array.
Inside the loop, write
get_user_input(guess);
instead of
get_user_input(char *guess[10]);
.
In addition, you should delete the useless prototype
void get_user_input(char *guess[10]);
and change the following function's signature to
void get_user_input(char * guess)
to let a pointer to the first char of the array be passed instead of a pointer to a pointer to the first char which will not compile. A side issue is that char *guess[10] means an array of 10 pointers to char.
PS: It helps to post the offending line number in addition to the error message.
PPS: You have a buffer overrun memory error if the use enters long answers. You can use fgets to avoid this.

Resources