The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Number to String Conversion Problem
Discuss Number to String Conversion Problem in the C Programming forum on Dev Shed. Number to String Conversion Problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 15th, 2013, 01:36 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 38 m 56 sec
Reputation 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..??
Please Help me.
|

February 15th, 2013, 02:54 PM
|
 |
Contributing User
|
|
|
|
Code:
struct {
char name[128];
int number;
} catalog[10];
Would an array of struct help?
__________________
[code] Code tags[/code] are essential for python code!
|

February 15th, 2013, 10:08 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
|
|
Thanks for the help...
Going to implement it...
|

February 15th, 2013, 11:31 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
|
|
I had made a simple program..
Quote: #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
|

February 15th, 2013, 11:50 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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].
|

February 16th, 2013, 01:36 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 38 m 56 sec
Reputation 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..
Quote: #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();
} |
|

February 16th, 2013, 07:21 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
|
|
Need Help with gets function..
I am also having a similar problem...
This is my program
Quote: #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...
Please Help me with gets function because I don't want to use scanf function...
Still any Help will be appreciated...
Thanks..
|

February 16th, 2013, 08:52 AM
|
 |
Contributed User
|
|
|
|
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"
|

February 16th, 2013, 09:19 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 1 h 38 m 56 sec
Reputation 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... 
|

February 16th, 2013, 12:29 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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. |
To answer your request, here is the man page for scanf:
http://linux.die.net/man/3/sscanf
On UNIX, the original OS for C (the two practically created each other), documentation for the C functions was provided by the manual utility called man, so the entire body of documentation was called the man pages. There appears to be several tens of thousands of man page collection on the Web, so there's never any reason to say that one does not have the documentation.
To do this search yourself, go to Google.com and enter the search phrase man page scanf and you will get about 81,900 hits; I just chose to give you the first one.
Last edited by dwise1_aol : February 16th, 2013 at 12:59 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|