Thread: A while loop?? homework..tips please.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    A while loop?? homework..tips please.

    Alright, thank you ahead of time.
    First off here is what the program is supposed to do, what i am having trouble with is in bold..

    "Write a program that will accept as input a one character desinator (C,T,S) followed by the number of minutes a vehicle has been in the lot. THe program should then compute the appropriate charge and print a ticket for the custormer. Any part of an hour is to be counted as a full hour. At a later time the program should be modified to loop until the character "x" is entered for input. Use functions as needed! "

    Alright this is my problem, the program displays correctly and executes and all that , BUT I just don't know how to do the loop... i think it has to be a while loop ...

    I know what it should say in english, I think ...

    It should keep asking for C,T, S and the number of minutes..infinitely until I input an "X"..but I dont know how to translate that into code.. here is where I "think" it has to go, but my full program is under it..

    Code:
    void getInput() 
    { 
     
     char vType;
     
     cout << "Enter the type of car"; 
     cout << endl; 
     cout << "(e.g. 'C' for car, 'T' for truck, or 'S' for senior citizen: "; 
     cout << endl;  
     cout << "Press X to exit program : ";
     cin  >> vType; 
     cout << endl; 
     if (tolower(vType) == 'x') 
             { 
      return; 
              } 
     cout <<"Enter the number of minutes the vehicle has been parked: "; 
     cin  >> mtime; 
      
     computePrice(vType,mtime); 
    } 
    int main() 
    { 
     getInput(); 
      
     cin.ignore(); 
     cin.get(); 
      
     return 0; 
    }
    and heres the full beautiful thing...
    Code:
     // Program 4 CSC140-001  3/15/04 
    //A program which prints tickets given an amount of time a car has been parked. 
    //Will read from file a list of various cars and times. 
    
    #include <iostream>  
    #include <fstream> 
    #include <string> 
    
    using namespace std; 
    
    int mtime; 
    
    void printTicket(char vType, int minutes, double p, int hours) 
    { 
      
    
     string carType; 
     if(vType == 'c'){ 
      carType = "Car"; 
     } 
     else if(vType == 't'){ 
      carType = "Truck"; 
     } 
     else if(vType == 's'){ 
      carType = "Senior"; 
     } 
     cout<<" PENTAGON VISITOR PARKING" << endl; 
      cout<<"       Type ..... " << carType << endl; 
      cout<<"       Minutes .. " << minutes<< endl; 
      cout<<"       Hours .... " << hours << endl; 
      cout<<"       Charge ... " << p << endl; 
    } 
      
      
    void computePrice(char vType, int time) 
    { 
     vType = tolower(vType); 
    
     double p = 0.0; 
      //int  h = time/60.0; 
     double h = time/60;
     if(time%60 > 0.0) 
      h++ ; 
     if(vType == 'c'){ 
      if(time <= 120){ 
        p = 0.00; 
       } 
       else if(time > 120 && time <= 300){ 
        p = .50*(h-2.00); 
       } 
       else if(time >300){ 
        p = .25*(h-5)+1.5; 
       } 
      } 
     else if(vType == 't'){ 
      if(time <= 60){ 
       p = 0.00; 
      } 
      else if(time > 60 && time <=180){ 
       p = 1.00*(h-1.00); 
      } 
      else if(time > 180){ 
       p = .75*(h-3)+2.00; 
      } 
     } 
      else if(vType == 's') 
       p = 0.00; 
    
     printTicket(vType,time,p,h); 
    } 
    
    
    void getInput() 
    { 
     
     char vType;
     
     
     cout << "Enter the type of car"; 
     cout << endl; 
     cout << "(e.g. 'C' for car, 'T' for truck, or 'S' for senior citizen: "; 
     cout << endl;  
     cout << "Press X to exit program : ";
     cin  >> vType; 
     cout << endl; 
     if (tolower(vType) == 'x') 
     { 
      return; 
    } 
     cout <<"Enter the number of minutes the vehicle has been parked: "; 
     cin  >> mtime; 
      
     computePrice(vType,mtime); 
    } 
    int main() 
    { 
    
     getInput(); 
      
     cin.ignore(); 
     cin.get(); 
      
     return 0; 
    }
    thanks again!

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    A while loop... like this?

    Code:
    char keyInput = 0; 
    keyInput = kbhit(); 
    
    while (keyInput != 'x')
    {
      //code in loop.. asking questions and such.
    }
    ...i'm not really sure if this is what you mean?

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Alright what I am trying to get it to say is this..

    Enter car type...C T or S...or press x to exit program..(not exactly say that...) but if you enter c t or s , it will then ask for miles which it does, then it calculates them. But when its done, I want it to keep asking for a new type of car., Then miles...and then calculate all the same stuff all over again Then when I've decided i want to end the program after trying,( say all the different types of cars and different minute variations,

    ..i would simple type in x, and then enter, and it would end the loop and the program will close.

    Hope that helps

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Actually, all the code in your get_input function could be put inside a while loop. It could even be a while(true), since the condition that breaks out of the loop already returns out of the function.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Theres one problem...I am very new in this so watch the lingo (terminolgy)..lol thanks again..

    see I dont know actually what to include in a while statement..or write it, ive been looking at tutorials and whatnot on line, but to no avail.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >see I dont know actually what to include in a while statement..
    Anything that evaluates to a boolean value: true or false, 1 or 0, or more informally (and accurate), non-zero or zero.
    My best code is written with the delete key.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, here's an example of a while loop:
    Code:
    bool notDone = true;
    while(notDone)    //As long as notDone == true,
    {                 //it will keep doing the stuff in the code block.
        char myChar;
        std::cin >> myChar;
        if(myChar == 'x')
            notDone = false;
        else
        {
            //Do whatever you want to do with myChar
        }
    }
    That's not necessarily the best way to handle something like that, but I tried to make it illustrate a point.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    still no clue, Ive gotten it to do about nothing worth telling about, a little light and then my building comes crashing down, this is A LOT tougher than I thought.

    I've tried the things suggested, I just really dont know where to put everything. More or less, I am a total Newb ...I apologize

    The bool statement is showing promise, but im just not sure how to really use it,

    .
    Last edited by ftblstr2319; 03-25-2004 at 12:35 AM.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    A while loop looks like the example Hunter2 gave.
    Code:
    while(condition)
    {
        statements
    }
    As long as condition evaluates to true, the statements are executed. Each time the statements are executed, the condition is checked again.

    This will happen an infinite number of times until the condition is no longer true. An example is Hunter2's code. The bool variable notDone is the condition, and it starts off as true. At some point it is given the value false. When that happens, the next time around the loop will not run anymore because the condition is now false.

    There is another way to stop a while loop. If you are in a function, and you call return; (like your code does already), then the loop will not run any more because the code will leave the function immediately. You can also break out of a loop with a break statement, which you should read about when you study up on while loops but is not necessary here.

    So now, the question is what to use as the statements and what to use as the condition:
    Originally posted by jlou
    Actually, all the code in your get_input function could be put inside a while loop. It could even be a while(true), since the condition that breaks out of the loop already returns out of the function.
    It seems I already gave you an answer to that question that should work just fine with the code you posted originally.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Gosh I am a fool it was one simple thing I had to put in....but with your guy's help I've learned a bunch, Alls it seems I had to do is..

    Code:
    void getInput() 
    { 
     
     char vType; 
     while (vType != 'x')
     {
     cout << endl;
     cout << "Enter the type of car"; 
     cout << endl; 
     cout << "(e.g. 'C' for car, 'T' for truck, or 'S' for senior citizen: "; 
     cout << endl; 
     cout << "Press X to exit program : "; 
     cin  >> vType; 
     cout << endl; 
     if (tolower(vType) == 'x') 
     { 
      return; 
     } 
     cout <<"Enter the number of minutes the vehicle has been parked: "; 
     cin  >> mtime; 
      
     computePrice(vType,mtime); 
     }
    }

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I'm glad you figured it out. You can probably ignore the post I just made at the same time as you.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Yes thanks, I just had No real idea what you meant. I had to recrack open the book and just sit and read....

    What really helped was for me just to translate it into english..

    While this isnt x keep going, and if it is STOP..

    THANKs again

  13. #13
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    What book are you using?

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Programming and problem solving with C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM