C++ | Using GoTo Statements

Join Programming and Development Chat Groups on Uphub.io

Introduction

This is a program for simple goto statement in c++. The user inputs the gender and if it is incorrect the it goes to start. The program is extendable. Go enjoy the program. Lets begin……

Program for Goto Statements in C++.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable char.
char gender;
//label for goto
start:
//Input the two numbers save them
cout<<"Enter your gender"<<endl;
cin>>gender;
//switch statement.
switch(gender)
{
case 'm':
     cout<<"You have got 10% discount";
     break;
case 'f':
     cout<<"You have got 20% discount";
     break;
default:
     cout<<"Try again"<<endl;
     goto start;
}
//get character
getch();
}

Output
Enter your gender
a
Try again
Enter your gender
m
You have got 10% discount.

How does it work.

  1. You enter gender.
  2. The switch statement decides the correct discount and if incorrect gender goes back to start.

Extending it.

The program is extendable. This is a basic concept of c++ and can be used in any program. Go extend it..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ and ‘fstream.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare variables.
  5. Declare your label.
  6. Add cout and cin to get data.
  7. Add a switch statement.
  8. Add a goto to label in default.
  9. Get character.

At the end

You learnt creating the C++ program for using goto statements. So now enjoy the program.

Please  share it.

Sharing options: