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 11th, 2013, 10:14 AM
Kuiva Kuiva is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 13 Kuiva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 8 m 34 sec
Reputation Power: 0
Input with space[strings]

hi, i have a problem with my inputs, whenever i input a name with space in the part asking for the FIRST NAME,it will jump off into asking LAST NAME. it skips the part where it asks for a middle initial. so how am i gonna fix the problem? how can i make it to accept characters with space?,below is the code:

#include <stdio.h>
#include <conio.h>

main()
{
char fn[25], mn[25], ln[25], adr[25], dob[25], corz[25];
int idnum;
printf("PERSONAL INFORMATION DATA");
printf("\n\n\nFIRST NAME: ");
scanf("%s", &fn[25]);


printf("MIDDLE INITIAL: ");
scanf("%s", &mn[25]);


printf("LAST NAME: ");
scanf("%s", &ln[25]);


printf("DATE OF BIRTH");
scanf("%s", &dob[25]);


printf("ADDRESS: ");
scanf("%s", &adr[25]);



printf("ID NUMBER: ");
scanf("%d", &idnum);


printf("COURSE: ");
scanf("%s", &corz[25]);


printf("PERSONAL INFORMATION DATA");
printf("\n\nNAME: %s %s. %s", fn[25], mn[25], ln[25]);
printf("\n\nDATE OF BIRTH: %s\tADDRESS: %s", dob[25], adr[25]);
printf("\n\nID NUMBER: %d\tCOURSE: %s", idnum, corz[25]);
getch();
}

Reply With Quote
  #2  
Old February 11th, 2013, 11:33 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 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 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> printf("\n\n\nFIRST NAME: ");
> scanf("%s", &fn[25]);
You point to an array by specifying the start subscript, not the size in the declaration.

So perhaps
scanf("%s", &fn[0]);

which is more conveniently (and usually) written as
scanf("%s", fn);

Oh, and next time, use [code][/code] tags around your code, to preserve indentation and formatting.
__________________
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
  #3  
Old February 14th, 2013, 02:54 AM
Kuiva Kuiva is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 13 Kuiva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 8 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by salem
> printf("\n\n\nFIRST NAME: ");
> scanf("%s", &fn[25]);
You point to an array by specifying the start subscript, not the size in the declaration.

So perhaps
scanf("%s", &fn[0]);

which is more conveniently (and usually) written as
scanf("%s", fn);

Oh, and next time, use [code][/code] tags around your code, to preserve indentation and formatting.



thank you =) and sorry about that. i do not know how to use it. ill get to it to learn how.

Reply With Quote
  #4  
Old February 16th, 2013, 07: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
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
  #5  
Old February 16th, 2013, 12:05 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
gets is bad mmkay.

use fgets with stdin for stream:

http://www.cplusplus.com/reference/cstdio/fgets/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Input with space[strings]

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