Error?

Cant fix this error in program.

Page 1 of 1

4 Replies - 693 Views - Last Post: 05 October 2008 - 06:54 PM Rate Topic: -----

#1 Stunt101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-September 08

Error?

Post icon  Posted 05 October 2008 - 04:37 PM

Hello everyone, I need helping finding out why my program does not work right. I've been trying all last night and this morning to find out what is wrong and tried so many different things but cannot fix this. Please some one help, this is driving me crazy.

the error happens when You push C to continue. After you push C it should prompt the user to select a choice for their sport. But instead of doing this it just skips and goes to the end of the while loop. Please help!!

//---------------------------------------------------------------------------//															   
//	  Program:  prog3a.c																													
//																																			
//																																			
//																																			
//	  Purpose:  This program simulates an ESPN ordering system for																		  
//	  several sports.																													   
//																																			
//																																			
//	  Comments: This program uses the "toupper" function which translates																   
//	  all lowercase input letters to their corresponding																					
//	  uppercase equivalent. All other input values are																					  
//	  returned unchanced.																												   
//																																			
//																																			
//---------------------------------------------------------------------------//															   

#include <stdio.h>
#include <ctype.h>

//---------------------------------------------------------------------------																 
//																																			
//																																			
//	  Purpose: This function calculates the appropriate numbers for																		 
//	  Hockey																																
//																																			
//																																			
//---------------------------------------------------------------------------																 
void Hockey (float *Amount, const int NumOfYrs)
{
const float PRE = 0.5;
const float REG = 1.0;
const float PLAY = 2.5;
int Preseason, Regular, Playoff;
int i;
		printf("_______________________________________\n");
		printf("Preseason Games cost $0.50 a game.\n");
		printf("Regular Season Games cost $1.00 a game.\n");
		printf("Playoff Games cost $2.50 a game.\n");
		printf("_______________________________________\n");
		printf("\nHow many Preseason Games per year you want? ");
		scanf("%d", &Preseason);
		printf("How many Regular Season Games per year do you want? ");
		scanf("%d", &Regular);
		printf("How many Playoff Games per year do you want? ");
		scanf("%d", &Playoff);
		for (i=0; i < NumOfYrs; i++)
				*Amount += Preseason * PRE + Regular * REG + Playoff * PLAY;
		printf("\n*** You have ordered %d years of Great Hockey.\n", NumOfYrs);
}

//---------------------------------------------------------------------------																 
//																																			
//																																			
//	  Purpose: This function calculates the appropriate numbers for																		 
//	  Basketball																															
//																																			
//																																			
//---------------------------------------------------------------------------  

void Basketball (float *Amount, const int NumOfYrs)
{
const float PRE = 0.5;
const float REG = 1.0;
const float PLAY = 2.25;
int Preseason, Regular, Playoff;
int i;
		printf("_______________________________________\n");
		printf("Preseason Games cost $0.50 a game.\n");
		printf("Regular Season Games cost $1.00 a game.\n");
		printf("Playoff Games cost $2.25 a game.\n");
		printf("_______________________________________\n");
		printf("\nHow many Preseason Games per year you want? ");
		scanf("%d", &Preseason);
		printf("How many Regular Season Games per year do you want? ");
		scanf("%d", &Regular);
		printf("How many Playoff Games per year do you want? ");
		scanf("%d", &Playoff);
		for (i=0; i < NumOfYrs; i++)
				*Amount += Preseason * PRE + Regular * REG + Playoff * PLAY;
				printf("\n*** You have ordered %d years of Great Basketball.\n", NumOfYrs);
}

//---------------------------------------------------------------------------																 
//																																			
//																																			
//	  Purpose: This function calculates the appropriate numbers for Football																
//																																			
//																																			
//																																			
//---------------------------------------------------------------------------																 
void Football(float *Amount, const int NumOfYrs)
{
const float PRE = 0.5;
const float REG = 2.0;
const float PLAY = 3.75;
const float SUPER = 5.20;
int Preseason, Regular, Playoff, Super;
char Answer, ReturnChar;
int i;
		printf("_______________________________________\n");
		printf("Preseason Games cost $0.50 a game.\n");
		printf("Regular Season Games cost $2.00 a game.\n");
		printf("Playoff Games cost $3.75 a game.\n");
		printf("The SuperBowl cost $5.20.\n");
		printf("_______________________________________\n");
		printf("\nHow many Preseason Games per year you want? ");
		scanf("%d", &Preseason);
		printf("How many Regular Season Games per year do you want? ");
		scanf("%d", &Regular);
		printf("How many Playoff Games per year do you want? ");
		scanf("%d", &Playoff);
		printf("Would you like to see the SUPERBOWL? (y/n) ");
		scanf("%c%c", &ReturnChar, &Answer);
		if (Answer == 'y')
				Super = 1;
		else
				Super = 0;
		for (i=0; i < NumOfYrs; i++)
				*Amount += Preseason * PRE + Regular * REG + Playoff * PLAY
						  + Super * SUPER;
		printf("\n*** You have ordered %d years of Great Football.\n", NumOfYrs);
}

