Make simple programs start over.

This is a discussion on Make simple programs start over. within the C++ Programming forums, part of the General Programming Boards category; I just started to learn C++ and I am using Dev-C++ and the command window never stayed open after the ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    1

    Make simple programs start over.

    I just started to learn C++ and I am using Dev-C++ and the command window never stayed open after the programs finished until I added the pause. Is there a way for the program to start it self over after I insert the feet so I don't have to keep reopening it?


    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        double f;// feet
        double m; // meters
        
        cout << "Enter the length in feet: " ;
        cin >> f; // reads number of feet
        
        m = f / 3.28; //Converts feet to meters
        cout << f << " feet is " << m << " meters.\n" ;
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You want to read a tutorial about loops.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    You could do something real simple n' sloppy like that below though I wouldn't call it the proper method.

    Code:
    #include <iostream>
    using namespace std;
    
        double f=1;
        double m; // meters
        
    int main() {
     while (f!=0)
     {
        cout << "Enter the length in feet: " ;  //0 to exit
        cin >> f; // reads number of feet
        
        m = f / 3.28; //Converts feet to meters
        cout << f << " feet is " << m << " meters.\n" ;
        cin.get();
     }
        return 0;
    }

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    or you could use an infinite for loop:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        for (;;)
        {
            double f;// feet
            double m; // meters
        
            cout << "Enter the length in feet: " ;
            cin >> f; // reads number of feet
        
            m = f / 3.28; //Converts feet to meters
            cout << f << " feet is " << m << " meters.\n" ;
            system("pause");
        }
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    if you want a clean display again add system("cls"); after system("pause");

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Less system calls. system() is unsafe, I am pretty sure there is a FAQ why. Use std::cin.get();

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    When i want something to ask me do i want to start again i use a simple loop

    Code:
    while (again == 'y')
    {
                    cout<<"Do you want to play again? (y/n)"<<endl;
    	cin>>again;
    
    }
    Just wrap it around your whole program, im sure i will get slated for this method but it works and if it aint broke dont fix it!

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
    while (again == 'y')
    {
        
        for (;;)
        {
            double f;// feet
            double m; // meters
        
            cout << "Enter the length in feet: " ;
            cin >> f; // reads number of feet
        
            m = f / 3.28; //Converts feet to meters
            cout << f << " feet is " << m << " meters.\n" ;
            system("pause");
        }
     cout<<"Do you want to play again? (y/n)"<<endl;
    	cin>>again;
    
    }
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    I don't see anything wrong with that method; except I don't see where again was actually declared. Otherwise, it looks like a clean and simple loop. Of course, I've been wrong so many times before!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make my program run on windows start up
    By kantze in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-24-2007, 11:14 AM
  2. Can't make a simple Project work
    By kantze in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2007, 02:21 AM
  3. How make dll in in simple c\c++?
    By awm9 in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2004, 02:31 PM
  4. Replies: 4
    Last Post: 07-05-2004, 07:41 PM
  5. Hints required to complete simple programs
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:20 PM

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