Hello,
I am need of urgent help.I've made a program to generate bills (using goto statements) but my teacher has asked me to remove those and use something else in place of it.The problem is that I have tried using separate functions for that (It's not working properly) and I'm unable to think of any other way to correct.
Plz Plz Plz help me...
this is really urgent...
Thanks in advance...
Here's the original code...

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip.h>

//global variable declaration
int k=7,r=0,flag=0;

struct date
{
	int mm,dd,yy;
};
ofstream fout;
ifstream fin;
class item
{
	int itemno;
	char name[25];
	date d;
public:
	void add()
	{
		cout<<"\n\n\tItem No: ";
		cin>>itemno;
		cout<<"\n\n\tName of the item: ";
		gets(name);
		cout<<"\n\n\tManufacturing Date(dd-mm-yy): ";
		cin>>d.mm>>d.dd>>d.yy;
	}
	void show()
	{
		cout<<"\n\tItem No: ";
		cout<<itemno;
		cout<<"\n\n\tName of the item: ";
		cout<<name;
		cout<<"\n\n\tDate : ";
		cout<<d.mm<<"-"<<d.dd<<"-"<<d.yy;
	}
	void report()
	{
		gotoxy(3,k);
		cout<<itemno;
		gotoxy(13,k);
		puts(name);
	}
	int retno()
	{
		return(itemno);
	}
};
class amount: public item
{
	float price,qty,tax,gross,dis,netamt;
public:
	void add();
	void show();
	void report();
	void calculate();
	void pay();
	float retnetamt()
	{
		return(netamt);
	}
}amt;
void amount::add()
{
	item::add();
	cout<<"\n\n\tPrice: ";
	cin>>price;
	cout<<"\n\n\tQuantity: ";
	cin>>qty;
	cout<<"\n\n\tTax percent: ";
	cin>>tax;
	cout<<"\n\n\tDiscount percent: ";
	cin>>dis;
	calculate();
	fout.write((char *)&amt,sizeof(amt));
	fout.close();
}
void amount::calculate()
{
	gross=price+(price*(tax/100));
	netamt=qty*(gross-(gross*(dis/100)));
}
void amount::show()
{
	fin.open("itemstore.dat",ios::binary);
	fin.read((char*)&amt,sizeof(amt));
	item::show();
	cout<<"\n\n\tNet amount: ";
	cout<<netamt;
	fin.close();
}
void amount::report()
{
	item::report();
	gotoxy(23,k);
	cout<<price;
	gotoxy(33,k);
	cout<<qty;
	gotoxy(44,k);
	cout<<tax;
	gotoxy(52,k);
	cout<<dis;
	gotoxy(64,k);
	cout<<netamt;
	k=k+1;
	if(k==50)
	{
		gotoxy(25,50);
		cout<<"PRESS ANY KEY TO CONTINUE...";
		getch();
		k=7;
		clrscr();
		gotoxy(30,3);
		cout<<" ITEM DETAILS ";
		gotoxy(8,5);
		cout<<"NUMBER";
		gotoxy(13,5);
		cout<<"NAME";
		gotoxy(23,5);
		cout<<"PRICE";
		gotoxy(33,5);
		cout<<"QUANTITY";
		gotoxy(44,5);
		cout<<"TAX";
		gotoxy(52,5);
		cout<<"DEDUCTION";
		gotoxy(64,5);
		cout<<"NET AMOUNT";
	}
}
void amount::pay()
{
	show();
	cout<<"\n\n\n\t\t*********************************************";
	cout<<"\n\t\t                 DETAILS                  ";
	cout<<"\n\t\t*********************************************";
	cout<<"\n\n\t\tPRICE                     :"<<price;
	cout<<"\n\n\t\tQUANTITY                  :"<<qty;
	cout<<"\n\t\tTAX PERCENTAGE              :"<<tax;
	cout<<"\n\t\tDISCOUNT PERCENTAGE         :"<<dis;
	cout<<"\n\n\n\t\tNET AMOUNT              :Rs."<<netamt;
	cout<<"\n\t\t*********************************************";
}
void main()
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout<<setprecision(2);
	menu:						//LABEL 1
	clrscr();
	gotoxy(25,2);
	cout<<"Canteen Billing ";
	gotoxy(20,3);
	cout<<"===========================\n\n";
	cout<<"\n\t\t1.Bill Report\n\n";
	cout<<"\t\t2.Add/Remove/Edit Item\n\n";
	cout<<"\t\t3.Show Item Details\n\n";
	cout<<"\t\t4.Exit\n\n";
	cout<<"\t\tPlease Enter Required Option: ";
	int ch,ff;
	float gtotal;
	cin>>ch;
	switch(ch)
	{
		case 1:
		ss:			//LABEL 2
		clrscr();
		gotoxy(25,2);
		cout<<"Bill Details";
		gotoxy(25,3);
		cout<<"================\n\n";
		cout<<"\n\t\t1.All Items\n\n";
		cout<<"\t\t2.Back to Main menu\n\n";
		cout<<"\t\tPlease Enter Required Option: ";
		int cho;
		cin>>cho;
		if(cho==1)
		{
			clrscr();
			gotoxy(30,3);
			cout<<" BILL DETAILS ";
			gotoxy(3,5);
			cout<<"ITEM NO";
			gotoxy(13,5);
			cout<<"NAME";
			gotoxy(23,5);
			cout<<"PRICE";
			gotoxy(33,5);
			cout<<"QUANTITY";
			gotoxy(44,5);
			cout<<"TAX %";
			gotoxy(52,5);
			cout<<"DISCOUNT %";
			gotoxy(64,5);
			cout<<"NET AMOUNT";
			fin.open("itemstore.dat",ios::binary);
			if(!fin)
			{
				cout<<"\n\nFile Not Found...";
				goto menu;}
				fin.seekg(0);
				gtotal=0;
				while(!fin.eof())
				{
					fin.read((char*)&amt,sizeof(amt));
					if(!fin.eof())
					{
						amt.report();
						gtotal+=amt.retnetamt();
						ff=0;
					}
					if(ff!=0) gtotal=0;
				}
				gotoxy(17,k);
				cout<<"\n\n\n\t\t\tGrand Total="<<gtotal;
				getch();
				fin.close();
			}
			if(cho==2)
			{
				goto menu;
			}
			goto ss;
		case 2:
			db:					//LABEL 3
			clrscr();
			gotoxy(25,2);
			cout<<"Bill Editor";
			gotoxy(25,3);
			cout<<"=================\n\n";
			cout<<"\n\t\t1.Add Item Details\n\n";
			cout<<"\t\t2.Edit Item Details\n\n";
			cout<<"\t\t3.Delete Item Details\n\n";
			cout<<"\t\t4.Back to Main Menu ";
			int apc;
			cin>>apc;
			if (apc == 1)
			{
				fout.open("itemstore.dat",ios::binary|ios::app);
				amt.add();
				cout<<"\n\t\tItem Added Successfully!";
				getch();
				goto db;
			}
			else if (apc==2)
			{
				int ino;
				flag=0;
				cout<<"\n\n\tEnter Item Number to be Edited :";
				cin>>ino;
				fin.open("itemstore.dat",ios::binary);
				fout.open("itemstore.dat",ios::binary|ios::app);
				if(!fin)
				{
					cout<<"\n\nFile Not Found...";
					goto menu;
				}
				fin.seekg(0);
				r=0;
				while(!fin.eof())
				{
					fin.read((char*)&amt,sizeof(amt));
					if(!fin.eof())
					{
						int x=amt.item::retno();
						if(x==ino)
						{
							flag=1;
							fout.seekp(r*sizeof(amt));
							clrscr();
							cout<<"\n\t\tCurrent Details are\n";
							amt.show();
							cout<<"\n\n\t\tEnter New Details\n";
							amt.add();
							cout<<"\n\t\tItem Details editted";
						}
					}
					r++;
				}
				if(flag==0)
				{
					cout<<"\n\t\tItem No does not exist...Please Retry!";
					getch();
					goto menu;
				}
				fin.close();
				getch();
				goto menu;
			}
			else if(apc == 3)
			{
				int ino,flag=0;
				cout<<"\n\n\tEnter Item Number to be deleted :";
				cin>>ino;
				fin.open("itemstore.dat",ios::binary);
				if(!fin)
				{
					cout<<"\n\nFile Not Found...";
					goto menu;
				}
				fstream tmp("temp.dat",ios::binary|ios::out);
				fin.seekg(0);
				while(fin.read((char*)&amt, sizeof(amt)))
				{
					int x=amt.item::retno();
					if(x!=ino)
					tmp.write((char*)&amt,sizeof(amt));
					else
					{
						flag=1;
					}
				}
				fin.close();
				tmp.close();
				fout.open("itemstore.dat",ios::trunc|ios::binary);
				fout.seekp(0);
				tmp.open("temp.dat",ios::binary|ios::in);
				if(!tmp)
				{
					cout<<"Error in File";
					goto db;
				}
				while(tmp.read((char*)&amt,sizeof(amt)))
				fout.write((char*)&amt,sizeof(amt));
				tmp.close();
				fout.close();
				if(flag==1)
				cout<<"\n\t\tItem Succesfully Deleted";
				else if (flag==0)
				cout<<"\n\t\tItem does not Exist! Please Retry\a";
				getch();
				goto db;
			}
			else if (apc == 4)
				goto menu;
			else
			{
				cout<<"\n\n\t\tWrong Choice!!! Retry";
				getch();
				goto menu;
			}
		case 3:
			clrscr();
			flag=0;
			int ino;
			cout<<"\n\n\t\tEnter Item Number :";
			 cin>>ino;
			fin.open("itemstore.dat",ios::binary);
			if(!fin)
			{
				cout<<"\n\nFile Not Found...\nProgram Terminated!";
				goto menu;
			}
			fin.seekg(0);
			while(fin.read((char*)&amt,sizeof(amt)))
			{
				int x=amt.item::retno();
				if(x==ino)
				{
					amt.pay();
					flag=1;
					break;
				}
			}
			if(flag==0)
			cout<<"\n\t\tItem does not exist....Please Retry!";
			getch();
			fin.close();
			goto menu;
		case 4:
			clrscr();
			gotoxy(20,20);
			 cout<<"ARE YOU SURE, YOU WANT TO EXIT (Y/N)?";
			char yn;
			cin>>yn;
			if((yn=='Y')||(yn=='y'))
			{
				gotoxy(12,20);
				cout<<"SPECIAL THANKS TO RITU MA'M FOR GIVING US THE GUIDING LIGHT";
				getch();
				clrscr();
				gotoxy(15,20);
				cout<<"COPYRIGHTS (THOUGH NOT TAKEN) - 2010-2011";
				getch();
				clrscr();
				gotoxy(12,20);
				cout<<"CREATED BY CHIRAG MITTAL";
				getch();
				exit(0);
		 }
		 else if((yn=='N')||(yn=='n'))
		 goto menu;
		 else{goto menu;}
	default:
		cout<<"\n\n\t\tWrong Choice....Please Retry!";
		getch();
		goto menu;
	}
}

