how to loop a program from the start

This is a discussion on how to loop a program from the start within the C++ Programming forums, part of the General Programming Boards category; could somebody please give me an idea of how i should loop my progam ive looked through the tutorials and ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    how to loop a program from the start

    could somebody please give me an idea of how i should loop my progam ive looked through the tutorials and when i try to get my program to loop it gives error messages here is my code
    Code:
    // PROGRAM TO WORK MATERIALS OUT FOR A CEILING
    
    #include <iostream>
     
    using namespace std;
    
    int main()
    {
    char option;
    float long_wall;
    float short_wall;
    int atile, btile, totaltile;
    float runnersinglerow, rowsofrunner,totalrunner;
    float a1200, b1200, total1200;
    float a600, b600, total600;
     
    cout<< "please enter the the length of the longest wall if the\n";
    cout<<" sofit is concerete otherwise enter the length of the wall\n";
    cout<<"opposite the joists.please note always round up\n";
    cin>>long_wall;
    cin.ignore();
    
    cout<< "now enter the lenght of the other wall.\n";
    cin>>short_wall;
    cin.ignore();
    
    runnersinglerow=long_wall/3600;
    rowsofrunner=short_wall/1200;
    totalrunner=runnersinglerow*rowsofrunner;
    cout<<"you will need... "<<totalrunner<<" runners\n";
    a1200=(long_wall/600)+0.5;
    b1200=(short_wall/1200);
    total1200=a1200*b1200;
    cout<<"you will need... "<<total1200<<" 1200 bars\n";
    a600=long_wall/1200;
    b600=short_wall/1200+0.5;
    total600=a600*b600*2;
    cout<<"you will need... "<<total600<<" 600 bars\n";
    atile=long_wall/600+1;
    btile=short_wall/600+1;
    totaltile=atile*btile;
    cout<<"you will need... "<<totaltile<<" tiles\n";
    cout<<"dont forget to round the numbers up as i cant do it yet\n";
    cin.get();
    cout<<"do you want to try another?\n";
    cout<<"y\n";
    
    }
    its a program to make my work easier but it will be more ecconomic if i can keep it open and ready to accept a new job.
    the char option is left over from when i tried to get a yes/no loop. thank you in advance

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ahh ill try that i used a do while loop for some strange reason thank you

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You can use a 'while' loop. For example:
    Code:
    while(true)
    {
         cout << "Enter your name:"
         cin >> blah;
    
         if(blah=="exit")
         {
                break;
         }
    }
    You can use the 'break' keyword to break the loop.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Code:
    while(true)
    {
         cout << "Enter your name:"
         cin >> blah;
    
         if(blah=="exit")
         {
                break;
         }
    }
    Why wasting while loop condition?

    It should be like this:
    Code:
    do
    {
         cout << "Enter your name:"
         cin >> blah;
    
      
    }while(!blah); //Note the ;
    [EDIT]
    A better way can be:
    Code:
    bool YesNo(char* prompt)
    {
          cout << prompt;
          char yesno;
          cin.ignore(cin.rdbuf()->in_avail());
          yesno = cin.get();
          cin.ignore(cin.rdbuf()->in_avail());
          return(yesno == 'Y' || yesno == 'y');
    }
    int main(){
          do{
    
                //Your program
    
          }while(YesNo("Restart? (Y / Any key) "));
          
          return 0;
    }
    Last edited by siavoshkc; 10-03-2006 at 03:01 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Well, there are many ways to go about doing this, that was just my way. Don't bash it by saying it's wrong, because it's not.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by siavoshkc
    Code:
    while(true)
    {
         cout << "Enter your name:"
         cin >> blah;
    
         if(blah=="exit")
         {
                break;
         }
    }
    Why wasting while loop condition?

    It should be like this:
    Code:
    do
    {
         cout << "Enter your name:"
         cin >> blah;
    
      
    }while(!blah); //Note the ;
    SiavoshKC, I don't see any reason, why it's better than while loop.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I don't see any reason, why it's better than while loop.
    It's shorter, more understandable, doesn't make a warning in MSVC 2005 (v8.0) and not bug prone. You should exit a loop by its own condition and try not using break inside it where possible.

    [I edited last post]
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    They both work, and get the job done, nothing more to talk about. Who cares if it's .000001 second faster, if it does what you want it to do, then be happy!
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  10. #10
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Maxorator, the two given methods are identical once compiled. Either method is perfectly valid and it comes down to the situation in which you require the specified loop. In your case it would be a much wiser idea to use siavoshkc's do...while loop as it logically fits the program better.

    The thing to remember is do...while loops are always executed at least once. At the end of the loop the while statement is tested and returns if necessary to the top of the loop. This is different from a while loop which first checks its condition and then executes its code (Sentral's example). Siavoshkc points out that Sentral's example doesn't make sense in this case purely because of the redundancy of checking while(true) at the beginning of the loop.

    Also, while it is good to avoid breaks if not necessary, they do have their places so do not completely shun them away. Break statements (along with the continue statement) are especially useful for error-checking conditions inside of a loop so as to avoid execution of any further instructions.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    They both work, and get the job done, nothing more to talk about. Who cares if it's .000001 second faster, if it does what you want it to do, then be happy!
    So you say it is not important how to write the code, it should just get the job done. Right?
    It is a small problem for small projects and a big trouble in big projects.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    So you say it is not important how to write the code, it should just get the job done. Right?
    Yes. That's exactly what I'm saying. Sure there are better ways, but finding a way that works is half the fun (and the most important).
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    hey guys thank you for your help.
    i tried the do while loop but an error happend said expected a while before the second cout? i will try again with the ways you have told me and thank you again

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    i tried the do while loop but an error happend said expected a while before the second cout? i will try again with the ways you have told me and thank you again
    Post your code.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. start a program with space in the path
    By cindyding in forum C Programming
    Replies: 6
    Last Post: 12-28-2006, 10:50 AM
  3. Need help with a loop program
    By hieugene in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2006, 02:42 AM
  4. How to start program from subfolder
    By trancedeejay in forum C Programming
    Replies: 2
    Last Post: 04-01-2006, 02:39 PM
  5. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21