//---------------------------------------------------------------------------																 
//																																			
//																																			
//	  Purpose: This function calculates the appropriate numbers for Soccer																  
//																																			
//																																			
//																																			
//---------------------------------------------------------------------------																 
void Soccer (float *Amount, const int NumOfYrs)
{
const float PRE = 0.5;
const float REG = 1.0;
const float PLAY = 2.15;
int Preseason, Regular, Playoff;
int i;
		printf("_______________________________________\n");
		printf("Preseason Games cost $0.50 a game.\n");
		printf("Regular Season Games cost $1.00 a game.\n");
		printf("Playoff Games cost $2.15 a game.\n");
		printf("_______________________________________\n");
		printf("\nHow many Preseason Games per year you want? ");
		scanf("%d", &Preseason);
		printf("How many Regular Season Games per year do you want? ");
		scanf("%d", &Regular);
		printf("How many Playoff Games per year do you want? ");
		scanf("%d", &Playoff);
		for (i=0; i < NumOfYrs; i++)
				*Amount += Preseason * PRE + Regular * REG + Playoff * PLAY;
		printf("\n*** You have ordered %d years of Great Soccer.\n", NumOfYrs);
}


//---------------------------------------------------------------------------																 
//																																			
//																																			
//	  This is the main program that you need to write																					   
//																																			
//																																			
//																																			
//---------------------------------------------------------------------------																 
int main ()
{
   // Declarations																															
   char ControlKey, Key;
   int Years=0;
   float Sum=0.0;

  // print name and ID																													 

   printf("HELP! ID:000\n");

   //Check if user wants to continue																										  

   printf("Welcome! To order professional coverage of your favorite sports, Please enter C to continue or Q to quit:");
   scanf("%c", &Key);

   //while loop to check choice																											   

   while(Key!='C' && Key!='c' && Key!='Q' && Key!='q')
	 {
 printf("Please enter a valid choice:");
	 scanf("%c", &Key);
	 }


   //Print out user  choice																												   

   printf("Choice:%c:\n", Key);


   //Continuing the program																												   

   while((Key == 'C') || (Key == 'c'))
   {


   // Print the main menu:  
 // Print the main menu:																													

	   printf("Here is a list of programming choices\n");
	   printf("(A) Hockey\n");
	   printf("(B) Football\n");
	   printf("(C) Basketball\n");
	   printf("(D) Soccer\n");


	  // Get the sport which the user is interested in:																					   

	 printf("Please enter the letter of the sport you wish to order:");
	 scanf("%c", &ControlKey);

	  // Using a switch that moves according to the user sport, get the number of years and call the appropriate functon.	

switch(ControlKey)
	 {
	 case 'A':
	   printf("Plese enter the number of years you wish to order this program:");
	   scanf("%d", &Years);
	   Hockey(&Sum, Years);
	   break;
	 case 'B':
	   printf("Please enter the number of years you wish to order this program:");
	   scanf("%d", &Years);
	   Football(&Sum, Years);
	   break;
	 case 'C':
		printf("Please enter the number of years you wish to order this program:");
		scanf("%d", &Years);
		Basketball(&Sum, Years);
		break;
	 case 'D':
		printf("Please enter the number of years you wish to order this program:");
		scanf("%d", &Years);
		Soccer(&Sum, Years);
		break;
	 }

 // print the output of the sum value in decimal numbers with fixed notation and two decimal accuracy.								   

	 printf("Amount: $%4.2f\n", Sum);


	  // Ask users if they wish to continue or quit, use loops to take the appropriate action.												

		printf("Type 'C' to continue:\n");
		scanf("%c", &Key);


	 // end loop																															  
   }

   printf("Have a good day. Bye!\n");

return 0;
}



This post has been edited by Stunt101: 05 October 2008 - 04:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: Error?

Posted 05 October 2008 - 05:01 PM

See this post.
Was This Post Helpful? 0
  • +
  • -

#3 Stunt101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-September 08

Re: Error?

Posted 05 October 2008 - 05:23 PM

Thanks for the reply jackofalltrades. So if I understood that post. There is a new line character that is causing this and I need to stop that.?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: Error?

Posted 05 October 2008 - 06:27 PM

Wait, maybe my first instinct was incorrect. Let me compile and step through the code.

EDIT: No, that looks to be the likely culprit.

This post has been edited by JackOfAllTrades: 05 October 2008 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 Stunt101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-September 08

Re: Error?

Posted 05 October 2008 - 06:54 PM

Ok...no problem. I will continue and try to fix it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1