ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Here is the problem: I'm trying to exercise on binary trees in C, and I've made the following function that would create a binary tree:
Code:
NODE * create(void)
{NODE *p;
int f;
//system("cls");
if (idform==1000)
{printf("THE ROOT");}
printf("\n\nDo you want to create? (1/0)\nAswer: ");
scanf("%i",&f);
if (!f) return NULL;
p=(NODE*) malloc (sizeof(*p));
//system ("cls");
printf("\nEnter info:\n\n");
printf("\nAn id for the node will be generated automatically.\n");
p->IDindex=idform;
printf("The ID is: %i",p->IDindex);
printf("\nEnter info of the node: \n");
printf ("Model: ");
fflush(stdin);
gets(p->model);
printf ("Country: ");
fflush(stdin);
gets(p->country);
fflush(stdin);
printf ("Date of production: ");
fflush(stdin);
gets(p->date);
printf ("Horsepower: ");
scanf("%d",&p->hp);
printf("Price : ");
scanf("%d",&p->price);
idform++;
printf("\n\nLeft child for node %i",p->IDindex);
p->left=create();
printf("\n\nRight child for node %i",p->IDindex);
p->right=create();
return p;
}
The error that I get is that always the "model" field cannot be introduced from keyboard. This is what I get when running the soft:
Code:
Do you want to create? (1/0)
Aswer: 1
Enter info:
An id for the node will be generated automatically.
The ID is: 1000
Enter info of the node:
Model: Country: 2
Date of production: 2
Horsepower: 2
Price : 2
just a thought...
C is crap for user interfaces.
why don't you leave the user input out?
make a shell wrapper, or even a wish script, and pass the input in from that.
it's a lot easier to format the input correctly first then pass it in.
Never ever ever use gets(), because it doesn't allow you to specify the maximum number of bytes in your input. Exploiting buffer overflow bugs (and the use of gets() is always a bug) is the leading cause of malware and also of tooth decay.
I don't want to disagree with either of the first two answers. Both make good points. But I don't see the answer to the actual question in there anywhere.
I'm not sure myself the exact rules for when output from printf actually reaches stdout vs. when it is buffered. (I don't use printf much). But the quoted program output makes it look like the prompt/input is out of sync by one position because of that buffering.
Maybe you need to fflush(stdout) after any printf that you want displayed immediately.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.