The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Input with space[strings]
Discuss Input with space[strings] in the C Programming forum on Dev Shed. Input with space[strings] 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 11th, 2013, 10:14 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 13
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();
}
|

February 11th, 2013, 11:33 AM
|
 |
Contributed User
|
|
|
|
|
> 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.
|

February 14th, 2013, 02:54 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 13
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.
|

February 16th, 2013, 07:19 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
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..
|

February 16th, 2013, 12:05 PM
|
 |
Contributing User
|
|
|
|
|
gets is bad mmkay.
use fgets with stdin for stream:
http://www.cplusplus.com/reference/cstdio/fgets/
|
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
|
|
|
|
|