LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-24-2004, 05:46 AM   #1
shams
Member
 
Registered: Jan 2004
Posts: 535

Rep: Reputation: 30
Unhappy error coding structure in c


hi,
this is the code for a structure:
#include <stdio.h>
main()
{
char name[3];
float price[3];
int pages[3], i;

printf("\nEnter names,prices and no. of pages of 3 books\n");
for(i=0;i<=2;i++)
scanf("%c%f%d",&name[i],&price[i],&pages[i]);

printf("\nAnd this is what you entered\n");
for(i=0;i<=2;i++)
printf("%c%f%d\n",name[i],price[i],pages[i]);
}
but the output of this program after entering the name price and pages of
3 books is garbage.plz point me where is the mistake.
 
Old 08-24-2004, 06:13 AM   #2
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
The two most evident mistakes which i find in your code are

1)use char* name[3] instead of char name[3]

2)you are going out of bounds in ur loops with test condition

i<=2

it should be i<2

i am evaluting the code i ll tell you the other mistakes in a moment
 
Old 08-24-2004, 06:14 AM   #3
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
ah my mistake the loop thing is absolutely fine

the array size if 3 and ur condition is i<=2 which is right
 
Old 08-24-2004, 06:26 AM   #4
linuxman2k1
LQ Newbie
 
Registered: Nov 2003
Location: Baltimore, Maryland
Distribution: LFS 5.0
Posts: 17

Rep: Reputation: 0
this would be another way of doing it:

struct book{
char name[50]; // you can use a book name instead of just 1 char like yours had
float price;
int pages;
};

int main(void)
{
struct book books[3];
int i;

printf("for 3 books type- NAME PRICE PAGES <enter> for each\n");

for(i=0; i<=2; i++){
scanf("%s%f%d",&books[i].name,&books[i].price,&books[i].pages);
}

for(i=0; i <=2; i++){
printf("name: %s price: $%.02f pages: %i\n",
books[i].name, books[i].price, books[i].pages);
}
return 0;
}

my input:
a 2.09 10
b 2.15 30
c 0.99 50

my output:
name: a price: $2.09 pages: 10
name: b price: $2.15 pages: 30
name: c price: $0.99 pages: 50

Last edited by linuxman2k1; 08-24-2004 at 06:28 AM.
 
Old 08-24-2004, 06:29 AM   #5
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
You should create a 2-d char array as sibtay wrote and use scanf like these

Code:
scanf("%s %f %d", name[i], &price[i], &pages[i]);
Notice the %s placeholder which is for char array and lack of & in front of the name[i] because when you create a 2-d char array name[i] will be an address already. Also you should use %s when printing name[i] in the second loop.
 
Old 08-24-2004, 06:29 AM   #6
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
i have done this with c++'s cin function

I was not able to do it scanf...the problem is that in the second iteration of the first for loop, scanf("%c",&name[i]) is not working properly
 
Old 08-24-2004, 08:02 AM   #7
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Quote:
Originally posted by sibtay
i have done this with c++'s cin function

I was not able to do it scanf...the problem is that in the second iteration of the first for loop, scanf("%c",&name[i]) is not working properly
Because you are using a character placeholder %c instead of a character array placeholder %s. And declaration of name[i] should also be changed. Look at the posts before. These will fix i think.
 
Old 08-24-2004, 08:58 AM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
scanf() should never ever be used to input a string for the same reason gets() should never ever be used. Use fgets() to input a string.
 
Old 08-24-2004, 09:24 AM   #9
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Quote:
Originally posted by itsme86
scanf() should never ever be used to input a string for the same reason gets() should never ever be used. Use fgets() to input a string.
gets() is very dangerous; i agree with that, but when we use scanf() we have a chance to specify character count to be read:

GOOD:
Code:
char input[20];
scanf("%19s", input);
BAD:
Code:
char input[20];
scanf("%s", input);
WORSE:
Code:
char input[20];
gets(input);
 
Old 08-24-2004, 10:02 AM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're right. I'm sorry. What I meant to say was scanf() should never use the %s modifier to input a string unless you specifiy the width like you did with the %19s. Without the width scanf("%s", str); is just as bad as gets(str);
 
  


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
Error in coding a java server. mrobertson Programming 3 06-02-2005 01:37 PM
error coding string in c shams Programming 1 08-23-2004 09:01 PM
error coding string in c shams Programming 2 08-20-2004 05:44 AM
error coding a function in c shams Mandriva 1 07-28-2004 12:09 AM
error coding a funtion in c shams Programming 2 07-27-2004 09:47 PM

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

All times are GMT -5. The time now is 05:52 PM.

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