Thread: Help with State Machine in C; adding money glitches out program functionality

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    Help with State Machine in C; adding money glitches out program functionality

    Hello, I hope I'm posting this in the correct forum.

    I'm doing a state machine implementation as part of my C course. The program works fine except two problems, one of which I need to fix, the other is just a matter of how the professor feels about it.

    Problem 1: When I use scanf to add a float for change inserted by the adminstrator, the program glitches out. I am not sure what is causing this, and have banged my head on my desk for nearly 6 hours.

    Problem 2: If the amount of change in the machine (changeInMachine) is low enough after a transaction, it will sometimes return a negative float for the change left in the machine. It's kind of annoying but not a huge deal.

    Feel free to add anything you want to improve/clean up code but the first one is the only thing stopping me from meeting all project requirements.

    Source Code: [C] #include <stdio.h> #include <stdlib.h> //This is the project I use to debug - Pastebin.com

    State Diagram: Imgur: The magic of the Internet

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    928
    "Glitches out" is not very informative.

    In restock(), the first printf has no argument corresponding to the "%.2f euros".
    Also it looks like you're missing a continue in one of the switch cases.
    And the format for the scanf shouldn't have the width part (the .2).

    You don't need the variables to be static in event_handler.

    Instead of using floats you should use integers holding the value in cents.
    You can convert on input and output.
    Code:
    int input_money() {
        int dollars = 0;
        scanf("%d", &dollars);
        int cents = 0;
        scanf(".%d", &cents);
        return dollars * 100 + cents;
    }
     
    void output_money(int cents) {
        printf("%d.%02d\n", cents / 100, cents % 100);
    }
    If you need to validate the input it's more complicated.
    Last edited by john.c; 01-12-2020 at 09:25 AM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    My apologies, I actually copied and pasted the source code from the wrong working project (I have StateMachine.pro, StateMachineFinal.pro and StateMachineDebugging.pro so I got confused.) but either way your suggestion was correct, I actually should have had "scanf("%f", &changeStocked);" not "scanf("%.2f", &changeStocked);" and I did add a continue case and now it works like a charm.

    If I have time I will probably replace all my monies to ints to make them easier to handle, i probably could have saved a lot more time that way.
    Thank you so much John! You saved my grade!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs with a state machine
    By CodeSlapper in forum C Programming
    Replies: 5
    Last Post: 03-05-2016, 12:04 PM
  2. Finite state machine
    By wildanlsfs in forum C Programming
    Replies: 1
    Last Post: 11-16-2014, 10:05 PM
  3. Sequential State Machine
    By Blacky Ducky in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2011, 09:44 PM
  4. State machine
    By sergio in forum General Discussions
    Replies: 3
    Last Post: 07-03-2009, 09:03 AM
  5. State Machine Controller
    By scott_ill in forum C# Programming
    Replies: 0
    Last Post: 06-25-2009, 02:57 AM

Tags for this Thread