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 608909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:30:34+00:00 2026-05-13T17:30:34+00:00

I’m writing a program to basically check if a number is a type float,

  • 0

I’m writing a program to basically check if a number is a type float, double, or long double for an assignment using switch statements and a state machine. I am stepping through my program, and it gets all the way to the end except doesn’t seem to recognize the string terminator ‘\0’. So I was wondering if that portion of my code is correct. I included the whole code, but with an input like 0.0F, it gets all the way to the F_END state, and then does not return TYPE_FLOAT, but instead returns NOTFLOATING and I don’t see why it doesn’t enter the case ‘\0’: statement.

StatusCode DetectFloats(const char *cp) 
{
    enum States {
        START,
        NO_WHOLE,
        WHOLE,
        FRACT,
        EXPONENT,
        PLUS_MINUS,
        DIGIT,
        F_END,
        L_END
    } state = START;

    while (*cp != '\0') {
        switch (state) {

        case START:
            switch (*cp) {
                case '.':
                    state = NO_WHOLE;
                    break;
                default:
                    if (isdigit(*cp)) {
                        state = WHOLE;
                    }
                    else {
                        return NOTFLOATING;
                    }
                    break;
            }
            break;

        case WHOLE:
            switch (*cp) {
                case '.':
                    state = FRACT;
                    break;
                case 'e':
                case 'E':
                    state = EXPONENT;
                    break;
                default:
                    if (isdigit(*cp)) {
                        state = WHOLE;
                    }
                    else {
                        return NOTFLOATING;
                    }
                    break;
            }
            break;
        case FRACT:
            switch (*cp) {
                case 'f':
                case 'F':
                    state = F_END;
                    break;
                case 'l':
                case 'L':
                    state = L_END;
                    break;
                case 'e':
                case 'E':
                    state = EXPONENT;
                    break;
                case '\0':
                    return TYPE_DOUBLE;
                default:
                    if (isdigit(*cp)) {
                        state = FRACT;
                    }
                    else {
                        return NOTFLOATING;
                    }
                    break;
            }
            break;
        case EXPONENT:
            switch (*cp) {
                case '+':
                case '-':
                    state = PLUS_MINUS;
                    break;
                default:
                    if (isdigit(*cp)) {
                        state = DIGIT;
                    }
                    else {
                        return NOTFLOATING;
                    }
                    break;
            }
            break;
        case PLUS_MINUS:
            switch (*cp) {
                default:
                    if (isdigit(*cp)) {
                        state = DIGIT;
                    }
                    else {
                        return NOTFLOATING; 
                    }
                    break;
            }
            break;
        case DIGIT:
            switch (*cp) {
                case 'f':
                case 'F':
                    state = F_END;
                    break;
                case 'l':
                case 'L':
                    state = L_END;
                    break;
                case '\0':
                    return TYPE_DOUBLE;
                default:
                    if (isdigit(*cp)) {
                        state = DIGIT;  
                    }
                    else {
                        return NOTFLOATING;
                    }
            }
            break;
        case F_END:
            switch (*cp) {
                case '\0':
                    return TYPE_FLOAT;
                    break;
                default:
                    return NOTFLOATING;
            }
            break;
        case L_END:
            switch (*cp) {
                case '\0':
                    return TYPE_LDOUBLE;
                default:
                    return NOTFLOATING;
            }
            break;
    }
    cp++;
}

}

Also, with my code set up as it is, if I have a return statement, I don’t need a break statement do I?

Edited to add full code and clarification.

  • 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-13T17:30:34+00:00Added an answer on May 13, 2026 at 5:30 pm

    I’ve only skimmed your code, but…

    So, near the top you have this:

    while (*cp != '\0')
    

    Then inside that loop you have a bunch of these:

    switch (*cp)
    {
       // snip
    case '\0':
       // snip
    }
    

    Naturally these case labels are not going to get executed, because if *cp is 0 the condition *cp != '\0' is going to evaluate to false and the loop body will not execute.

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

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, an apk file is just a modified jar file.… May 15, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer This is assuming you ordered the rows by ID but… May 15, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer I don't think there is any tool that can automate… May 15, 2026 at 12:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.