Your instructor is correct. You shouldn't use them unless you absolutely have to; they're confusing and can be dangerous.

Except for jumping around between scopes illegally, there really isn't anything you can do with a goto that can't be done with a control structure. In your case, you want a loop. Read this.

Edited 5 Years Ago by Fbody: n/a

You can use do while loop for the whole block and give the condition in the do while loop as...

}while(yn!='y');

Edited 5 Years Ago by Arbus: n/a

Thank you guys for replying so quick but I also thought about that...
the thing is that I have used many and many goto statements and replacing them all with do..while loops is a messy work and I am getting it all wrong....
so can you pls tell how to use the loop in any one of the the functions???
plz....

A do-while is not the only loop type and is not necessarily the correct loop type every time. There are also while and for loops. Which one you use depends on your situation.

The while and do-while loops are what are called "indeterminate" loops. They are best for when you don't know how many times a loop should run. Which one you use in particular depends on the type of indeterminate behavior you are looking for. If you have a section of code that must execute and could potentially be required to repeat, use a do-while loop. If the section of code is optional and could potentially repeat, use a while loop.

The for loop is what is called a "determinate" loop. It is best used when you know how many times the loop needs to repeat (iterate).

Did you read the link I provided you earlier?

Dear Fbody,
I read the link provided by you.
It tells me about various kinds of loops and all...
The thing is that I know these loops and I just want an example for the code above for how to replace the goto with some other loops...
the problem for me is that I have used way too many goto statements and I'm unable to think of a way to replace them..
all I want is a example of some section of the above code so that i can implement that example for the rest of the code...
thank you in advance....

