How can I input into one %s.
in this manner,
scanf("%s",&x);
a string with spaces (and still maintains the spaces);
The Day of the Dragon
Young Gun of Of Love
Mouse of Gondar
so when I compare it, it will still be in spaces.
Printable View
How can I input into one %s.
in this manner,
scanf("%s",&x);
a string with spaces (and still maintains the spaces);
The Day of the Dragon
Young Gun of Of Love
Mouse of Gondar
so when I compare it, it will still be in spaces.
Read up on the format specifiers for scanf (and you're probably actually reading wrong since you're using &x with an array), you want the %[] modifier.
Quzah.
here is one way of doing it
Code:
char tony[10]; // the last space of the array is /0
printf("enter name");
gets(tony); // gets function will take the white spaces
printf("\nname is %s",tony); // this is the proof that it work
I would prefer a much bigger char array size - "The Day of the Dragon" and other titles will never fit into an array of 10 char's.
may seem overkill, but even with removing the newline hassle:Code:fgets(arrayName, sizeof(arrayName), stdin);
it's still a much safer input method.Code:if(arrayName[strlen(arrayName)- 1] == '\n')
arrayName[strlen(arrayName)- 1] = '\0';
you overkill it men. you can change the array size to a larger number if you want
I tried your code, and it works, thanks, but I am now having a problem with my code.
on my switch case, every time I put one, it just passes through my code. I got no chance to type my search. any idea?Code:{
char n = '0';
int x = 0;
int loop = 0;
int count = 0;
char trial[50];
system("cls");
printf("\n *** Search for an Item ***\n\n");
printf(" 1. Book\n");
printf(" 2. DVD\n\n > ");
n = getch();
if (n=='1')
{
printf("\nDo you want to search by:\n");
printf("[1]title\n");
printf("[2]Author\n");
printf("[3]Subject\n");
printf("[4]Call Number\n");
scanf("%d",&x);
switch(x)
{
case 1: printf("Please input the title\n");
gets(trial);
for(loop = 0; loop < 100; loop++)
{
if(strcmp(trial,ptr->title) != 0)
{
printf("hehe\n");
ptr++;
count = 1;
continue;
}
if(strcmp(trial,ptr->title) == 0)
{
count = 0;
printf("XXXXXXXXXXXXXXX");
break;
}
}
break;
}
}
}
You should not be posting solutions like this. Telling the OP to use gets() (which he did) is not helpful.
Read, and learn:
- cprogramming.com FAQ - Why gets() is bad/buffer overflows
- comp.lang.c C-FAQ - Why does everyone say not to use gets()?
- cpwiki FAQ - gets
And finally, from man 3 gets (Linux):
Quote:
Never use gets(). Because it is impossible to tell without knowing
the data in advance how many characters gets() will read, and
because gets() will continue to store characters past the end of
the buffer, it is extremely dangerous to use. It has been used to
break computer security. Use fgets() instead.
there should not be a quotation if(n=='1')
it should be if(n==1)
u are comparing n to and integer; by the way what school do you go to because i did this when i was in university
http://www.ehow.com/how_2056298_crea...atement-c.html
Also you have another problem withing your for loop
didnt see that