Saving to a file and checking a string for a letter (in C)

Sharky Forums


Results 1 to 8 of 8

Thread: Saving to a file and checking a string for a letter (in C)

  1. #1
    Expensive Sushi
    Join Date
    Oct 2001
    Posts
    9

    Post Saving to a file and checking a string for a letter (in C)

    I was wondering how I would go about checking the first letter entered into a string variable could be checked.

    As can be seen in this block of code

    fflush(stdin);
    printf("\n\nEnter statement #d", ctr);
    gets(statement);
    while ((statement != "Q") | | (statement != "q"));
    {
    fputs(statement, fp);
    ctr+=1;
    fflush(stdin);
    printf("\n\nEnter statement #%d",ctr);
    printf("\n(Enter only Q or q to exit)\n");
    gets(statement);
    }

    I am able to enter a text string, but am unable to compare it with the letters "Q" or "q".

    I'm also having a problem with writing to the text file which is indicated by the pointer fp. What is it I may be doing wrong?

  2. #2
    Sushi
    Join Date
    Dec 2001
    Location
    Denver, CO, USA
    Posts
    3

    Post

    Here's part of your answer:
    The reason you can't check q or Q is because you are using a while-loop, where the condition to end your loop gets checked PRIOR to executing the code inside the loop. Try using a DO loop here instead.

  3. #3
    Goldfish
    Join Date
    Sep 2001
    Posts
    84

    Post

    no stevetabler, he gets() befor the loop, so the condition is checked properly. a While loop will work fine. It's been a while since i've done C, but I think your problem is that gets() grabs the whole line, including the newline (/n). try storing Q or q in a string, and use the string compare function (strcmp i think). Ya it's been a while, i could be wrong.

    ------------------
    Come, son of jor-el, kneel before zod! Snootchy Bootchies!
    Come, son of jor-el, kneel before zod! Snootchy Bootchies!

  4. #4
    Reef Shark
    Join Date
    Apr 2001
    Location
    Traverse City, MI, USA
    Posts
    305

    Post

    Get rid of the OR | | and use AND && instead. Your while loop never exits since your OR statement always returns true, unless statement could be 'Q' and 'q' at the same time, which of course is impossibe.

    ------------------
    --------------------
    "Log, its better than bad, its good!"



    [This message has been edited by Begby (edited December 05, 2001).]
    --------------------
    When you were born you cried and the world smiled. Live your life so that when you die you smile and the world cries.

  5. #5
    Goldfish
    Join Date
    Sep 2001
    Posts
    84

    Exclamation

    blah begby got it. didn't see that, i hate programing. hrmph.

    ------------------
    Come, son of jor-el, kneel before zod! Snootchy Bootchies!
    Come, son of jor-el, kneel before zod! Snootchy Bootchies!

  6. #6
    Expensive Sushi
    Join Date
    Oct 2001
    Posts
    9

    Post

    Thanks to everyone!!!

    It turned out to be two things.

    1) Using strcmp, I was able to check the values of the first two letters of the string against the values returned by Q and then by q. This solved the problem on how to evaluate.

    2) That use of && instead of | | was exactly what I was looking for. As Begby pointed out, that loop was locked because there was no way that the values returned could ever satisfy that origianl condition.

    I also learned something to pass on to other beginnig C programmers concering fprintf---fprintf will _only_ write to the output device when fclose is invoked.

    Again, thanks for the help from stevetabler,
    the_real_oldie, and Begby

  7. #7
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203

    Post

    Originally posted by Pupalaikis:

    I also learned something to pass on to other beginnig C programmers concering fprintf---fprintf will _only_ write to the output device when fclose is invoked.
    This is a bufferring problem common to most languages. Not sure about C, but Tcl and a few others allow you to set the buffer to 0 or at least very small so that the write is done when the small buffer is full rather than when the file is closed.

    ------------------
    The price of freedom is high.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  8. #8
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    1) Using strcmp, I was able to check the values of the first two letters of the string against the values returned by Q and then by q. This solved the problem on how to evaluate.
    I'm not sure what type of variable your string was, but there's an easier (and faster) way to check against a single character.

    First of all, "Q" is a string literal, while 'Q' would represent a character. Here's the difference:

    char a[10] = "Q";
    char b[10] = 'Q';

    The first statement copies a Q character AND a null character into the first two chars of a. I don't think the second will even compile, but if it did, it would only assign the character Q to the first char of b. I'm not even sure what the conditional statement

    if (a == "Q")

    would do. Probably something goofy, if it compiles

    The "correct" way to check to see if the first char of *a is a Q would be:

    if ((a[0] == 'Q') | | (a[0] == 'q'))

    I prefer using toupper:

    if (toupper(a[0]) == 'Q')

    Now, if you want to distinguish between a user entering "Q" and "Quasimodo," you'll need to do a check like:

    if ((toupper(a[0]) == 'Q') && (a[1] == 0))

    Of course, in this case the strcmp you used will work, too:

    if (!strcmp(a, "Q"))
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •