Thread: String problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    String problem

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    help

    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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    Code:
    fgets(arrayName, sizeof(arrayName), stdin);
    may seem overkill, but even with removing the newline hassle:

    Code:
    if(arrayName[strlen(arrayName)- 1] == '\n')
       arrayName[strlen(arrayName)- 1] = '\0';
    it's still a much safer input method.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    you overkill it men. you can change the array size to a larger number if you want

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by mouse666666 View Post
    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 tried your code, and it works, thanks, but I am now having a problem with my code.
    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;
    		}	
    	}
    }
    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?

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by mouse666666 View Post
    you overkill it men. you can change the array size to a larger number if you want
    You should not be posting solutions like this. Telling the OP to use gets() (which he did) is not helpful.

    Read, and learn:



    And finally, from man 3 gets (Linux):

    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.
    Last edited by kermit; 05-07-2010 at 10:08 PM.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    there should not be a quotation if(n=='1')
    it should be if(n==1)
    Last edited by mouse666666; 05-07-2010 at 10:04 PM.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by mouse666666 View Post
    there should not be a quotation if(n=='1')
    it should be if(n==1)
    n is a single char.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    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
    Last edited by mouse666666; 05-07-2010 at 10:13 PM.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by mouse666666 View Post
    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
    uh...
    char n = '0';

    I didn't write
    int n = 1;

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    didnt see that

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by mouse666666 View Post
    didnt see that
    can't find my error in the for-loop. I don't think I have one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM