LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2009, 01:43 AM   #1
dracuss
Member
 
Registered: May 2006
Location: Chisinau, Moldova
Distribution: Gentoo, Debian sid
Posts: 151

Rep: Reputation: 29
Error with GCC


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
 
Old 04-02-2009, 03:44 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I wouldn't use scanf.
use gets and convert.

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.
 
Old 04-02-2009, 05:48 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
danger danger danger danger danger

Quote:
Originally Posted by bigearsbilly View Post
I wouldn't use scanf.
I agree wholeheartedly.
Quote:
Originally Posted by bigearsbilly View Post
use gets and convert.
*facepalm*

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.

Never ever. Instead, use fgets().
 
Old 04-02-2009, 07:52 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 04-02-2009, 08:12 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
As the thread title indicates, the problem is with GCC.

@ OP

Fix your initial scanf() to discard unwanted characters, or in your case, a newline.
Code:
  printf("\n\nDo you want to create?  (1/0)\nAswer: ");
  scanf("%i%*c", &f);
  ...
When reading a string, use fgets().
Code:
fgets(str, sizeof(str), stdin);
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile error, 'H5G_obj_t' does not name a type, is gcc 4.3 incompatible with gcc 3 ? samrat_rao Linux - Software 5 03-03-2009 02:28 PM
Error in compiling gcc-3.3.3 with gcc-4.3.2 asahlot Linux - Software 6 02-08-2009 11:25 AM
pkg-get install gcc ERROR: gcc unrecognized Raidmax Solaris / OpenSolaris 10 08-30-2007 12:51 PM
have gcc compilation error during gcc installtion in linuxfromscratch vbshanmugaprakash Linux - General 3 12-13-2006 05:52 AM
Error compiling gcc 3.3.6 with 64bit gcc 4.0.3 cs-cam Linux - Software 0 04-22-2006 05:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration