Help - Search - Members - Calendar
Full Version: C Programming Questions
BleepingComputer.com > Software > Programming
   
Mordecai
I am learning programming and the first one I was told to learn was C as I was told it was the basis for all other programs

So I got my first program made but I am trying to figure out how to do a few things, those are listed below. I already got the program to work , I am just trying to tweak it to learn more possibilities and make it better. I have a friend who is actually taking a C programming class through University of Phoenix Online and just to learn C I have been using the information in their class to practice.

Note:This is not for a school homework assignment as I am 28 and have been out of school for a while, just decided to learn programming with a job offer I received for a business.


1. I have a place where the user needs to choose a 1, 2, or 3 with a while loop to make sure only a 1,2,or 3 is chosen however I had someone test it out and they tried 2.5 and it messed up on them

Question 1, What code for C do I use to prevent an entry of a 1.3 or 2.5 from messing up and what code do I use to get the loop to repeat and re-ask them to choose if they do not choose a 1, 2, or 3.

2. I was thinking maybe I could have a question in the end asking them if they would like to try again and if they say yes the loop would repeat if they say no the program would end

Question 2, What code would I use to do something along those lines?

Thank you in advance
Alan-LB
Post the code you already have so that we can see what you have done. Without that we can't make suggestions.

Alan
Mordecai
This is the code I have so far
CODE
#include <stdio.h>

int main(void)
{
    //Declare Variables
    float fsubtotal, taxdelmar, taxencinitas, taxlajolla;
    float fCost;//calculation operator
    int iResponse = 0;
    
    fCost = 0;
    fsubtotal=125.00;
    taxdelmar=.0725;
    taxencinitas=.0775;
    taxlajolla=.0750;

    //Program Header
    printf("\n\tKudler Fine Foods 'Sales Tax Calculator'\n");
    printf("\n\t1. Del Mar\n");
    printf("\n\t2. Encinitas\n");
    printf("\n\t3. La Jolla\n");
    printf("\n Please choose your Location (1,2, or 3)\n");
  
    //Repeatedly get input until a number between 1 and 3 is entered
    while(iResponse <= 0 || iResponse > 3)
        scanf("%i", &iResponse);
    
    
    if(iResponse ==1)    
    {
        printf("\n Welcome Del Mar Associate.\n");
        printf(" Please enter your sub-total: $");
        scanf("\n%f", &fCost);//sale sub-total
        printf(" --------------------------------\n");
        printf(" Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
        printf(" The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    }
    else if(iResponse ==2)    
    {
        printf("\n Welcome Encinitas associate.\n");
        printf(" Please enter your sub-total: $");
        scanf("%f", &fCost);//sale sub-total
        printf(" --------------------------------\n");
        printf(" Your total tax is $%.2f\n", fCost * .075);//in-line calculation
        printf(" The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    }
    else
    {    
        printf("\n Welcome La Jolla associate.\n");
        printf(" Please enter your sub-total: $");
        scanf("%f", &fCost);//sale sub-total
        printf(" --------------------------------\n");
        printf(" Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
        printf(" The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    }
    getchar();
    getchar();//keeps application viewable on screen
    // end selection
    return 0;
}                                                                                                 de#include <stdio.h>

int main(void)
{
    //Declare Variables
    float fsubtotal, taxdelmar, taxencinitas, taxlajolla;
    float fCost;//calculation operator
    int iResponse = 0;
    
    fCost = 0;
    fsubtotal=125.00;
    taxdelmar=.0725;
    taxencinitas=.0775;
    taxlajolla=.0750;

    //Program Header
    printf("\n\tKudler Fine Foods 'Sales Tax Calculator'\n");
    printf("\n\t1. Del Mar\n");
    printf("\n\t2. Encinitas\n");
    printf("\n\t3. La Jolla\n");
    printf("\n Please choose your Location (1,2, or 3)\n");
  
    //Repeatedly get input until a number between 1 and 3 is entered
    while(iResponse <= 0 || iResponse > 3)
        scanf("%i", &iResponse);
    
    
    if(iResponse ==1)    
    {
        printf("\n Welcome Del Mar Associate.\n");
        printf(" Please enter your sub-total: $");
        scanf("\n%f", &fCost);//sale sub-total
        printf(" --------------------------------\n");
        printf(" Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
        printf(" The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    }
    else if(iResponse ==2)    
    {
        printf("\n Welcome Encinitas associate.\n");
        printf(" Please enter your sub-total: $");
        scanf("%f", &fCost);//sale sub-total
        printf(" --------------------------------\n");
        printf(" Your total tax is $%.2f\n", fCost * .075);//in-line calculation
        printf(" The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    }
    else
    {    
        printf("\n Welcome La Jolla associate.\n");
        printf(" Please enter your sub-total: $");
        scanf("%f", &fCost);//sale sub-total
        printf(" --------------------------------\n");
        printf(" Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
        printf(" The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    }
    getchar();
    getchar();//keeps application viewable on screen
    // end selection
    return 0;
}
]
nigglesnush85
You would need a try catch block to catch the imput mismatch errors.

You could use a while loop that would prompt the user to enter more details. In Java it would look something like
CODE

while (!(inputString.equals("exit")))// while the name entered is not exit then continue asking for names
{
System.out.println("Please enter a name");
inputString = input.next();
}
groovicus
The best way to validate your input would be to make sure that it only contains digits; as is, what happens if someone puts in an exclamation point? What happens if someone puts in a letter? It will bomb out. Do you know about regular expressions yet?
Mordecai
Regular Expression would be the text strings for some sets of Math strings for what I understood it to be
groovicus
A regular expression is a string that can be used to find patterns within other strings. For example, you can use a pattern to make sure that a string contains all numbers, all letters, or any combination of letters, numbers, and symbols.
http://www.regular-expressions.info/

I don't know what the proper syntax is for C off the top of my head.
Mordecai
Thank you very much
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.