Hi guys after fixing my previous bug this one has resulted. What I am trying to do:

  1. Make getchar() read only one character per input
  2. Make the while loop not accept numbers
  3. When the user enters duplicate characters the program should only accept the first character.

code:

// abcmail.c -- This is a program that calculates all fo ABC Mail Order Grocery activities
#define ARTICHOKE_PRICE 2.05
#define BEET_PRICE 1.15
#define CARROT_PRICE 1.19

#include <stdio.h>
#include <ctype.h>
int main (void)
{

    // Declaring my Variables 
    char ch;                        // Input character
    float artichoke_count;          // artichoke counter in pounds
    float beet_count;                   // beet counter in pounds
    float carrot_count;                 // carrot counter in pounds

    float artichokes_cost;              //  artichoke_count * price const
    float beets_cost;
    float carrots_cost;
    float total_cost;                   // Will be the sum of artichoke_cost, beef_cost, carrot_cost

    //  Initializing my variables 
    artichoke_count = 0.0;
    beet_count = 0.0;
    carrot_count = 0.0;

    /************************************************* PART 1: Collecting User Input *********************************************/
    printf("Enter the letter a for artichoke, b for beet and c for carrot and q to quit: ");

    while((ch = getchar()) != 'q')
    {

        float artichokes; // Will collect artichokes numbers from the user
        float beets;        // Will collect beets numbers from the user
        float carrots;  // Will collect carrot numbers from the user

        if ('\n' == ch) // What this does is put a new line without affecting the program because the '\n' 
            continue;   // will be regarded as a character and placed in default section

        //if ((isdigit(ch)) == -1)
        if (isalpha(ch))
        {
            ch = tolower(ch); // make the input characters to lower case if the user enters uppercase

            switch(ch)
            {
                case 'a':
                {
                    printf("Enter the number of arhitokes you need in pounds: ");
                    scanf("%f", &artichokes);
                    artichoke_count += artichokes;
                    break;
                }

                case 'b':
                {
                    printf("Enter the number of beets you need in pounds: ");
                    scanf("%f", &beets);
                    beet_count += beets;
                    break;
                }

                case 'c':
                {
                    printf("Enter the number of carrots you need in pounds: ");
                    scanf("%f", &carrots);
                    carrot_count += carrots;
                    break;
                }

                default:
                //{
                //  if (n_count <= 1)
                    printf("\nI am only accepting a for arhitokes, b for beets and c for carrots");
                //  break;
                //}

            }   // end of switch

        }

        printf("Enter the letter a for artichoke, b for beet and c for carrot and q to quit: ");    

    } // end of while loop

    printf("You have entered %.2f artichokes, %.2f beets, and %.2f carrots\n", artichoke_count, beet_count, carrot_count); // testing 
   /******************************************** End of Part 1 ******************************************************************/

    /******************************************* Part 2: Calculate the user's costs ********************************************/
    artichokes_cost = ARTICHOKE_PRICE * artichoke_count;
    beets_cost = BEET_PRICE * beet_count;
    carrots_cost = CARROT_PRICE * carrot_count;

    // Test line
    printf("artichokes costs = %.2f\n", artichokes_cost);
    printf("beets cost = %.2f\n", beets_cost);
    printf("carrots cost = %.2f\n", carrots_cost);
    total_cost = artichokes_cost + beets_cost + carrots_cost;
    return 0;
}
  1. When I try to enter input with numbers I get the following output:

    Enter the letter a for artichoke, b for beet and c for carrot and q to quit: 12
    Enter the letter a for artichoke, b for beet and c for carrot and q to quit: Enter the letter a for artichoke, b for beet and c for carrot and q to quit:

  1. When I enter duplicate characters at once this is the output:

    Enter the letter a for artichoke, b for beet and c for carrot and q to quit: bb
    Enter the number of beets you need in pounds: Enter the letter a for artichoke, b for beet and c for carrot and q to quit: Enter the number of beets you need in pounds:

I hope I can get some help on this. Thanks in advance!

Recommended Answers

All 2 Replies

You still have a bit of the same problems as your last thread. I switched the input around so you call a function to get a letter and a function to get a float. Error checking and gobbling up white space is done in those functions. You have some obsolete variable names in there (i.e. "count" vs. "weight"), but I left that part alone as it's obvious you are still working on it. The code below seems to catch and recover from most errors, though it I'm sure it's quite breakable still.

// abcmail.c -- This is a program that calculates all fo ABC Mail Order Grocery activities

#define ARTICHOKE_PRICE 2.05
#define BEET_PRICE 1.15
#define CARROT_PRICE 1.19
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

char getOption(const char* prompt)
{
    // returns a single lower-case letter
    int numNonSpace = 0;
    char temp, ch;
    printf("%s : ", prompt);
    while((temp = getchar()) != '\n')
    {
        if(isspace(temp))
            continue;
        ch = temp;
        numNonSpace++;
    }
    if(numNonSpace == 1 && isalpha(ch))
        return tolower(ch);
    return getOption(prompt);
}

float getFloat(const char* prompt)
{
    float number;
    bool error = false;
    char temp;
    printf("%s : ", prompt);
    scanf("%f", &number);
    while((temp = getchar()) != '\n')
    {
        if(!isspace(temp))
            error = true;
    }
    if(error || number < 0)
    {
        printf("Please enter a valid non-negative decimal number.\n");
        return getFloat(prompt);
    }
    return number;
}

int main (void)
{
    // Declaring my Variables
    char ch;                        // Input character
    float artichoke_count;          // artichoke counter in pounds
    float beet_count;                   // beet counter in pounds
    float carrot_count;                 // carrot counter in pounds

    float artichokes_cost;              //  artichoke_count * price const
    float beets_cost;
    float carrots_cost;
    float total_cost;                   // Will be the sum of artichoke_cost, beef_cost, carrot_cost

    //  Initializing my variables
    artichoke_count = 0.0;
    beet_count = 0.0;
    carrot_count = 0.0;

    while((ch = getOption("Enter the letter a for artichoke, b for beet and c for carrot and q to quit")) != 'q')
    {
        switch(ch)
        {
            case 'a':
            {
                artichoke_count += getFloat("Enter the number of artichokes you need in pounds");
                break;
            }

            case 'b':
            {
                beet_count += getFloat("Enter the number of beets you need in pounds");
                break;
            }

            case 'c':
            {
                carrot_count += getFloat("Enter the number of carrots you need in pounds");
                break;
            }

            default:
                printf("I am only accepting a for artichokes, b for beets, and c for carrots\n\n");
        }   // end of switch
    } // end of while loop

    printf("You have entered %.2f artichokes, %.2f beets, and %.2f carrots\n", artichoke_count, beet_count, carrot_count); // testing

    return 0;
}

Thanks @AssertNull, I didnt think about creating the external functions and using them, This worked well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.