Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C Programming Questions


  • Please log in to reply
7 replies to this topic

#1 Mordecai

Mordecai

  • Members
  • 8 posts
  • OFFLINE
  •  
  • Location:Saint Louis, Missouri
  • Local time:01:34 PM

Posted 11 June 2008 - 12:34 AM

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
May you be blessed according to your deeds

Mordecai

BC AdBot (Login to Remove)

 


#2 Alan-LB

Alan-LB

  • Members
  • 71 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Junee, NSW, Australia
  • Local time:05:34 AM

Posted 11 June 2008 - 02:57 AM

Post the code you already have so that we can see what you have done. Without that we can't make suggestions.

Alan
There are 10 types of people - those who understand binary and those who don't!!

Today is the Beta version of Tomorrow!

#3 Mordecai

Mordecai
  • Topic Starter

  • Members
  • 8 posts
  • OFFLINE
  •  
  • Location:Saint Louis, Missouri
  • Local time:01:34 PM

Posted 11 June 2008 - 11:28 AM

This is the code I have so far
#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;
}
]

Edited by KoanYorel, 11 June 2008 - 11:48 AM.
to place code within code box

May you be blessed according to your deeds

Mordecai

#4 nigglesnush85

nigglesnush85

  • Members
  • 4,371 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:06:34 PM

Posted 11 June 2008 - 12:05 PM

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
[codebox]
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();
}
[/codebox]
Regards,

Alan.

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:34 PM

Posted 11 June 2008 - 02:53 PM

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?

#6 Mordecai

Mordecai
  • Topic Starter

  • Members
  • 8 posts
  • OFFLINE
  •  
  • Location:Saint Louis, Missouri
  • Local time:01:34 PM

Posted 11 June 2008 - 04:11 PM

Regular Expression would be the text strings for some sets of Math strings for what I understood it to be
May you be blessed according to your deeds

Mordecai

#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:34 PM

Posted 11 June 2008 - 05:47 PM

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.

#8 Mordecai

Mordecai
  • Topic Starter

  • Members
  • 8 posts
  • OFFLINE
  •  
  • Location:Saint Louis, Missouri
  • Local time:01:34 PM

Posted 11 June 2008 - 06:05 PM

Thank you very much
May you be blessed according to your deeds

Mordecai




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users