Jump to content
Sign in to follow this  
fieldarchy

C programming

Recommended Posts

Hey Guys,

 

So, I'm needing some help trying to figure out what is wrong with the code I've written. It's not functioning properly! It's my final assignment that is due tomorrow and I've been working on it all week! I can't figure it out!

 

Is there anyone out there who is good with C programming? This is very basic programming and I'm using Miracle C workbench as my compiler.

 

If there is anyone who is good with C programming please let me know. I'm desperate for help at this point!

 

Meg

Share this post


Link to post
Share on other sites

It's been a long time since I've worked with C (I'm almost exclusively on the interaction design side these days) and probably running too close to your deadline, but I'd be happy to take a look as well.

 

Ron

Share this post


Link to post
Share on other sites

Hey Guys,

 

Here is my code so far!

 

If you give me your emails I can send you the benchmark of how the program is supposed to behave. My problem is that no matter what loops I try I keep getting an inifinite loop. So, this is just with the if, else, if statements but I've tried the while loop and do while loop!

 

If you need a copy of Miracle C, it is a free download. That is the compiler I'm using just in case you want to give it a shot yourself.

 

I am so frustrated and I have no idea what needs fixing. I've got until Midnight tonight (AZ time!) Some of this code I borrowed from someone elses program that I found on the web. It performs the calculations correctly I just can't figure out how to get the darned thing to loop properly! GRR!

 

Oh and I have to figure out how to fix the code for inputing the store letter so that the user does not have to hit enter.

 

//Tax Calculation Program for Kudler Fine Foods, Service Request"SR-kf-008" with ability for user to input sales amount

//by Megan Maples

 

#include<stdio.h>

//variable declarations and initializations

//#define creates constants and macros

#define DelmTax 7.25

#define EnciTax 7.50

#define LajoTax 7.75

 

 

float user_input(){

float fSaleAmt;//variable for sale amount

//header for program

printf("\n\n\tName:\t\tMegan Maples\n\tClass:\t\tCSS 561\n\tAssignment:\tWorkshop 6");

printf("\n\t--------------------------------\n");

printf("\n\tTax calculation program for Kudler Stores\n");

//input sale amount by user

printf("\n\tEnter Sale Amount: ");

scanf("%f", &fSaleAmt);

return fSaleAmt;

}//end of function

 

char storeMenu(){

char cMenuOpts;//variable for menu options

printf("\n\tStore Selection Menu");

printf("\n\t--------------------------------\n");

//display menu and options for user

printf("\n\tD)\tDel Mar");

printf("\n\tE)\tEnicinitas");

printf("\n\tL)\tLa Jolla");

printf("\n\tQ)\tQuit");

printf("\n\n\tSelect D, E, L or Q: ");

scanf("%c", &cMenuOpts);

return cMenuOpts;

}//end of function

 

float main(){

float fSaleAmt=0.00;//variable for user input of sale amount

char storeLet;

 

fSaleAmt=user_input();

if(fSaleAmt < 0.00){

printf("\n\tInvalid Purchase Amount");

}

 

else{

storeLet=storeMenu();

storeLet!='q'||storeLet!='Q';

if(storeLet=='D'||storeLet=='d'){

printf("\n\tStore Name : Del Mar");

printf("\n\tTax Rate : %.2f", DelmTax);

printf("\n\tSale Amount : %.2f", fSaleAmt);

printf("\n\tTax Amount : %.2f", DelmTax*fSaleAmt/100);

printf("\n\tTotal Sale Amount : %.2f", DelmTax*fSaleAmt/100+fSaleAmt);

}//end if

 

else

if(storeLet=='E'||storeLet=='e'){

printf("\n\tStore Name : Encinitas");

printf("\n\tTax Rate : %.2f", EnciTax);

printf("\n\tSale Amount : %.2f", fSaleAmt);

printf("\n\tTax Amount : %.2f", EnciTax*fSaleAmt/100);

printf("\n\tTotal Sale Amount : %.2f", EnciTax*fSaleAmt/100+fSaleAmt);

}//end if

 

 

else

if(storeLet=='L'||storeLet=='l'){

printf("\n\tStore Name : La Jolla");

printf("\n\tTax Rate : %.2f", LajoTax);

printf("\n\tSale Amount : %.2f", fSaleAmt);

printf("\n\tTax Amount : %.2f", LajoTax*fSaleAmt/100);

printf("\n\tTotal Sale Amount : %.2f", LajoTax*fSaleAmt/100+fSaleAmt);

}//end if

 

 

}//end else

 

 

 

getch();//keeps DOS window open

 

}//end main

Share this post


Link to post
Share on other sites

If I'm remembering my C correctly...

 

For the looping problem, you need to have something like:

 

while (storeLet !='q' && storeLet !='Q')
{
// get amount
// get store letter
// do whatever
} // end while

or

 

do (
{
// get amount
// get store letter
// do whatever
} while (storeLet != 'q' && storeLet != 'Q')

 

You should also always initialize your variables. In this case set storeLet = 'g' or something like that when you declare it.

 

Finally, shouldn't the header for the program go at the beginning of the main (outside of the loop) rather than being printed every time the user can enter in input?

 

Ron

Share this post


Link to post
Share on other sites

I'm not quite sure what needs to go between the brackets for the do while loop though. I've tried several different things and it won't work! as you can see I'm frustrated. I tried going off examples in our book, that my teacher had given and I just don't get something!

 

do I put the do { before the block of code for each store and the while at the end of it? Or does other information need to go in there?

Share this post


Link to post
Share on other sites
I'm not quite sure what needs to go between the brackets for the do while loop though. I've tried several different things and it won't work! as you can see I'm frustrated. I tried going off examples in our book, that my teacher had given and I just don't get something!

 

do I put the do { before the block of code for each store and the while at the end of it? Or does other information need to go in there?

 

 

Ah, okay, I've got the reference now. I'll send you an email.

 

Ron

Share this post


Link to post
Share on other sites

One problem I'm seeing with the code above is this one:

 

		storeLet!='q'||storeLet!='Q';

 

All this does is checks if the value of storeLet is not 'q' or 'Q', but it doesn't act on that at all.

 

 

 

Also, I'm not sure if you've covered the switch and case statements, but they'd make your code a bit more efficient.

 

		switch ( storeLet )
	{
	case 'D':
	case 'd':
		{
			printf("\n\tStore Name : Del Mar");
			printf("\n\tTax Rate : %.2f", DelmTax);
			printf("\n\tSale Amount : %.2f", fSaleAmt);
			printf("\n\tTax Amount : %.2f", DelmTax*fSaleAmt/100);
			printf("\n\tTotal Sale Amount : %.2f", DelmTax*fSaleAmt/100+fSaleAmt);
		}
		break;
	case 'E':
	case 'e':
		{
			printf("\n\tStore Name : Encinitas");
			printf("\n\tTax Rate : %.2f", EnciTax);
			printf("\n\tSale Amount : %.2f", fSaleAmt);
			printf("\n\tTax Amount : %.2f", EnciTax*fSaleAmt/100);
			printf("\n\tTotal Sale Amount : %.2f", EnciTax*fSaleAmt/100+fSaleAmt);
		}
		break;
	case 'L':
	case 'l':
		{
			printf("\n\tStore Name : La Jolla");
			printf("\n\tTax Rate : %.2f", LajoTax);
			printf("\n\tSale Amount : %.2f", fSaleAmt);
			printf("\n\tTax Amount : %.2f", LajoTax*fSaleAmt/100);
			printf("\n\tTotal Sale Amount : %.2f", LajoTax*fSaleAmt/100+fSaleAmt);
		}
		break;
	case 'Q':
	case 'q':
	default:
		break;
	}

 

Finally, your main function is defined to return a float, but you don't have it returning anything at all. I'd either define it as void, or return some value.

Share this post


Link to post
Share on other sites

flynn thanks for your help! Ron already worked with me on it!

 

I had actually tried to do the switch case but it wasn't returning a loop. That was the main issue. I was either getting an infinite loop or no loop at all. Ron was able to pin point the issues though and help me fix them. Once I saw it it totally made sense. I was just so done with the program since I had been working on it non stop for a day and a half! I'm glad we got it working.

 

Thank you so much Ron! I really really REALLY SUPER DUPER appreciate it!

Share this post


Link to post
Share on other sites

I'm glad to see you got this resolved Meg. And with 7 hours to spare too! Plenty of time ::P: . I definitely remember sweating it out to the last minute for a few of these programming classes.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...