1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Help with C code

Discussion in 'Software Development' started by Crowe182, Oct 7, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. Crowe182

    Crowe182 Thread Starter

    Joined:
    Oct 7, 2003
    Messages:
    14
    I am creating a program but am having trouble with my nested if statements. Below is the code. My getchar(); for computer will not pick up the 'S' or 's' and skips past the first if statement and goes into the els statement, also my do/while loop is not working properly. Any help would be greatly appriceated.

    Thanks in advance:
    Crowe182....
    ________________________
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void main()
    {
       char mon,
              prin,
              computer;
    
    
       int customer = 0,
           pentium = 0,
           pentium2 = 0,
           response;
    
      double s = 0, //pentium
                p = 0, //pentium II
                printer = 0,
                monitor = 0,
                sub_total = 0,
                total = 0,
                days = 0;
    
      do
      {
    
        printf("\nDo you want to rent a Pentium or a Pentium II system? ");
        printf("\nEnter 'S' for a Pentium or 'P' for a Pentium II: ");
        getchar();
        computer = getchar();
    
        printf("\nEnter the number of days you want to rent computer: ");
        scanf("%lf", &days);
    
        if ((computer == 'S') || (computer == 's')) //Pentium
        {
             printf("\nDo you want to rent a printer (y/n): ");
             getchar();
             prin = getchar();
    
             if ((prin == 'Y') || (prin == 'y')) //Printer
             {
                 printf("\nDo you want to rent a monitor (y/n): ");
                 getchar();
                 mon = getchar();
    
                 if ((mon == 'Y') || (mon == 'y')) //Monitor
                 {
                      monitor += 15.00;
                      sub_total = monitor * days;
                 }
                 else //No Monitor
                 {
                      printer += 13.50;
                      sub_total = printer * days;
                 }
             }
             else //No Printer
             {
                 printf("\nDo you want to rent a monitor (y/n): ");
                 getchar();
                 mon = getchar();
    
                 if ((mon == 'Y') || (mon == 'y')) //Monitor or Not
                 {
                     monitor = 12.50;
                     sub_total = monitor * days;
                 }
                 else //No Monitor
                 {
                     p += 10.50;
                     sub_total = p * days;
                 }
             }
        printf("\nYour subtotal is $ %7.2f", sub_total);
        ++pentium;
        }
        else // Pentium II
        {
            printf("\nDo you want to rent a printer (y/n): ");
            getchar();
            prin = getchar();
    
            if ((prin == 'Y') || (prin == 'y')) //Printer
            {
                  printf("\nDo you want to rent a monitor (y/n): ");
                  getchar();
                  mon = getchar();
    
                  if ((mon == 'Y') || (mon == 'y')) //Monitor
                  {
                        monitor += 21.00;
                        sub_total = monitor * days;
                  }
                  else //No Monitor
                  {
                        printer += 19.00;
                        sub_total = printer * days;
                  }
            } 
            else //No Printer
            { 
                   printf("\nDo you want to rent a monitor (y/n): ");
                   getchar();
                   mon = getchar();
    
                   if ((mon == 'Y') || (mon == 'y')) //Monitor or Not
                   {
                         monitor = 18.50;
                         sub_total = monitor * days;
                   }
                   else //No Monitor
                   {
                         p += 15.50;
                         sub_total = p * days;
                   }
            }
        printf("\nYour subtotal is $ %7.2f", sub_total);
        ++pentium2;
        } 
        printf("\nDo you want to rent another computer?(y/n) ");
        getchar();
        response = getchar();
        total += sub_total;
        ++customer;
        }
        while ((response == 'Y') || (response == 'y'));
        printf("\nThe total number of customers are: %d", customer);
        printf("\nThe total number of Pentium's rented are: %d", pentium);
        printf("\nThe total number of Pentium II's rented are: %9d", pentium2);
        printf("\nThe total rental charges for all the computers: %17.2f\n", total);
        }
    
     
  2. Crowe182

    Crowe182 Thread Starter

    Joined:
    Oct 7, 2003
    Messages:
    14
    ~BUMP~

    Or does anyone know how to clear the getchar() value for the computer type statement after the loop starts over?
    See below for the statement I need to clear in the loop.

    Thanks....
    Code:
    do
      {
        /* this is the question I want to clear out the getchar() */
        printf("\nDo you want to rent a Pentium or a Pentium II system? ");
        printf("\nEnter 'S' for a Pentium or 'P' for a Pentium II: ");
        getchar();
        computer = getchar();
    
        printf("\nEnter the number of days you want to rent computer: ");
        scanf("%lf", &days);
    
    
     
  3. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Didn't look at your code, but in addition to asking here, ask at www.hardforum.com in the webmastering and programming forum.
     
  4. codejockey

    codejockey

    Joined:
    Feb 11, 2002
    Messages:
    1,405
    Apologies if I have misunderstood your question, but I'l try to help, anyway. I think the main problem is the following:

    getchar () reads the next character from stdin; note that newline ("\n") is a character. When you prompt the user to enter either "S" or "P", the first getchar () following the prompt reads the input character and throws it away (does not assign it to a variable). The next getchar () reads the terminating newline character ("\n") that follows the user's response, and assigns it to the variable "computer". Since the newline character doesn't match either 'S' or 's', your first if statement is skipped, and the else is executed. You have the same problem with your logic that asks for the printer selection.

    You also might consider moving the initializations of variables such as computer inside the main do loop. You can do this by initializing response to 'Y', and changing the do-while loop to a while-loop such as:
    Code:
    response = 'Y';
    while (response == 'Y')
    {
        computer = 0;
        etc ...
    }
    
    Another suggestion: try running your program under a debugger such as gdb in order to figure out what's going on (beats inserting printf statements everywhere!).

    Hope this helps -- and apologies if I have misunderstood the code. This is strictly a QND ('quick and dirty" :)) response.
     
  5. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    Hi Crowe,

    One thing that seems odd to me is why you have opted for the nested if statements in the form you have above.

    You first ask for the computer type and number of days, then according to this choice you go on to ask whether a printer is required duplicated for each processor type, then according to the printer choice you ask about a monitor which now means four times. A lot of this code is repeated for each processor type and each printer type unnecessarily.

    Why not simply make all 3 choices once before any logical billing is applied? Then work out the costs according to these 3 choice variables. If you put the costs for each unit required into their own variables before performing any totalling and updating of your overall database variables, you could even report back the cost to your customer for their go ahead. When they ok the order you can then go on to finalise it and update your database variables.
    ____________________________

    Collect information stage: replies will be stored in "processor", "printer" and "monitor"

    "Do you want Pentium or PentiumII, or [P]?" (store in "processor" variable)
    "Do you want printer, [Y] or [N]?" (store in "printer" variable)
    "Do you want monitor, [Y] or[N]?" (store in "monitor" variable)


    Act on it: costs will be stored in "processor_cost", "printer_cost" and "monitor_cost"

    If processor == 'S' // Pentium
    ...If printer == 'Y'
    ......If monitor == 'Y' calculate all charges and store
    ......else calculate all charges and store
    ...else
    ......If monitor == 'Y' calculate all charges and store
    ......else calculate all charges and store
    else // PentiumII
    ...If printer == 'Y'
    ......If monitor == 'Y' calculate all charges and store
    ......else calculate all charges and store
    ...else
    ......If monitor == 'Y' calculate all charges and store
    ......else calculate all charges and store


    Report back for confirmation:
    Display variables for transaction costs
    "Do you wish to go ahead with the rental, [Y] or [N]?" (store in "confirm" variable)


    Update data:
    If confirm == 'Y'
    ...update all necessary variables with information previously stored in "processor_cost", "printer_cost" and "monitor_cost"


    Print out customer's bill and report your own overall data as before:



    Separating each phase like this will make your code easier to follow and debug, and should make it more efficient with less duplication of code.

    But then I could be just being overly fussy though, it has been known! ;)
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/170100

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice