Hello there!

Right, Im designing a simple program for college. Its just a basic wage input program and it works fine. On the final screen I want the user to have the option of going back to the start of the program. 'Do you wish to start again?(y/n)' so if they type 'y' then they will be brought back to the first screen. Can this be done and if so how?

Any help much appreciated!

Shane

Dani AI

Generated

— you don’t need goto to return the user to the first screen. Wrap your wage input/processing in a small routine and repeat it with a loop. ’s loop idea is the right direction; ’s labeled goto example shows it’s possible in other languages, but is correct that using goto for normal flow makes maintenance harder.

Use a clear loop + a short procedure so each iteration starts from a known state, and validate the Y/N response before acting. The example below keeps the program structure separate from the restart logic and forces a valid answer before continuing:

program WageRestartExample;
var
  resp: string;

procedure RunWage;
begin
  { Put wage input, calculations and final display here.
    Keep local variables inside this procedure so they are reset each call. }
end;

begin
  repeat
    RunWage;
    repeat
      write('Start again? (Y/N): ');
      readln(resp);
    until (Length(resp) > 0) and (resp[1] in ['Y','y','N','n']);
  until resp[1] in ['N','n'];
end.

Troubleshooting tips: always reset or localize variables inside the procedure so old values don’t leak between runs; validate and trim input to handle accidental blanks; avoid recursion or calling your “main” routine recursively to restart; and prefer loops + small procedures for testability and readability. Goto exists in Pascal, but for this kind of repeat/confirm flow a loop is the clearer, safer choice.

Recommended Answers

All 4 Replies

program keepGoing;
uses
  SysUtils;
var
  Response: string;
begin
  Response := 'Y';
  repeat
    // Do your stuff here.
    write('Do you want to continue? ');
    readln(Response);
  until UpperCase(Response) = 'N';
end.

This works too:

int main()
{
begin:

//main code over here

restartchoice:

     char choice;
     cout << "Do you wish to start again?(y/n): ";
     cin >> choice;
	
     switch(choice)
     {
     case 'Y' :
     case 'y' :
          goto begin;
     break;

     case 'N' :
     case 'n' :
          goto end;
     break;

     default:
          cout << "You must either enter Y to start again, or N to end the program." << endl << endl;
     system("pause");
     goto restartchoice;
     }

end:
return 0;
}

Good luck :)

You do realize that this thread is over three years old?

...And that using goto for this instead of a loop is Wrong...

...And that this is the Pascal and Delphi forum...

(But at least you didn't call main(); ;) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.