Q: How can you store whitespace in a string?

This is a discussion on Q: How can you store whitespace in a string? within the C Programming forums, part of the General Programming Boards category; Hi all, me again, I want to know how you can store whitespace in a string? e.g.: "what a beautiful ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    13

    Q: How can you store whitespace in a string?

    Hi all, me again,

    I want to know how you can store whitespace in a string?

    e.g.: "what a beautiful day"

    my code doesn't seem to work..

    Code:
    	  puts("Add a new question.");
    	  puts("Press [ENTER] at a new line when you are done.");
    	  while (count < MAXX && gets(libry[count].question) != NULL && libry[count].question[0] != '\0')
    	  {
    			puts("Type the first answer.");
    			scanf("%s", libry[count].answ);
    			puts("Type the second answer.");
    			scanf("%s", libry[count].answ1);
    			puts("Give the right answer (A or B)");
    			scanf("%s", libry[count++].choice);
    			while (getchar() != '\n') 
    				 continue; 
    			if (count < MAXX)
    				 puts("Type your next question.");
    	  }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    why would you want to store the phyical "whitespace" or do you want to know the amount of whitespace and hold the value in an array/pointer?
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    13
    i would like to store the whitespace because i want to add a question for a quiz game, like,

    "What is soccer?"

    and do the same for the answers, then i would like to write it to a file, line per line, like this;

    What is soccer?;A game;A car;A

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    I dont have time to code it out but make everything a string and store it in an array is the ghist of the idea.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    try %[^\n] instead of %s

    scanf("%s", char_array_name);
    stores the characters from stdin into char_array_name until white space is encountered.

    scanf("%[^\n]", char_array_name);
    stores the characters from stdin into char_array_name until newline is encountered.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    13
    Quote Originally Posted by peterchen
    try %[^\n] instead of %s

    scanf("%s", char_array_name);
    stores the characters from stdin into char_array_name until white space is encountered.

    scanf("%[^\n]", char_array_name);
    stores the characters from stdin into char_array_name until newline is encountered.
    This also doesn't seem to work, still the same problem.

    Here's my entire code.

    Code:
    struct vragen {   /* setup vraag structure   */
    	 char vraag[MAXVRAAG];
    	 char   antw[MAXANTW];
    	 char  antw1[MAXANTW];
    	 char        keuze[2];
    };
    Code:
    void addvraag()
    {	  
          struct vragen libry[MAXX]; /* array van structures > vragen.libry >> libry.vraag */
    	  int count = 0;
    	  int index, filecount;
    	  int size = sizeof (struct vragen);
          int welk;
          
          
           FILE * vragen;
          filecount = count;
          
    	  if ((vragen = fopen("vragen.txt", "a+")) == NULL)
    	  {
    			fputs("Kan vragen.txt niet openen!\n",stderr);
    			exit(1);
    	  }
    	  rewind(vragen);
    
    
    	  if (count == MAXX)
    	  {
    			fputs("Het bestand met vragen is vol!", stderr); /* max v define */
    			exit(2);
    	  }
    	  puts("Voeg nieuwe vraag toe.");
    	  puts("Druk [enter] bij een nieuwe lijn als je alle vragen hebt toegevoegd.");
    	  while (count < MAXX && gets(libry[count].vraag) != NULL && libry[count].vraag[0] != '\0')
    	  {
    			puts("Typ het eerste antwoord.");
    			scanf("%[^\n]", libry[count].antw);
    			puts("Typ het tweede antwoord.");
    			scanf("%[^\n]", libry[count].antw1);
    			puts("Geef het juiste antwoord (A of B)");
    			scanf("%[^\n]", libry[count++].keuze);
    			while (getchar() != '\n') /* kijken naar ENTER */
    				 continue; /* vrijmaken input line  */
    			if (count < MAXX)
    				 puts("Typ uw volgende vraag.");
    	  }
    	  
    
    	  for (index = 0; index < count; index++)
          {
    			printf("Vraag: %s \n [A] %s \n [B] %s \n [JUISTE ANTWOORD]: %s\n",libry[index].vraag,libry[index].antw, libry[index].antw1, libry[index].keuze);
                
          }    
                
    	  fwrite(&libry[filecount], size, count - filecount, vragen);
    	  fclose(vragen);
    	  
    
    }

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    1. don't use gets - use fgets http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    2. if you want to save strings as text separated by ; use fprintf, not fwrite
    3. scanf("%[^\n]", libry[count].antw);
    use fgets instead and remove the \n from the end as described in FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    sorry about that.

    try fgets(char_array, max_number_char, sdtin)

    as vart pointed out.

    or put
    Code:
    getchar();
    which removes '\n' from stdin

    before every scanf();
    Last edited by peterchen; 01-05-2007 at 08:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21