Thread: Character in Array to end for loop.

  1. #1
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Angry Character in Array to end for loop.

    Hello all,
    Can some please show me how I can end a for loop with a character? Right now it ends with a blank line. I want it to end when some types a '&' sign as the first charater of the array.
    So if some types: &this is text. It will end or if someone just types: &
    It will end, Understand?


    Thanks for any help

    Matt





    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream.h>

    #define YES 1
    #define NO 0 // not used

    struct data {
    char* text;
    int bold, italic, underline;
    };

    void createPage ( struct data *array, int num );

    int main ( void ) {
    int response, i;
    char buff[BUFSIZ];
    struct data myStructArray[80];

    // this loop in a function would be a good idea
    for ( i = 0 ; i < 80 ; i++ ) {
    printf( "Please enter a line of text (blank to exit): " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    if ( strlen(buff) == 1 ) break;
    // y = buff;
    // strcmp(x, y);
    // puts(buff);
    // printf("%d", strcmp(x, y));

    myStructArray[i].text = malloc( strlen(buff)+1 ); // allocate space
    strcpy( myStructArray[i].text, buff );

    printf( "\nDo you want text to be bold?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].bold = response;

    printf( "\nDo you want text to be Italics?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].italic = response;

    printf( "\nDo you want text to be Underlined?[1/0] " );
    fflush( stdout );
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &response );
    myStructArray[i].underline = response;
    }

    createPage( myStructArray, i );
    return 0;
    }

    void createPage( struct data *a, int num ) {
    int i;
    FILE* outFile;
    outFile = fopen( "phaseTwo.html","w" );

    /* All HTML files must start with this tag. */
    fprintf( outFile,"<html>\n" );

    for ( i = 0 ; i < num ; i++, a++ ) {
    if(a->bold == YES) {
    fprintf( outFile,"<b>" );
    }
    if(a->italic == YES) {
    fprintf( outFile,"<i>" );
    }
    if(a->underline == YES) {
    fprintf( outFile,"<u>" );
    }

    /* Write to the file HTML representing a line of text. */
    fprintf( outFile,"<p>" );
    fprintf( outFile,"%s",a->text );
    fprintf( outFile,"</p>\n" );

    if(a->bold == YES) {
    fprintf( outFile,"</b>\n" );
    }
    if(a->italic == YES) {
    fprintf( outFile,"</i>\n" );
    }
    if(a->underline == YES) {
    fprintf( outFile,"</u>\n" );
    }
    }

    fprintf( outFile,"</html>\n" );
    fclose( outFile );
    }

  2. #2
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Did some reading, here is the answer.

    if ( buff[i] == '&' ) break;


    Matt

  3. #3
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Oops ..not the answer...only does it once through..second time does not work.

  4. #4
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    I am new at this just like you...but here's a shot at it.
    Code:
    //declare a character array
    char string[80];
    
    printf("Enter some text\n");
    fflush(stdin);
    scanf("%s", string);
    
    if(string[0]!='&')
    {
         do stuff;
    }
    don't forget the proper #include statements.
    Use a C reference book to get that info.


    essentially, you can use the string[0] in a variety of ways to test for your condition.

    Hope this helps,
    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  5. #5
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    One question for ya Matt....

    do you want the for loop to end at anytime in the string you encounter the special character? or only when the & is the first character?

    if the latter, try this. It will end either when it encounters the end of the string, or when it encounters this special character.

    Code:
    for(i=0, string[i]!='&' || i<=strlen(string); ++i)
    {
      do stuff
    }
    At least you see the gist of what I am trying to do.
    it may be that you have to use && instead of ||. I am not the best at thinking through the logic stuff just yet.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  6. #6
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Okay...finally...

    if ( buff[0] == '&' )
    break;

    Thanks of help Mike..

    Matt

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    fflush(stdin) doesn't work.
    use this instead.

    while(getchar() != '\n')continue;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  3. Overwriting all in array, why?
    By guesst in forum C Programming
    Replies: 7
    Last Post: 10-09-2008, 05:56 PM
  4. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  5. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 12:12 AM