### Thread: Number to String Conversion Problem

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
7
Rep Power
0

#### String Problem (Problem related to white_space in Output)

Hi,
I am a student and for programming practice I am making a small C program for my library.

I had given a particular code to my every book. For e.g. I have given maths a code 001 and to science 002.

Now I need admin to register those books with my program.

For e.g He should first ask to enter code, suppose user enters 003 than than compiler will ask for the name of book to enter at that code.
Like this user will update all his books to numeric codes in virtual library.
Now in future if user need to know whether which book is registered on 005 code.
Than he will enter 005 and compiler will give in output the book's name which he had previously entered.

Now You have understand what kind of program I am trying to make.
So my main question is how can I do that.?

If I use char c[10]; than I can only store name.
If I use int i[10]; than I can only enter numbers.

So how can i do that..??
2. Code:
```struct {
char name[128];
int number;
} catalog[10];```
Would an array of struct help?
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
7
Rep Power
0

Thanks for the help...
Going to implement it...
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
7
Rep Power
0

#include<stdio.h>
#include<conio.h>
struct shelf
{ char member_name[20],book_name1[30],book_name2[30],book_name3[30],book_name4[30],book_name5[30];
} mem[10];
void main()
{
clrscr();
printf("Enter member name: "); scanf("%s",mem1.member_name);
printf("Enter member name: "); scanf("%s",mem2.member_name);
printf("Enter member name: "); scanf("%s",mem3.member_name);
printf("\n\nMember 3: %s",mem3.member_name);
getch();
}
But it says there are 3 errors
1) Undefined symbol mem1
2) Undefined symbol mem2
3) Undefined symbol mem3
5. It's exactly what it says: mem1, mem2, and mem3 are not defined. You never declared them. They don't exist.

Now, there is a mem[1], mem[2], and mem[3]. Along with the first struct in that array, mem[0].
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
7
Rep Power
0

#### Problem with whitespace

Thanks for the help...

Now There's one more Problem...

Below is my Program, Whenever I write name or books name with space it creates problem for me..
For example in 1st book I wrote "Within Light" than It will not allow me to enter 2nd Book's name and cursor will directly move to 3rd Book's place.
If I enter two white_spaces than it skips next to scans..

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct shelf
{
int member_id;
char member_name[25],book_name1[50],book_name2[50],book_name3[50],book_name4[50],book_name5[50]; } mem[20];
void main()
{
clrscr();
int i=0;
printf("Enter Member's ID: ");
scanf("%d",&mem[i].member_id);
printf("Enter Name: "); scanf("%s",&mem[i].member_name);
printf("1)Enter Issued Book: "); scanf("%s",&mem[i].book_name1);
printf("2)Enter Issued Book: "); scanf("%s",&mem[i].book_name2);
printf("3)Enter Issued Book: "); scanf("%s",&mem[i].book_name3);
printf("4)Enter Issued Book: "); scanf("%s",&mem[i].book_name4);
printf("5)Enter Issued Book: "); scanf("%s",&mem[i].book_name5);
i++;
getch();
}
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
7
Rep Power
0

#### Need Help with gets function..

I am also having a similar problem...

This is my program

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct shelf
{
int member_id;
char member_name[25],book_name1[50],book_name2[50],book_name3[50],book_name4[50],book_name5[50];
}
mem[20];

void main()
{
clrscr();
int i=0;
printf("Enter Member's ID: ");
scanf("%d",mem[i].member_id);

printf("Enter Name: "); gets(mem[i].member_name);
printf("1)Enter Issued Book: "); gets(mem[i].book_name1);
printf("2)Enter Issued Book: "); gets(mem[i].book_name2);
printf("3)Enter Issued Book: "); gets(mem[i].book_name3);
printf("4)Enter Issued Book: "); gets(mem[i].book_name4);
printf("5)Enter Issued Book: "); gets(mem[i].book_name5);
}
i++;
getch();
}

Now It does not allow me to type the Member's name and if I use scanf function than I have some problems with white_space...

Still any Help will be appreciated...

Thanks..
8. The first thing is to forget you ever knew anything about gets() and never use it again

Next, input is tricky stuff, especially when you start mixing and matching input styles.
Ideally, you should read each line into a buffer using say fgets(buff,sizeof(buff),stdin), then pick it apart with say sscanf, or any other string functions you like.

But if you're stuck with scanf, then perhaps this
Code:
```#include<stdio.h>
#include<string.h>
struct shelf {
int member_id;
char member_name[25], book_name1[50], book_name2[50], book_name3[50], book_name4[50],
book_name5[50];
} mem[20];

int main()
{
int i = 0;
printf("Enter Member's ID: ");
scanf("%d", &mem[i].member_id);
printf("Enter Name: ");
scanf(" %24[^\n]%*c", mem[i].member_name);
printf("1)Enter Issued Book: ");
scanf(" %49[^\n]%*c", mem[i].book_name1);
printf("2)Enter Issued Book: ");
scanf(" %49[^\n]%*c", mem[i].book_name2);
printf("3)Enter Issued Book: ");
scanf(" %49[^\n]%*c", mem[i].book_name3);
printf("4)Enter Issued Book: ");
scanf(" %49[^\n]%*c", mem[i].book_name4);
printf("5)Enter Issued Book: ");
scanf(" %49[^\n]%*c", mem[i].book_name5);

printf("Id=%d, name=%s\n", mem[i].member_id, mem[i].member_name);
printf("Books: \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"\n",
mem[i].book_name1, mem[i].book_name2,
mem[i].book_name3, mem[i].book_name4,
mem[i].book_name5 );
return 0;
}```
Figuring out what " %49[^\n]%*c" does is an exercise for you (read the manual).
If you get stuck, at least post what you think it does (to show you've at least tried).

Oh, example
Code:
```\$ gcc bar.c
\$ ./a.out
Enter Member's ID: 123
Enter Name: fred flintstone
1)Enter Issued Book: born free
2)Enter Issued Book: die hard
3)Enter Issued Book: rabbit stew
4)Enter Issued Book: war and peace
5)Enter Issued Book:      infinity
Id=123, name=fred flintstone
Books: "born free", "die hard", "rabbit stew", "war and peace", "infinity"```
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
7
Rep Power
0

I checked the program. Its working perfect.
But I don't understand all those things you written in scanf..
So can you provide me with a link containing some more details about it..??

I am just a very new beginner in C and I don't have any reference book right now. All I do is study on various web sites and I have never gone through such a thing. So please provide me a web link which has some details about what you have written.

Thank you... :)
10. Originally Posted by altaf114
... But I don't understand all those things you written in scanf..
So can you provide me with a link containing some more details about it..??

... I don't have any reference book right now. ... So please provide me a web link which has some details about what you have written.