Thread: Help needed for a C beginner!

  1. #1
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33

    Help needed for a C beginner!

    Hello there! I'm studying Computer Science in my first year at university, and we are learning C for the first programming module. I have no prior programming experience and could do with a little help! For my first assignment I have been asked to write a program that calculates and prints bills for a water company. It should also compute and print summary reports about the company's business.

    I am not allowed to use arrays, structs, functions, or pointers. Just variable/declaration statements, printf and scanf functions, assignment statements, conditional if else statements and loops.

    Here is my code thus far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv)
    {    
    
      //Declare our variables
      int menuChoice;
      int customerType, qwc, band;
      char domestic, commercial
      
    
      float bandP1 = 0.20, bandP2 = 0.35, bandP3 = 0.5, bandP4 = 0.75, bandP5 = 2.5; //Multipliers for bands
      float freshChargeD; //Calculations for charges to be displayed on bill (Domestic)
      float wasteChargeD = qwc*0.25; 
      float surfChargeD = 10;
      float standChargeD = 91 * 0.1;
      float totalD = freshChargeD + wasteChargeD + surfChargeD + standChargeD;
    
      float freshChargeC = qwc*bandP5; //Calculations for charges to be displayed on bill (Commercial)
      float wasteChargeC = qwc*2.0;
      float surfChargeC = 50;
      float standChargeC = 91 * 1.30;
      float totalCost = freshChargeC + wasteChargeC + surfChargeD + standChargeD, VAT = totalCost * 0.2;
      float tc2 = totalCost + VAT;
      
    
      printf("Hello. Welcome to the program.\n");
      printf("Press RETURN key to continue...\n");
      getchar();
      system("clear");
    
      do
      {
        printf("Main Menu\n");
        printf("-----------\n");
        printf("1. Compute and Print the Bill for a Customer\n");                   //Menu for user to select what they want to do
        printf("2. Show Sums and Statistics\n");
        printf("3. Quit the program\n\n");
        printf("-----------\n");
        scanf("%d", &menuChoice); //Scans and stores users input 
      }
        while (menuChoice != 3);
    
        switch (menuChoice) //Switch statement for what to do depending on what option is selected by the user
        {
          case 1:
            system("clear");
            printf("Enter type of customer (Domestic or Commercial):\n");
            scanf("%d", &customerType);
            printf("Enter quarterly water consumption total:");
            scanf("%d", &qwc);
    
            
            if(qwc >= 1 && qwc <= 5)
            {    
               band = bandP1;
               freshChargeD = bandP1 * qwc;  //Works out charge depending on band
            }
            else if(qwc >= 6 && qwc <= 12)
            {
               band = bandP2;
                       freshChargeD = ((qwc + 1) - 6) + (bandP1 * 5);
            }
                else if(qwc >= 13 && qwc <= 25)
            {
               band = bandP3;
               freshChargeD = ((qwc + 1) - 13) + (bandP2 * 7) + (bandP1 * 5);
            }
            else if(qwc >= 26 && qwc <= 40)
            {
               band = bandP4;
               freshChargeD = ((qwc + 1) - 26) + (bandP3 * 13) + (bandP2 * 7) + (bandP1 * 5);    
            }            
                    else if(qwc >= 40)
            {
               band = bandP5;
               freshChargeD = (qwc - 40) + (bandP4 * 15) + (bandP3 * 13) + (bandP2 * 7) + (bandP1 * 5);
            }
    
        if(customerType = Domestic) //Bill summary is printed for domestic cust
            system("clear");
            printf("Type of customer: £%d", customerType);
            printf("Amount of fresh water consumption: %d", qwc);
            printf("Fresh water charges: £%.2f", freshChargeD);
            printf("Waste water charges: £%.2f", wasteChargeD);
            printf("Surface water charges: £%d", surfChargeD);
            printf("Standing charges: £%.2f", standChargeD);
            printf("Amount of VAT (if applicable: NA");
            printf("Total amount to pay: £%.2f", totalD);
        
        else if(customerType = Commercial) //Bill summary is printed for commercial cust
            system("clear");
            printf("Type of customer: £%d", customerType);
            printf("Amount of fresh water consumption: %d", qwc);
            printf("Fresh water charges: £%.2f", freshChargeC);
            printf("Waste water charges: £%.2f", wasteChargeC);
            printf("Surface water charges: £%d", surfChargeC);
            printf("Standing charges: £%.2f", standChargeC);
            printf("Amount of VAT (if applicable:", VAT);
            printf("Total amount to pay: £%.2f", tc2);
        break;
                
          case 2:
            system("clear");
            printf("This option is not yet implemented\n\n");
            break;
          case 3:
                printf("Thank you for using this program\n");
            break;
          default:
            system("clear");
            printf("Wrong choice. Please select either 1, 2, or 3.\n");
            break;
    
    }
    
       return 0;
       
     
     
    }
    Option 3 of the menu (exiting the program) is the only option that works correctly and I'm unsure as to why. Also how can I get the if else statement that prints a bill depending on if the customer is domestic or commercial to work correctly? I'm getting a compiler error as the customer types 'domestic' and 'commercial' are undeclared but I'm not sure what to use so that the correct bill is printed.

    Any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    90
    Your loop condition is:
    Code:
    while (menuChoice != 3)
    This means that if I enter anything different than the number 3 this condition is true, thus the loop will continue by showing the menu.

    As for why is it working when your input is the number 3, it is simply because this loop is false (3 != 3), therefore the code continues with its execution, entering the switch statement which is always the case 3.

    Your while loop condition should be some current program state that controls if the program execution is terminated.
    For this kind of assignments this is the pseudocode I would go with:
    Code:
    while(!state_finished)
    {
        switch(state)
        {
            case state_menu:
                // print_menu
    
            // other cases
    
            case exit:
                // set state_finished to true
    
        }
    }


    Some other comments:
    Code:
      printf("Press RETURN key to continue...\n");
      getchar();
    If I press anything other than the return key I could still keep using the program.
    If I input more than one letter before hitting the return key, the input after the first letter will be read by scanf further down in the do-while loop. This will lead to undefined behaviour.

    Another thing is that menuChoice is not initialized, it's garbage, it contains any integer number. You then compare menuChoice in your while condition to a number, but menuChoice could also be the number 3, you don't want to compare unitinitialized variables.

    Your compiler is complaining because you are mistaking the assignment operator '=' with the equality operator '=='
    This
    Code:
    if(customerType = Domestic)
    is assigning Domestic to customerType not comparing them.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I haven't as of yet taken the time to understand the context of your code very well, but here are the errors and an explanation of why they are errors.

    Code:
    char domestic, commercial
    This code needs to end with;

    Code:
    float wasteChargeD = qwc*0.25;
    qwc is a variable with no value.

    Code:
    if (customerType = Domestic)
    4 errors:
    customerType confused type?
    = is an assignment. == is a comparison operator.
    I believe your intention is that Domestic is a string literal which would mean it needs quotes. "Domestic"
    I believe your intention is to execute the statements below if on the condition that if is true. This would be accomplished by wrapping these statements within braces.

    Code:
    else if (customerType = Commercial)
    same errors again.

    Code:
    band = bandP1;
    bandP1 is a float. band is an int. If your intention is to truncate the value of bandP1, you should cast it like so:

    Code:
    band = (int) bandP1;
    This makes your intention obvious rather than giving the suspicion that you may have made an error.

    Code:
    printf("Amount of VAT (if applicable:", VAT);
    no format specifier.

    There are so many errors in this that what I really suggest is trying to get used to the debugger. It really is your best coding friend.
    Last edited by jack jordan; 11-11-2017 at 03:45 PM.

  4. #4
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Thanks for the help guys. I have edited the code as follows, but am still getting a compiler error.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char** argv)
    {    
    
      //Declare our variables
      int menuChoice, menuChoice_finished;
      int customerType, qwc;
      float band;
      int Domestic, Commercial;
    
      float bandP1 = 0.20, bandP2 = 0.35, bandP3 = 0.5, bandP4 = 0.75, bandP5 = 2.5; //Multipliers for bands
      float freshChargeD; //Calculations for charges to be displayed on bill (Domestic)
      float wasteChargeD = qwc*0.25; 
      float surfChargeD = 10;
      float standChargeD = 91 * 0.1;
      float totalD = freshChargeD + wasteChargeD + surfChargeD + standChargeD;
    
      float freshChargeC = qwc*bandP5; //Calculations for charges to be displayed on bill (Commercial)
      float wasteChargeC = qwc*2.0;
      float surfChargeC = 50;
      float standChargeC = 91 * 1.30;
      float totalCost = freshChargeC + wasteChargeC + surfChargeD + standChargeD, VAT = totalCost * 0.2;
      float tc2 = totalCost + VAT;
      
    
      printf("Hello. Welcome to the program.\n");
      printf("Press any key to continue...\n");
      getchar();
      system("clear");
    
      do
      {
        printf("Main Menu\n");
        printf("-----------\n");
        printf("1. Compute and Print the Bill for a Customer\n");                   //Menu for user to select what they want to do
        printf("2. Show Sums and Statistics\n");
        printf("3. Quit the program\n\n");
        printf("-----------\n");
        scanf("%d", &menuChoice); //Scans and stores users input 
      }
        while (!menuChoice_finished);
    
        switch (menuChoice) //Switch statement for what to do depending on what option is selected by the user
        {
          case 1:
            system("clear");
            printf("Enter type of customer (Domestic or Commercial):\n");
            scanf("%d", &customerType);
            printf("Enter quarterly water consumption total:\n");
            scanf("%d", &qwc);
    
            
            if(qwc >= 1 && qwc <= 5)
            {    
               band = bandP1;
               freshChargeD = bandP1 * qwc;  //Works out charge depending on band
            }
            else if(qwc >= 6 && qwc <= 12)
            {
               band = bandP2;
                       freshChargeD = ((qwc + 1) - 6) + (bandP1 * 5);
            }
                else if(qwc >= 13 && qwc <= 25)
            {
               band = bandP3;
               freshChargeD = ((qwc + 1) - 13) + (bandP2 * 7) + (bandP1 * 5);
            }
            else if(qwc >= 26 && qwc <= 40)
            {
               band = bandP4;
               freshChargeD = ((qwc + 1) - 26) + (bandP3 * 13) + (bandP2 * 7) + (bandP1 * 5);    
            }            
                    else if(qwc >= 40)
            {
               band = bandP5;
               freshChargeD = (qwc - 40) + (bandP4 * 15) + (bandP3 * 13) + (bandP2 * 7) + (bandP1 * 5);
            }
         
         if(customerType == "Domestic") //Bill summary is printed for domestic cust
         {        
            system("clear");
            printf("Type of customer: £%d", customerType);
            printf("Amount of fresh water consumption: %d", qwc);
            printf("Fresh water charges: £%.2f", freshChargeD);
            printf("Waste water charges: £%.2f", wasteChargeD);
            printf("Surface water charges: £%d", surfChargeD);
            printf("Standing charges: £%.2f", standChargeD);
            printf("Amount of VAT (if applicable: NA");
            printf("Total amount to pay: £%.2f", totalD);
         }
        else if(customerType == "Commercial") //Bill summary is printed for commercial cust
         {        
            system("clear");
            printf("Type of customer: £%d", customerType);
            printf("Amount of fresh water consumption: %d", qwc);
            printf("Fresh water charges: £%.2f", freshChargeC);
            printf("Waste water charges: £%.2f", wasteChargeC);
            printf("Surface water charges: £%d", surfChargeC);
            printf("Standing charges: £%.2f", standChargeC);
            printf("Amount of VAT (if applicable:", VAT);
            printf("Total amount to pay: £%.2f", tc2);
        }    
        
        break;
                
          case 2:
            system("clear");
            printf("This option is not yet implemented\n\n");
            return;
            break;
          case 3:
                printf("Thank you for using this program\n");
            menuChoice_finished = 1; 
            break;
    
        
          default:
            system("clear");
            printf("Wrong choice. Please select either 1, 2, or 3.\n");
            break;
    
    }
       return 0;
       
    }
    The error is as follows;

    Code:
    miniProject1.c: In function ‘main’:
    miniProject1.c:82:19: warning: comparison between pointer and integer [enabled by default]
       if(customerType == "Domestic") //Bill summary is printed for domestic cust
                       ^
    miniProject1.c:94:23: warning: comparison between pointer and integer [enabled by default]
      else if(customerType == "Commercial") //Bill summary is printed for commercial cust
    It compiles when I remove the else if statements that the above error is part of and the menu choice is now working better than before, but I would still like for the program to display the menu again after displaying the bill or stats after selecting choice 1 or 2. How would I go about implementing this?

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    90
    customerType is an integer and "Domestic" is a string, why are you comparing them?
    Code:
    printf("Enter type of customer (Domestic or Commercial):\n");
    This could be of the type 'D' for Domestic and 'C' for commercial and then you scan for a single character.

    Also, please remember that variables that you declare are uninitialized, meaning that they hold any value. Are you compiling with -Wall? Please do.
    In your while loop menuChoice_finished is uninitialized.

    See my reply above for my opinion on the code design, I'd get rid of that do-while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed for a programme .. (beginner level )
    By dineshpabbi10 in forum C Programming
    Replies: 4
    Last Post: 06-04-2015, 10:48 PM
  2. [Beginner] Monty Hall Guide Needed
    By Nazrin Japiril in forum C Programming
    Replies: 5
    Last Post: 05-11-2014, 03:17 AM
  3. Helps needed from beginner!
    By Xian Yao in forum C Programming
    Replies: 3
    Last Post: 12-01-2013, 01:50 AM
  4. C++ help needed - beginner and so confused
    By staceyktaylor in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2010, 11:28 PM
  5. Help needed, C++ beginner
    By JamMan in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2001, 11:16 PM

Tags for this Thread