I have some data
"name of client"
I have to read in the name (it is entered within double quotes) and store it has a string (with 20max characters)
How do I read the name into a string array?
I have some data
"name of client"
I have to read in the name (it is entered within double quotes) and store it has a string (with 20max characters)
How do I read the name into a string array?
Read up on the use of fgets()
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.
we didn't learn fgets yet.
i only know scanf and getchar()
can they be used?
So use what you know and post what you tried then.
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.
well....I know I have to read in the name between the quotes, and I know that getchar() only reads one character at a time, so how do I get it to read the name (everything within quotes) and put it into a string?
what im trying so far is along the lines of this.
Code:#include <stdio.h> #include <conio.h> void main (void) { char a; printf ("enter lines:- "); a=getname(); printf ("name is %s",a); } char getname (void) { char ch; char string [21]; ch=getchar(); while (ch!='"') ch=getchar(); if (ch='"') { ch=getchar(); while (ch!='"') scanf ("%s" ,&string); } return string; }
How about something more like this:
Code:void getName ( char *buff, size_t limit ) { char ch; char *pb = buff; while ( ( ch = getchar() ) != '\"' ) ; while ( --limit > 0 && ( ch = getchar() ) != '\"' ) *pb++ = ch; *pb = '\0'; }
My best code is written with the delete key.
well I don't exactly understand that. and we didn't learn pointers yet, so I shouldn't use them...but can reading the name in be done with scanf and getchar() cause to me it can't be done with getchar() seeing that it only reads one character at a time and I need to read in all the name characters and set it into a string.
by the way, how do I set something to a string? C doesn't have string data types does it...
Do you know how to use loops? If you must use getchar, loop until you reach the end of the line, or until you've run out of room in your array. Place the characters one at a time in the array. And no, C doesn't have a 'string' data type.
Quzah.
Hope is the first step on the road to disappointment.
Maybe this, which is a bit like Prelude's code only perhaps within your reach
Code:#include <stdio.h> void getname(char string[], int max); int main(void) { char a[21]; printf("enter lines:- "); getname(a,21); printf("name is %s\n", a); return 0; } void getname(char string[], int max) { int ch; int i = 0; /* find the " */ while ( getchar() != '"' ); /* now copy some chars */ while ( i < (max-1) && (ch=getchar()) != '"' ) { string[i] = ch; i = i + 1; } string[i] = '\0'; /* find the other " if we didn't already */ if ( ch != '"' ) { while ( getchar() != '"' ); } }
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.
why doesn't this code work?
Code:#include <stdio.h> #include <conio.h> void main (void) { void getname(); printf ("enter line:- "); getname(); } void getname (void) { char ch; char string [21]; ch=getchar(); while (ch!='"') ch=getchar(); if (ch=='"') { ch=getchar(); while (ch!='"') scanf ("%s", &string); } printf ("the name is %s", string); }
> while (ch!='"') scanf ("%s", &string);
This never changes ch
So either it doesn't execute at all, or it executes forever
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.
ok, I did it over as
Code:#include <stdio.h> #include <conio.h> void getname(char string[21]); main () { char name[21]; printf ("enter line:- "); getname(name); printf ("name is %s", name); } void getname (char string[21]) { char ch; int i=0; ch=getchar(); while (ch!='"') ch=getchar(); if (ch=='"') { ch=getchar(); while (ch!='"') { string[i]=ch; i++; ch=getchar(); } } string[i]='\0'; }
Last edited by Hexxx; 11-02-2003 at 07:09 AM.
Im suppose to let the program stop when the (Data) name entered is "$$$$"
i put
but I get an error "character constant must be one or two characters long" and a warning "Non portable pointer conversion"Code:while (name!='$$$$')
how do i make it stop when I enter "$$$$"?
Any length of characters that doesn't represent an escape character is a string, you need to use double quotes. And since you can't compare strings with the equality operators, include the string.h header and do this:
Code:while ( strcmp ( name, "$$$$" ) != 0 )
My best code is written with the delete key.
thanks a million!!!
the functions seem to be working now, now to try and put them together!!!!
thanks for helping!!!