我有这个代码的麻烦...它显示我的错误:跳转到案例标签[fpermissive]和错误:交叉'int cnt'的初始化..请帮我解决这个错误.Tq。 [英] Im having trouble with this code...it showing me error: jump to case label[fpermissive] and error: crosses intialization of 'int cnt'..please do help me solve this error.Tq.

查看:136
本文介绍了我有这个代码的麻烦...它显示我的错误:跳转到案例标签[fpermissive]和错误:交叉'int cnt'的初始化..请帮我解决这个错误.Tq。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
#include <conio.h>
using namespace std;

void clrscr() {
  #ifdef WINDOWS
  system("cls");
  #endif
  #ifdef LINUX
  system("clear");
  #endif
}

class phoneBook{
	char name[20],phno[15];
	public:
	void getdata();
	void showdata();
	char *getname(){ return name; }
	char *getphno(){ return phno; }
	void update(char *nm,char *telno){
		strcpy(name,nm);
		strcpy(phno,telno);
	}
};

void phoneBook :: getdata(){
	cout<<"\nEnter Name : ";
	cin>>name;
	cout<<"Enter Phone No. : ";
	cin>>phno;
}

void phoneBook :: showdata(){
	cout<<"\n";
	cout<<setw(20)<<name;
	cout<<setw(15)<<phno;
}


int main(){
	phoneBook rec;
	fstream file;
	file.open("d:\\phone.dat", ios::ate | ios::in | ios::out | ios::binary);
	char ch,nm[20],telno[6];
	int choice,found=0;
	while(1){
		clrscr();
		cout<<"\n*****Phone Book*****\n";
		cout<<"1) Add New Record\n";
		cout<<"2) Display All Records\n";
		cout<<"3) Search Telephone No.\n";
		cout<<"4) Search Person Name\n";
		cout<<"5) Update Telephone No.\n";
		cout<<"6) Exit\n";
		cout<<"Choose your choice : ";
		cin>>choice;
		switch(choice){
			case 1 : //New Record
				 rec.getdata();
				 cin.get(ch);
				 file.write((char *) &rec, sizeof(rec));
				 break;

			case 2 : //Display All Records
				 file.seekg(0,ios::beg);
				 cout<<"\n\nRecords in Phone Book\n";
				 while(file){
					file.read((char *) &rec, sizeof(rec));
					if(!file.eof())
						rec.showdata();
				 }
				 file.clear();
				 getch();
				 break;

			case 3 : //Search Tel. no. when person name is known.
				 cout<<"\n\nEnter Name : ";
				 cin>>nm;
				 file.seekg(0,ios::beg);
				 found=0;
				 while(file.read((char *) &rec, sizeof(rec)))
				 {
					if(strcmp(nm,rec.getname())==0)
					{
						found=1;
						rec.showdata();
					}
				 }
				 file.clear();
				 if(found==0)
					cout<<"\n\n---Record Not found---\n";
				 getch();
				 break;

			case 4 : //Search name on basis of tel. no
				 cout<<"\n\nEnter Telephone No : ";
				 cin>>telno;
				 file.seekg(0,ios::beg);
				 found=0;
				 while(file.read((char *) &rec, sizeof(rec)))
				 {
					if(strcmp(telno,rec.getphno())==0)
					{
						found=1;
						rec.showdata();
					}
				 }
				 file.clear();
				 if(found==0)
					cout<<"\n\n---Record Not found---\n";
				 getch();
				 break;

			case 5 : //Update Telephone No.
				 cout<<"\n\nEnter Name : ";
				 cin>>nm;
				 file.seekg(0,ios::beg);
				 found=0;
				 int cnt=0;
				 while(file.read((char *) &rec, sizeof(rec)))
				 {
					cnt++;
					if(strcmp(nm,rec.getname())==0)
					{
						found=1;
						break;
					}
				 }
				 file.clear();
				 if(found==0)
					cout<<"\n\n---Record Not found---\n";
				 else
				 {
					int location = (cnt-1) * sizeof(rec);
					cin.get(ch);
					if(file.eof())
						file.clear();

					cout<<"Enter New Telephone No : ";
					cin>>telno;
					file.seekp(location);
					rec.update(nm,telno);
					file.write((char *) &rec, sizeof(rec));
					file.flush();
				 }
				 break;
			case 6 : goto out;
		}
	}
out:
file.close();
return 0;
}

推荐答案

在案例5中,您声明整数变量 cnt 这是不允许的。它应该在开关之外声明。你也不应该使用 goto 语句,特别是不要在开关 > while loop。
In case 5 you declare the integer variable cnt which is not allowed. It should be declared outside the switch. You should also not use a goto statement, especially not to break out of a switch within a while loop.


我建​​议在那里进行一些重构,例如

I would suggest a bit refactoring there, e.g.
 //...
 bool terminate = false;
 while( ! terminate )
 {
   clrscr();
   show_menu();
   cin>>choice;
   switch(choice)
   {
     case 1 : //New Record
       rec.getdata();
       cin.get(ch);
       file.write((char *) &rec, sizeof(rec));
       break;

     case 2 : //Display All Records
       display_all_records(file);
       getchar();
       break;
     //... use helper functions also in cases 3,4,5
     case 6:
     default:
      terminate = true;
      break;
   } // end of switch
   file.close();
   return 0;
 } // end of main



// helper functions
  void show_menu()
  {
    cout<<"\n*****Phone Book*****\n";
    cout<<"1) Add New Record\n";
    cout<<"2) Display All Records\n";
    cout<<"3) Search Telephone No.\n";
    cout<<"4) Search Person Name\n";
    cout<<"5) Update Telephone No.\n";
    cout<<"6) Exit\n";
    cout<<"Choose your choice : ";
  }
  void display_all_records(fstream & file)
  {
    phoneBook rec;
    file.seekg(0,ios::beg);
    cout<<"\n\nRecords in Phone Book\n";
    while(file)
    {
      file.read((char *) &rec, sizeof(rec));
      if(!file.eof())
        rec.showdata();
    }
    file.clear();
  }
  //...


问题在你的案例5:块。



如果你将
The problem is in your case 5: block.

If you change
int cnt=0;

改为

to

int cnt;
cnt = 0;



问题消失了。基本上,编译器重新组织你的代码,以便在switch语句之前发生 int cnt; 声明。





另一种解决方法是将这个特殊情况的整个内容包含在它自己的块中。



ie


the problem goes away. Basically, the compiler re-organizes your code so that the int cnt; declaration occurs before the switch statement.


The other way you can fix this is to enclose the entire contents of this particular case satement inside it's own block.

i.e

case 5:
 ....
 ....
 ....
 break;

case 6:
 ....
 ....





变为





becomes

case 5:
 {
 ....
 ....
 ....
 }
 break;

case 6:
 ....
 ....


这篇关于我有这个代码的麻烦...它显示我的错误:跳转到案例标签[fpermissive]和错误:交叉'int cnt'的初始化..请帮我解决这个错误.Tq。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