-5

Very new to programming and l can't find any basic explanation online or code that will work well for what l need. I have a fairly long piece of programme (approx 300 lines) it all works. This is the structure to give an idea:

#include <iostream>   
#include <stdlib.h>    
#include <time.h>      
#include <vector>      
#include <algorithm>   

using namespace std;   

int main() 
{
      //code....

    { 
         //code... etc...
    }

}

I want to ask the user to repeat the programme. If enters y, then repeat int main up to the point of asking the same repeat question again. Else to cout<< "e.g. Thank you, goodbye";

Mike Ericson
  • 23
  • 1
  • 2
  • 7

3 Answers3

2
#include <iostream>   
#include <stdlib.h>    
#include <time.h>      
#include <vector>      
#include <algorithm>   

//using namespace std;   <--- Don't use using namespace std, it pollutes the namespace

void repeat()
{
   //... code to repeat
}

int main() 
{
      //code....
    char answer;
    while((std::cin >> answer) != 'y')
    { 
        repeat();
    }
}
user2970916
  • 1,146
  • 3
  • 15
  • 37
  • To me this looks like l have to place my entire code that is already enclosed within int main () into the void repeat () brackets? Sorry if l am mistaken, but this looks a very long way to to it – Mike Ericson Feb 26 '15 at 19:25
  • @MikeEricson Yes. `repeat` is called inside the loop so all the code inside the *definition* of `repeat` will be executed inside the loop. – Emil Laine Feb 26 '15 at 19:29
2
#include <iostream>
#include <conio.h>

using namespace std;

//Class
class DollarToRs {
  public:

      int Dollar;
      int Rs;
      int ToRs;
      ConversionToRs() {
      cout << "Enter the amount of Dollar: ";
      cin >> Dollar;
      ToRs = Dollar * 154;
      cout << "This is the total amount in PKR: " << ToRs <<endl;
      }

};

int main()
{
  //Dollar Convertion Function
  DollarToRs convert;
  convert.ConversionToRs();


  //For Repeating Program

  int repeat;
  int exit;

  cout << "To repeat program enter 1" <<endl;
  cin >> repeat;

  while (repeat == 1) {
    convert.ConversionToRs();
    cout << "To repeat program enter 1" <<endl;
    cin >> repeat;
  }
  exit =0;

  if (exit == 0) {

  }

  getch();
  return 0;
}
1

Here's an example of a simple solution:

int main()
{
    for (;;) // "infinite" loop (while (true) is also possible)
    {
        // stuff to be repeated here

        cout << "Repeat? [y/n]" << endl;
        char answer;
        cin >> answer;
        if (answer == 'n')
            break; // exit loop
    }              // else repeat
    cout << "Thank you, goodbye" << endl;
}

Here's another one:

int main()
{
    bool repeat = true;
    while (repeat)
    {
        // stuff to be repeated here

        cout << "Repeat? [y/n]" << endl;
        char answer;
        cin >> answer;
        repeat = answer == 'y';
    }
    cout << "Thank you, goodbye" << endl;
}

As a side note, don't do this: #include <stdlib.h>. In C++ you should use the c prefixed header file names when using the C headers: #include <cstdlib> and #include <ctime>.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • It says error: expected unqualified-id before')' token. on the first line ... for (;;) – Mike Ericson Feb 26 '15 at 19:11
  • @user2970916 Yeah that's also possible, added to answer. – Emil Laine Feb 26 '15 at 19:14
  • @MikeEricson Also, added a side note about your includes. – Emil Laine Feb 26 '15 at 19:19
  • @zenith Thanks very much. I am new to programming hence why l need all the help l can get. Do l place bool repeat = true; while (repeat) before or after int main; – Mike Ericson Feb 26 '15 at 19:22
  • @MikeEricson Your program starts execution from `int main() {` so in this case you'd want them *inside* the `main` function (between the `{` and `}`). – Emil Laine Feb 26 '15 at 19:25
  • Yes, that's working but it repeats when l type in y AND when n is typed. – Mike Ericson Feb 26 '15 at 19:28
  • Sorry! Error in the compiler! It does work. And your my winning answer. Well done! – Mike Ericson Feb 26 '15 at 19:30
  • @MikeEricson Excellent. This is a fairly basic question though and I'd really recommend you pick up a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn the basics of the language first before coming to ask questions here that are already answered in every C++ introductory book and course. That's the reason for the the downvotes of your question as well in case you were wondering. – Emil Laine Feb 26 '15 at 19:35