以下C代码有什么问题。当我编译它时,它表示无法访问的代码。我正在使用Turbo C ++ [英] What is the problem with following C code..It states Unreachable code when I compile it..I am using Turbo C++
本文介绍了以下C代码有什么问题。当我编译它时,它表示无法访问的代码。我正在使用Turbo C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h> ///for input output functions like printf, scanf
#include <stdlib.h>
#include <conio.h>
#include <string.h> ///string operations
/**
function : gotoxy
@param input: x and y coordinates
@param output: moves the cursor in specified position of console
*/
/** Main function started */
int main()
{
FILE *fp, *ft; /// file pointers
char another, choice;
/** structure that represent a employee */
struct emp
{
char name[40]; ///name of employee
int age; /// age of employee
float bs; /// basic salary of employee
};
struct emp e; /// structure variable creation
char empname[40]; /// string to store name of the employee
long int recsize; /// size of each record of employee
/** open the file in binary read and write mode
* if the file EMP.DAT already exists then it open that file in read write mode
* if the file doesn't exit it simply create a new copy
*/
fp = fopen("EMP.DAT","rb+");
if(fp == NULL)
{
fp = fopen("EMP.DAT","wb+");
if(fp == NULL)
{
printf("Connot open file");
exit(1);
}
}
/// sizeo of each record i.e. size of structure variable e
recsize = sizeof(e);
/// infinite loop continues untile the break statement encounter
while(1)
{
system("cls"); ///clear the console window
gotoxy(30,10); /// move the cursor to postion 30, 10 from top-left corner
printf("1. Add Record"); /// option for add record
gotoxy(30,12);
printf("2. List Records"); /// option for showing existing record
gotoxy(30,14);
printf("3. Modify Records"); /// option for editing record
gotoxy(30,16);
printf("4. Delete Records"); /// option for deleting record
gotoxy(30,18);
printf("5. Exit"); /// exit from the program
gotoxy(30,20);
printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
fflush(stdin); /// flush the input buffer
choice = getche(); /// get the input from keyboard
switch(choice)
{
case '1': /// if user press 1
system("cls");
fseek(fp,0,SEEK_END); /// search the file and move cursor to end of the file
/// here 0 indicates moving 0 distance from the end of the file
another = 'y';
while(another == 'y') /// if user want to add another record
{
printf("\nEnter name: ");
scanf("%s",e.name);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter basic salary: ");
scanf("%f", &e.bs);
fwrite(&e,recsize,1,fp); /// write the record in the file
printf("\nAdd another record(y/n) ");
fflush(stdin);
another = getche();
}
break;
case '2':
system("cls");
rewind(fp); ///this moves file cursor to start of the file
while(fread(&e,recsize,1,fp)==1) /// read the file and fetch the record one record per fetch
{
printf("\n%s %d %.2f",e.name,e.age,e.bs); /// print the name, age and basic salary
}
getch();
break;
case '3': /// if user press 3 then do editing existing record
system("cls");
another = 'y';
while(another == 'y')
{
printf("Enter the employee name to modify: ");
scanf("%s", empname);
rewind(fp);
while(fread(&e,recsize,1,fp)==1) /// fetch all record from file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
{
printf("\nEnter new name,age and bs: ");
scanf("%s%d%f",e.name,&e.age,&e.bs);
fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from current position
fwrite(&e,recsize,1,fp); /// override the record
break;
}
}
printf("\nModify another record(y/n)");
fflush(stdin);
another = getche();
}
break;
case '4':
system("cls");
another = 'y';
while(another == 'y')
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ft = fopen("Temp.dat","wb"); /// create a intermediate file for temporary storage
rewind(fp); /// move record to starting of file
while(fread(&e,recsize,1,fp) == 1) /// read all records from file
{
if(strcmp(e.name,empname) != 0) /// if the entered record match
{
fwrite(&e,recsize,1,ft); /// move all records except the one that is to be deleted to temp file
}
}
fclose(fp);
fclose(ft);
remove("EMP.DAT"); /// remove the orginal file
rename("Temp.dat","EMP.DAT"); /// rename the temp file to original file name
fp = fopen("EMP.DAT", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
another = getche();
}
break;
case '5':
fclose(fp); /// close the file
exit(0); /// exit from the program
}
}
return 0;
}
推荐答案
返回0;
最后将永远不会执行,因为你退出程序时使用退出(0);
(或退出(1);
如果文件出错了... ...
Thereturn 0;
at the end will never be executed, as you are exiting the program with theexit(0);
(orexit(1);
if something with the file went wrong)...
问题是最终的返回0
行无法执行,因为没有办法退出它上面的while循环。
结果,它不会编译,因为它假定(正确地)你不打算创建一个无限循环然后之后回来。
并帮自己一个忙,并始终如一地缩进你的代码!它使阅读更容易,特别是当你不知道代码或正在寻找这样的问题时。
The problem is that the finalreturn 0
line can't be executed, because there is no way to exit the while loop above it.
As a result, it won't compile, because it assumes (rightly) that you didn't intend to create an infinite loop and then return after it.
And do yourself a favour and indent your code consistently! It make it a lot easier to read, especially when you don't know the code, or are looking for problems like this.
这篇关于以下C代码有什么问题。当我编译它时,它表示无法访问的代码。我正在使用Turbo C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文