Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,577 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,896 people online right now. Registration is fast and FREE... Join Now!




Error?

 
Reply to this topicStart new topic

Error?, Cant fix this error in program.

Stunt101
5 Oct, 2008 - 03:37 PM
Post #1

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 22

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!!

CODE
//---------------------------------------------------------------------------//                                                              
//      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: 5 Oct, 2008 - 03:49 PM
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Error?
5 Oct, 2008 - 04:01 PM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
See this post.
User is offlineProfile CardPM
+Quote Post

Stunt101
RE: Error?
5 Oct, 2008 - 04:23 PM
Post #3

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 22

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.?
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Error?
5 Oct, 2008 - 05:27 PM
Post #4

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
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: 5 Oct, 2008 - 06:02 PM
User is offlineProfile CardPM
+Quote Post

Stunt101
RE: Error?
5 Oct, 2008 - 05:54 PM
Post #5

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 22

Ok...no problem. I will continue and try to fix it.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month