C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 15th, 2013, 01:36 PM
altaf114 altaf114 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 altaf114 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
Lightbulb 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.

Reply With Quote
  #2  
Old February 15th, 2013, 02:54 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,348 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
Code:
struct {
  char name[128];
  int number;
} catalog[10];


Would an array of struct help?
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old February 15th, 2013, 10:08 PM
altaf114 altaf114 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 altaf114 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
Thumbs up

Thanks for the help...
Going to implement it...

Reply With Quote
  #4  
Old February 15th, 2013, 11:31 PM
altaf114 altaf114 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 altaf114 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old February 15th, 2013, 11:50 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 45 m 53 sec
Reputation Power: 1949
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].

Reply With Quote
  #6  
Old February 16th, 2013, 01:36 AM
altaf114 altaf114 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 altaf114 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
Exclamation 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();
}

Reply With Quote
  #7  
Old February 16th, 2013, 07:21 AM
altaf114 altaf114 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 altaf114 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
Exclamation 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..

Reply With Quote
  #8  
Old February 16th, 2013, 08:52 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 20 m 47 sec
Reputation Power: 1774
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"
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #9  
Old February 16th, 2013, 09:19 AM
altaf114 altaf114 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 altaf114 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 56 sec
Reputation Power: 0
Post

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...

Reply With Quote
  #10  
Old February 16th, 2013, 12:29 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 45 m 53 sec
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Number to String Conversion Problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap