Thread: Newbie mind wrecking glitch

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Newbie mind wrecking glitch

    Hi guys, im working on my school assignment and there is that glitch in my program. it basically skips or ignores my code to read a char. I cant fig it out. my code is as it should be. Please dont ask me why there are so many functions its not my fault its the teachers requirement. we are not supposed to use parameters and use anything except void so... please just help me with those lines to read a character

    Code:
    none of those 2 work... the compiled proggie just skips the line. using bloodshed 4 (and im not allowed to use anything else.
    
    scanf("%c",&ch);
    ch = getchar();
    Code:
    /*
      Program Internet Service.
      
      Add your own documentation.
    
    */
    
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    
    /* Global Variables */
    int number;
    int clients;
    char ch;
    float MonthlyCharge;
    float HourlyCharge;
    float totalCharge;
    float GlobalCharge;
    float AverageCharge;
    float usage;
    
    
    
    /* Function Prototypes */
    void initialize_2100();
    void obtain_input_2200();
    void process_customer_2300();
    void display_details_2400();
    void calculate_average_2500();
    void display_summary_2600();
    
    void calculate_monthly_charge_3310();
    void calculate_monthly_charge_rate_4110();
    void Calculate_connection_charge_4120();
    void calculate_total_charge_4130();
    
    void Update_Accumulator_And_Counter_3320();
    void Increment_number_of_clients_4210();
    void Add_to_total_charge_for_all_clients_4220();
    
    /* Program Mainline */
    int main()
    {
        initialize_2100();
        obtain_input_2200();
       while(number !=  999 )
       {
            process_customer_2300();
            display_details_2400();
            obtain_input_2200();
        }
       calculate_average_2500();
       display_summary_2600();
    
        printf ("\n\n");
        system ("PAUSE");
        return 0;
    }
    /*  Code/Define all prototyped modules  */
    
    void initialize_2100()
    {
     number = 0;
     clients = 0;
     totalCharge = 0;
     usage = 0;
     GlobalCharge =0;
    
    }
    
    void obtain_input_2200()
    {
     printf("Enter the customer number : ");
     scanf("%d",&number);
     printf("Enter service code : ");
     //scanf("%c",&ch);
     ch = getchar();
     printf("Usage in hours : ");
     scanf("%f",&usage);
    }
    
    void process_customer_2300()
    {
     calculate_monthly_charge_3310();
     Update_Accumulator_And_Counter_3320();
    }
    
    void calculate_monthly_charge_3310()
    {
     calculate_monthly_charge_rate_4110();
     Calculate_connection_charge_4120();
     calculate_total_charge_4130();
    }
    
    void calculate_monthly_charge_rate_4110()
    {
    const RateMonthBusiness = 40;
    const RateMonthEconomy = 10;
    const RateMonthUnlimited = 30;
    
     switch(toupper(ch))
     {
      case 'B' :
           MonthlyCharge = RateMonthBusiness ;
           break;
      case 'E' :
           MonthlyCharge = RateMonthEconomy ;
           break;
      case 'U' :
           MonthlyCharge = RateMonthUnlimited;
           break;
      default :
              printf("Unknown operator %c\n",ch);
      }
    
    }
    
    void Calculate_connection_charge_4120()
    {
     const float RateHourBusiness = 0.20;
     const float RateHourEconomy = 0.10;
    
     switch(ch)
     {
      case 'B' :
           printf("We are processing B : %f\n",usage );
           HourlyCharge =usage * RateHourBusiness ;
           break;
      case 'E' :
           printf("We are processing E\n");
           HourlyCharge =usage * RateHourEconomy  ;
           break;
       default :
              HourlyCharge = 0  ;
              printf("Unknown Customer Charge %c\n",ch);
      }
    
    }
    
    void calculate_total_charge_4130()
    {
     printf("Hourly Customer Charge %f\n",HourlyCharge);
     printf("Monthly Customer Charge %f\n",MonthlyCharge);
     totalCharge = HourlyCharge + MonthlyCharge;
    }
    
    void Update_Accumulator_And_Counter_3320()
    {
     Increment_number_of_clients_4210();
     Add_to_total_charge_for_all_clients_4220();
    }
    
    void Increment_number_of_clients_4210()
    {
     clients+=1;
    }
    
    void Add_to_total_charge_for_all_clients_4220()
    {
     GlobalCharge += totalCharge;
    }
    
    void display_details_2400()
    {
     printf("Customer number : %d \n",number);
     printf("Service Code : %c \n",ch);
     printf("Total Charge for Customer : %.2f \n",totalCharge);
    }
    
    void calculate_average_2500()
    {
     AverageCharge =  GlobalCharge / clients;
    }
    
    void display_summary_2600()
    {
     printf("Number of Customers Processed : %d \n",clients);
     printf("Average Monthly Charge for All Customers : %.2f \n",AverageCharge);
    }

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    The scanf() (shown in bold below) reads in the number entered by the user, but leaves the newline in the stream. So, the next input function (in ch = getchar(); ) reads this newline into ch.

    You simply need to add an extra getchar() after the scanf() to gobble up the newline, as shown below.
    Code:
    void obtain_input_2200()
    {
     printf("Enter the customer number : ");
     scanf("%d",&number);
     getchar();
     printf("Enter service code : ");
     ch = getchar();
     printf("Usage in hours : ");
     scanf("%f",&usage);
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    thnx man

    you helped me fig out the cause of that pbm, but instead of getchar i used fflush (stdin);

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by mas0
    you helped me fig out the cause of that pbm, but instead of getchar i used fflush (stdin);
    Don't use fflush(stdin) even though it may work on your system, because fflushing input streams is not defined in the standard. For more details, see the following useful links:


    C- FAQ Question 12.26a


    C-FAQ Question 12.26b

  5. #5
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    if it's not defined, why does it seem to work? just curious
    It is not who I am inside but what I do that defines me.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It works because your compiler writer felt like making it work. It's still not portable, and is undefined behavior according to the standard. This means that anything can happen. Take your code on my compiler, and it won't work. It doesn't have to. Therefore, you cannot rely on it working.

    You can do things the right way, or the "works for me" way.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    okies... my teacher once used flushall() to clear all streams... is this non-portable or non-standard too???
    It is not who I am inside but what I do that defines me.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Correct. It is a non-standard function. You can use non-standard functions, but you want to avoid undefined behavoir. Or at the very least, be able to tell the difference.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    jeeze... i wish C were like java, where there are hardly any standard or portability issues becoz of the JVM... hehehe! peace to all java-haters here
    It is not who I am inside but what I do that defines me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM