I am writing this program for my computer class. I am having a problem with my do/while loop. I can go through my main IF statement, but when I go through the loop a second time, I enter 'S' os 's' for computer type it skips past the main IF statement. Do i need to clear my computer variable before going through the loop again, if so how do I do that. Please help.

Thanks,

Crowe182

_________

#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);
    }

Recommended Answers

All 4 Replies

Ergh, It's very difficult to read your code like that. Isn't there a code option or something?

Found it.

When typing code, wrap it in [CODE] and [/CODE] tags; and then indent stuff using the [INDENT] and [/INDENT]; for example:

// This is some code:

if( something == true || somethingelse != false )
{
[INDENT]
// This has been indented using the Indent tags.
printf('blah blah blah');
[/INDENT]
}

OK got the changes made. How is that??

Remember, you can also go to the user control panel, and somewhere in your preference options, you can set to use a WYSIWYG editor to edit posts. Unfortunately, I have to run off to class in a few minutes, so don't really have time to look at this program right now. :(

no problem, just let me know when you get a chance.

Thanks....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.