Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:39:38+00:00 2026-05-31T22:39:38+00:00

Possible Duplicate: If statement in my loops are being skipped in C Having a

  • 0

Possible Duplicate:
If statement in my loops are being skipped in C

Having a weird problem. I have a program that I made where you can order drinks, chips, or fish. When you order anything, the second line is “what size do you want: (s small, m medium, l large)”. Now, you can only enter s, S, m, M, l, or L. Anything beyond that, it enters the loop and tells you to enter only one of the above 6 characters.

In my verify function, I have 3 cases, one being if you have the following as the first parameter in verify: foodSize, foodSelect, or order.

Once in the verify function, and if the choice is foodSize, it goes into this loop:

if (choice == "foodSize") 
{
    do {
        answer = getchar();
        if (answer == 'S' || answer == 's')
        { rc = 0; }
        else if (answer == 'M' || answer == 'm')
        { rc = 1; }
                    else if (answer == 'L' || answer == 'l')
        { rc = 2; }
        if (rc == -1 && answer != -1) 
        {
            printf("Please enter S or M only: ");
            while (answer != -1 && answer != '\n')
                answer = getchar();
        } 
    } while (rc == -1 && answer != -1);
}

I have the EXACT SAME CODE in foodSize as I do in the other two if’s, just switching around the printf inside the 3rd if statement within the do while to Please enter X or X only: which doesn’t affect anything. All the other loops work, regardless if I put them in different orders, or put it into if, else if, and else, or if, else if, or else if.

THE LOOP THAT HAS THE SAME CODE AND WORKS FINE (meaning it asks me for my input first, doesn’t give me error message first):

            if (foodChoice == "Drinks")
            {
                do {
                    answer = getchar();
                    if (answer == 'S' || answer == 's')
                    { rc = 1; }
                    else if (answer == 'C' || answer == 'c')
                    { rc = 2; }
                    else if (answer == 'T' || answer == 'T')
                    { rc = 3; }
                    if (rc == -1 && answer != -1) 
                    {
                        printf("Please enter S, C, or T only: ");
                        while (answer != -1 && answer != '\n')
                        answer = getchar();
                    } 
                } while (rc == -1 && answer != -1);
            }   

Nothing works, nothing for foodSize work. Not even when I put verify("foodSize", foodChoice) FIRST in my question function:

part of my question function:

printf("\nDo you order %s? (Y/N): ", foodChoice);
    verify("order", foodChoice);
printf("%s choice %s: ", foodChoice, foodOptions);
    verify("foodSelect", foodChoice);
printf("What size (L - Large, M - Medium, S - Small): ");
    verify("foodSize", foodChoice);

If I put verify("foodSize", foodChoice) right after the first printf it doesn’t change anything either.

the output, no matter what, always skips my get char, gives me the error message first, then prompts me for input within my foodSize loop in my verify function REGARDLESS where the loop is placed, or what order it is in.

OUTPUT:

What size (L - Large, M - Medium, S - Small): Please enter S or M only:

when instead it should be:

What size (L - Large, M - Medium, S - Small):

I should only be prompted if I don’t enter a lower case or upper case L, M, or S. Yet it prompts me regardless. I’m at wits and haven’t been able to solve this yet. I know my code looks like shite, I know some of the ways I do things in it aren’t the current standard way, but that’s the way we have to do it in school, so please don’t point out those errors. I just want to know why this loop isn’t working and I want to fix it, that’s all.

FULL SOURCE in case somebody wants to compile it and give it a run for themselves.

http://pastebin.com/raw.php?i=hjPDu4QB

Eternal great-fullness to whoever discovers why this does not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:39:40+00:00Added an answer on May 31, 2026 at 10:39 pm

    If the line

    if (choice == "foodSize")
    

    is working at all, then you are depending on compiler optimizations. What this is doing is comparing where in memory those two strings are located; if it is true, this is only because the compiler optimized them into two pointers to the same string. The correct way to compare strings is with strcmp:

    if(strcmp(choice, "foodSize") == 0)
    

    This will actually compare the contents of the strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP make if shorter I have an if statement that looks like
Possible Duplicate: Should I use uint in C# for values that can’t be negative?
Possible Duplicate: Format Date in Bind Statement inside a ListView I have modified a
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Why does this() and super() have to be the first statement in
Possible Duplicate: java PreparedStatement Can I make the prepared statement SELECT * FROM STUDENTS
Possible Duplicate: Howto build a SQL statement with using IDs that might not be
Possible Duplicate: what is this weird code notation mean I have previously asked this
Possible Duplicate: need help with some stored procedure plz ERROR:- A SELECT statement that
Possible Duplicate: For loop and if statement Hi everyone, i have a following for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.