Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,806 people online right now. Registration is fast and FREE... Join Now!




Reading Input With Spaces In C

 
Reply to this topicStart new topic

Reading Input With Spaces In C

wingz198
9 Apr, 2007 - 06:44 AM
Post #1

D.I.C Head
**

Joined: 14 Oct, 2005
Posts: 55


My Contributions
Hi
I'm writing a pretty simple C program and I have to read in information from the user at the command line. I'm having problems getting input that has spaces. I have this code i:

CODE

    char *query = "";
    char *email = "";
    char fname[30] = "";
    char lname[30] = "";
    char name[] = "";
    char *street = "";
    char *city = "";
    char *state = "";
    char *zip = "";
    char *phone = "";
  
            printf("***************NEW USER SIGNUP***************");
            printf("\n\tEnter email address: ");
            scanf ("%s", &email);
            printf("\n\tEnter first name: ");
            scanf ("%s", &fname);
            printf("\n\tEnter last name: ");
            fgets (lname, 50, stdin);
//          scanf ("%s", &lname);
//          strcat(name, fname);
//          strcat(name, lname);
            printf("\n\tEnter street: ");
            fgets (street, 50, stdin);
            printf("\n\tEnter city: ");
            scanf ("%s", &city);
            printf("\n\tEnter state: ");
            scanf ("%s", &state);
            printf("\n\tEnter zip code: ");
            scanf ("%s", &zip);


I've tried the commented out lines, but it gives me a seg fault and the way I have it now, it skips the input part of 'last name' and gets a seg fault on 'street' even when it's a single line input.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Reading Input With Spaces In C
9 Apr, 2007 - 05:47 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
If you are looking for input with spaces, why not simply use the fgets function to capture the first and last name in the same variable?
User is offlineProfile CardPM
+Quote Post

lifeRoot
RE: Reading Input With Spaces In C
9 Apr, 2007 - 05:56 PM
Post #3

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 23


My Contributions
Something else to keep in mind...

When you declare a string using "char string[]", that variable "string" is already a "pointer"... meaning you can get rid of the "&" symbol...

So scanning in a name would look like this:
CODE

scanf("%s", string);

User is offlineProfile CardPM
+Quote Post

wingz198
RE: Reading Input With Spaces In C
16 Apr, 2007 - 06:30 AM
Post #4

D.I.C Head
**

Joined: 14 Oct, 2005
Posts: 55


My Contributions
I have this code right now, but when it gets to the fgets part it just goes to the next iteration of the loop without getting any keyboard input. Am I forgetting to put something else in?

CODE

    int action = 0;
    char *query = "";
    char email[30];
    char name[30];
    while (action != 4)
    {
        printf("\n\t1) Sign up new user");
        printf("\n\nEnter a number for query (1-3) or quit (4):  ");
        scanf ("%d", &action);
        printf("\n\n");

        if (action == 1)
        {
            printf("***************NEW USER SIGNUP***************");
            printf("\n");
            printf("Enter name: ");
            fgets(name, 30, stdin);
            printf("your name is : %s\n", name);
        }
    }


I get this output:
***************NEW USER SIGNUP***************
Enter name: your name is :


1) Sign up new user
2) Wishlist detail for user
3) Movie renting detail

Enter a number for query (1-3) or quit (4):

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Reading Input With Spaces In C
16 Apr, 2007 - 07:13 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
It's likely due to the fact that there is a character left in the buffer from the scanf() operation...probably the newline character. As it's already in stdin, the fgets operation takes that as input. After the scanf(), you may have to flush the buffer.
User is offlineProfile CardPM
+Quote Post

wingz198
RE: Reading Input With Spaces In C
16 Apr, 2007 - 07:26 AM
Post #6

D.I.C Head
**

Joined: 14 Oct, 2005
Posts: 55


My Contributions
I think that might be it. When I take out the fgets, it works. How would I flush the buffer in C? I don't see a command for it like in C++. I tried fflush(stdin) after the scanf, but it still does the same thing

QUOTE(Amadeus @ 16 Apr, 2007 - 09:13 AM) *

It's likely due to the fact that there is a character left in the buffer from the scanf() operation...probably the newline character. As it's already in stdin, the fgets operation takes that as input. After the scanf(), you may have to flush the buffer.


This post has been edited by wingz198: 16 Apr, 2007 - 07:30 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Reading Input With Spaces In C
16 Apr, 2007 - 07:36 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You appear to be using C, not C++...in either case, there are a few ways to address the issue.

1. Place a call to getchar() after the scanf() operation.
2. Use fgets to take the original input, then convert it to the data type you need.
User is offlineProfile CardPM
+Quote Post

wingz198
RE: Reading Input With Spaces In C
16 Apr, 2007 - 07:42 AM
Post #8

D.I.C Head
**

Joined: 14 Oct, 2005
Posts: 55


My Contributions
That seems to work. Thanks a lot for your help. I can't believe I spent so much time on something this simple.

User is offlineProfile CardPM
+Quote Post

k0b13r
RE: Reading Input With Spaces In C
16 Apr, 2007 - 07:49 AM
Post #9

D.I.C Head
Group Icon

Joined: 18 Jul, 2006
Posts: 196



Thanked: 1 times
Dream Kudos: 250
My Contributions
Try using getline() function smile.gif
User is offlineProfile CardPM
+Quote Post

Mickey
RE: Reading Input With Spaces In C
16 Apr, 2007 - 07:55 AM
Post #10

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 7


My Contributions
QUOTE(k0b13r @ 16 Apr, 2007 - 08:49 AM) *

Try using getline() function smile.gif


Actually getline would be more of a c++ solution and not C. fgets is the way to go.

http://www.cplusplus.com/reference/clibrar...tdio/fgets.html

This post has been edited by Mickey: 16 Apr, 2007 - 08:01 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month