Since you are a student, I can't actually give you code that solves your problem, it's against forum policy and it is generally considered cheating under schools' "Truth in Learning" policies.

All that I can say is look at what the goto is supposed to accomplish. Can you replace some of them with functions instead of loops?

Based on what I'm seeing, the code controlled by "menu:" should probably be a function. The code controlled by "ss" and "db" should probably be loops.

I suspect that if you focus on converting "menu" first, it will make "ss" and "db" easier to convert.

Here is a, hopefully general-enough, example to keep you out of trouble:
"Spaghetti" Code (what yours is):

#include <iostream>

using namespace std;

int main() {
appLoop:
  cout << "Beginning main application loop..." << endl;

  cout << "Executing someSegment" << endl;
  goto someSegment;
  
  goto appLoop;

someSegment:
  cout << "In someSegment. Returning to main loop..." << endl;
  goto appLoop;

  return 0;
}

Please note, I am aware that this is an infinite loop. But it effectively demonstrates the concept.

"Structured" Code (what it should be):

#include <iostream>

using namespace std;

void someSegment();

int main() {
  bool keepGoing = true;
  do {
    cout << "Beginning main application loop..." << endl;

    cout << "Executing someSegment()" << endl;
    someSegment();
  
  } while(keepGoing);

  return 0;
}

void someSegment() {
  cout << "In someSegment()." << endl;
}

Edited 5 Years Ago by Fbody: n/a

hello chirag mittal,
Here is an example of do while loop in your program.

void main()
{  .
   .
   .
 setprecision(2);
do
{  clrscr();
   .
   .
   .
   switch(ch)
   {
    case 1:  ... 
    case 2: ....
     .
     .
     . 
    default:
          ...//remove the goto in default
  }
 }while(yn!='y');
}

the above do while loop displays the first menu(bill reports,show edit item,add/remove etc.,) till you exit.
Likewise replace all goto's with do while

Edited 5 Years Ago by Arbus: n/a

>>Likewise replace all goto's with do while
Not all gotos can be replaced by a do-while loop. Sometimes, they should be replaced by either a function or a different type of loop, it all depends on context. See the example I posted.

The OP uses 3 different labels. Of the 3, at least 1 would be best replaced by a function.

Thanks to all of you guys...
I was able to successfully complete my project work..
all you guys just rock!!

Edited 5 Years Ago by chirag_mittal: n/a

This question has already been answered. Start a new discussion instead.