"Why does this not compile?"

int main(){
	struct people{
    
		char name[20];
		short age;

	} people;

struct people me;



cout<<"Write your name"<<endl;
gets(me.name);

cout<<"How old are you"<<endl;
gets(me.age);//error
}

Recommended Answers

All 8 Replies

This requires a c string

gets(me.age);//error

not a short.

Here's gets prototype

char *gets(char *s);

Also gets is a dangerous function that should never be used..Here's a short blurb on why.

Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

The functions you should be using are fgets() and fscanf

What can I write in:

fgets(me.name,sizeof(me.name),)//?

What should age be:

1short
2char
3int

What can I write in:

fgets(me.name,sizeof(me.name),)//?

What should age be:

1short
2char
3int

Hi Alex,

gets - get a string from a stdin stream not a integer value.Better use short or short int for age variable since this has the least range other data type. Use cin to read the age variable.

The below code will help you....

#include<stdio.h>
#include <iostream>

using namespace std;


int main()
{
	struct people
	{
 
		char name[20];
		short age;
 
	} people;
 
struct people me; 
 
cout<<"Write your name"<<endl;
cin>>me.name; 
cout<<"How old are you"<<endl;
cin>>me.age;

cout<<"THe size is"<<sizeof(me.age);

cout<<"Name is :"<<me.name;
cout<<endl<<"THe age is"<<me.age;
return;
}

Better use short or short int for age variable since this has the least range other data type.

Shorter than char? I don't think so...

Use cin to read the age variable.

You realize this is the C forum, right? That code can't possibly help.

Shorter than char? I don't think so...


You realize this is the C forum, right? That code can't possibly help.

Ya char is shorter than short int....

"Why does this not compile?"

int main(){
	struct people{
    
		char name[20];
		short age;

	} people;

struct people me;



cout<<"Write your name"<<endl;
gets(me.name);

cout<<"How old are you"<<endl;
gets(me.age);//error
}

Hi Alex,

The below code will help you,

#include<stdio.h>

int main()
{
	struct people
	{
		char name[20];
		short age;
 
	} people;

struct people me; 
printf("\nEnter ur name");
scanf("\n%s",me.name);
printf("\nEnter number");
scanf("%hd",&me.age);

printf("\nTHe size of short int is : %d",sizeof(me.age));

printf("\nName is : %s",me.name);
printf("\nTHe age is %hd",me.age);
return 0;
}
int main(){
	struct people{
    
		char name[20];
		short age;

	} people;

struct people me;



printf("\n Write your name:");

fgets(me.name,20,stdin);

printf("\nHow old are you:");

scanf("%d",&me.age);

return 0;
}

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.