| | |
C Help!!!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2003
Posts: 6
Reputation:
Solved Threads: 0
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
_________
Thanks,
Crowe182
_________
C Syntax (Toggle Plain Text)
#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); }
Last edited by Crowe182; Oct 7th, 2003 at 2:14 pm.
•
•
Join Date: Feb 2003
Posts: 282
Reputation:
Solved Threads: 6
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:
Found it.
When typing code, wrap it in [CODE] and [/CODE] tags; and then indent stuff using the [INDENT] and [/INDENT]; for example:
C Syntax (Toggle Plain Text)
// This is some code: if( something == true || somethingelse != false ) {<blockquote> // This has been indented using the Indent tags. printf('blah blah blah'); </blockquote>}
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.
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
![]() |
Other Threads in the C Forum
- Previous Thread: Array limit
- Next Thread: Clear getchar in a loop
| Thread Tools | Search this Thread |
#include * api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory dynamic execv feet fgets file floatingpointvalidation fork framework frequency function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socket socketprogramming standard strchr string suggestions system test testautomation testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






