如果用户输入错误的值,我需要一种方法返回主菜单 [英] I need a way to go back to the main menu if the user enters the wrong values

查看:59
本文介绍了如果用户输入错误的值,我需要一种方法返回主菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个航班预订计划。



该程序应该与用户输入的内容匹配存储在文件中的内容。它工作正常,但只有正确的数据,如果用户输入错误的值示例,如果用户输入错误的国家代码或航班日期,我需要一种方法返回主菜单。数据在底部的comparefirst函数中匹配。



  #include   <   stdio.h  >  
#include < windows.h >
#include < stdlib.h > / *使用malloc函数* /
#include < string.h >

/ * 使用符号常量* /
#define BOOK_FLIGHT'1'
#define CHECK_FLIGHT'2'
#define QUIT'3'

#define MAXSTRINGSIZE 100
#define MAXARRAYSIZE 100

typedef char String [MAXSTRINGSIZE];

char Menu1()
{
printf( CUB-CUBA \ n);
printf( BAR-BARBADOS \ n);
printf( GRE-GRENADA \ n);
printf( STV-ST.VINCENT \ n);
printf( STL-ST.LUCIA \ n);
printf( STK-ST.KITTS AND NEVIS \ n);
printf( ARU-ARUBA \ n);
printf( PUE-PUERTO RICO \ n);
printf( HAI-HAITI \ n);
printf( BAH-BAHAMAS \ n);
printf( JAM-JAMAICA \ n);
printf( DOM-DOMINICA \ n);
}


char mainMenu( void ) ;
typedef struct
{
String flight_num;
字符串目的地;
String dep_date; String dep_time;
int EC_seats; float EC_price;
int FC_seats; float FC_price;

}航班;

int loadFlights(Flights * FDA []);

void printFlights(Flights * newFlights);

int 预订(航班* FDA []);

int comparefirst(Flights * FDA [], String destination, String dep_date, int FC_seats);

int main()
{

} // 主要结束

int 预订(航班* FDA [])
{
String 目的地;
String dep_date;
String seatclass;
String flight_num;
int EC_seats;
int FC_seats;
int Num_persons;
float 价格;
字符串名称;
String ID_num;

char first [ 10 ] = FC;
char econ [ 10 ] = EC;
char eandf [ 10 ] = FC& EC;

printf( 通过在\ n下面输入国家代码选择目的地);
printf( \ n);
Menu1();
scanf( %s,目的地);
printf( 选择航班日期dd / mm / yy format \ n);
scanf( %s,dep_date);
printf( 输入头等舱FC,经济舱EC或FC& EC选择旅行舱both\\\
);
scanf( %s,seatclass);
if (strcmp(seatclass,first)== 0
{
printf( \ n许多人乘坐头等舱);
scanf( %d,& FC_seats);
comparefirst(FDA,destination,dep_date,FC_seats);
}
}

int loadFlights(Flights * FDA [])
{
int EC_seats,FC_seats;
float EC_price,FC_price;
字符串 flight_num,destination,dep_date,dep_time;

// 打开一个只读的文件
文件* FP;
fp = fopen( Flightinfo.txt.txt r);

// 测试文件是否存在
if (fp == NULL)
{
printf( \ nFile不存在或已损坏,请重新输入文件名);
返回 - 1 ;
}

int count = 0 ;
// 使用循环读取文件中的所有记录
< span class =code-keyword> while
(!feof(fp))
{
// 宣布存储航班的新结构
航班* newFlights;

fscanf(fp, %s,flight_num); fgetc( fp); // get eoln char
fscanf(fp, %s,destination); fgetc(fp); // get eoln char
fscanf(fp, %s%s,& dep_date,& dep_time); fgetc(fp);
fscanf(fp, %d%f,& EC_seats,& EC_price );龟etc(FP);
fscanf(fp, %d%f,& FC_seats,& FC_price );龟etc(FP);

newFlights =(Flights *)malloc( sizeof (Flights));

strcpy(newFlights-> flight_num,fixString(flight_num));
strcpy(newFlights-> destination,fixString(destination));
strcpy(newFlights-> dep_date,fixString(dep_date));
strcpy(newFlights-> dep_time,fixString(dep_time));
newFlights-> EC_seats = EC_seats;
newFlights-> EC_price = EC_price;
newFlights-> FC_seats = FC_seats;
newFlights-> FC_price = FC_price;

// 数组中没有位置
FDA [count ] = newFlights;

// 打印刚刚添加到数组中的航班
printFlights(FDA [count]);
// 更新计数
count ++;
} // 结束时

return count;
} // 加载结束

char * fixString( String 行)
{
for int i = 0 ; i< maxstringsize;> {
if (line [i] == ' \ n '
{
line [i] = ' \0';
break ;
}
}

返回行;
}

无效 printFlights(Flights * newFlights)
{
printf( ////////////飞行//////// ///// \ n);
printf( %s \ n,newFlights-> flight_num);
printf( %s \ n,newFlights-> destination);
printf( %s%s \ n,newFlights-> dep_date, newFlights-> dep_time);
printf( %d%.02f \ n,newFlights-> EC_seats ,newFlights-> EC_price);
printf( %d%.02f \ n,newFlights-> FC_seats ,newFlights-> FC_price);
printf( \ n\ n);
}

int comparefirst(Flights * FDA [], String destination, String dep_date, int FC_seats)
{
float 价格;
int j = 0 ;
for int i = 0 ; I< maxarraysize;我++)>
{
// test for null

if (FDA [i] == NULL)
{
i ++; // 跳过此航班记录
}
其他 if (strcmp(FDA [i] - > dep_date,dep_date)== 0 &&(strcmp(FDA [i ] - >目的地,目的地)== 0 )&&(FC_seats< = FDA [i] - > FC_seats))
{
printFlights(FDA [i]);
价格=(FC_seats * FDA [i] - > FC_price);
printf( 总飞行成本为$%。02f\ n,Price );
getchar();
j ++;
if (j> = 10
{
< span class =code-keyword> break
; // 数组已满时停止循环
}
}
else if (strcmp(FDA [i] - > dep_date,dep_date)== 0 &&(strcmp(FDA [i] - >目的地,目的地)== 0 )&& ;(FC_seats> FDA [i] - > FC_seats))
{
printf( 没有足够的头等舱座位);
getchar();
getchar();
j ++;
if (j> = 10
{
< span class =code-keyword> break
; // 数组已满时停止循环
}
}
}
return j; // 返回找到的航班的数量。
}

解决方案

%。02f\ n,Price);
getchar();
j ++;
< span class =code-keyword> if (j> = 10
{
break ; // 当数组已满时停止循环
}
}
else if (strcmp(FDA [i] - > dep_date,dep_date)== 0 &&(strcmp(FDA [i] - > destination,destination)== 0 )&&(FC_seats> FDA [i] - > FC_seats))
{
printf( 没有足够的头等舱席位) ;
getchar();
getchar();
j ++;
if (j> = 10
{
< span class =code-keyword> break ; // 数组已满时停止循环
}
}
}
return j; // 返回找到的航班的数量。
}


这很容易实现一个简单的有限状态机,请参阅此Wikipedia页面:Finite -state machine [ ^ ]。

This is a flight booking program.

The program is supposed to match what the user enters to what is stored in a file. It works fine but only for correct data,I need a way to go back to the main menu if the user enters the wrong values example if the user enters the wrong country code or flight date. Data is matched in the comparefirst function at the bottom.

#include <stdio.h>
#include <windows.h>
#include <stdlib.h> /*use of malloc function*/
#include <string.h>

/*Using symbolic constants*/
#define BOOK_FLIGHT '1'
#define CHECK_FLIGHT '2'
#define QUIT '3'

#define MAXSTRINGSIZE 100
#define MAXARRAYSIZE 100

typedef char String[MAXSTRINGSIZE];

char Menu1()
{
printf("CUB-CUBA\n");
printf("BAR-BARBADOS\n");
printf("GRE-GRENADA\n");
printf("STV-ST.VINCENT\n");
printf("STL-ST.LUCIA\n");
printf("STK-ST. KITTS AND NEVIS\n");
printf("ARU-ARUBA\n");
printf("PUE-PUERTO RICO\n");
printf("HAI-HAITI\n");
printf("BAH-BAHAMAS\n");
printf("JAM-JAMAICA\n");
printf("DOM-DOMINICA\n");
}


char mainMenu(void);
typedef struct 
{
   String flight_num;
   String destination;
   String dep_date; String dep_time;
   int EC_seats; float EC_price;
   int FC_seats; float FC_price;

}Flights;

int loadFlights(Flights* FDA[]);

void printFlights(Flights *newFlights);

int booking(Flights* FDA[]);

int comparefirst(Flights *FDA[], String destination, String dep_date, int FC_seats);

int main()
{

}//end of main

int booking(Flights *FDA[])
{
String destination;
String dep_date;
String seatclass;
String flight_num;
int EC_seats;
int FC_seats;
int Num_persons;
float Price;
String Name;
String ID_num;

char first[10] = "FC";
char econ[10] = "EC";
char eandf[10]= "FC&EC";

printf("Chose a destination by entering a country code below\n");
printf("\n");
Menu1(); 
scanf("%s",destination);
printf("Choose flight date dd/mm/yy format\n");
scanf("%s",dep_date);
printf("Choose travel class by entering FC for first class,EC for economy class or FC&EC for both\n");
scanf("%s",seatclass);
if (strcmp(seatclass,first)==0) 
{
    printf("\nHow many persons are traveling in first class");
    scanf("%d", &FC_seats);
    comparefirst(FDA,destination,dep_date,FC_seats);                                                                                                                                       
}                                                 
}

int loadFlights(Flights* FDA[])
{
int EC_seats,FC_seats;
float EC_price,FC_price;
String flight_num,destination,dep_date,dep_time;

//open a file for reading only
FILE *fp;
fp = fopen("Flightinfo.txt.txt","r");

//test to see if file exist
if(fp==NULL) 
{
    printf("\nFile does not exist or is corrupted please re-enter file name"); 
    return -1;
}

int count=0;
//using a loop to read all the records in the file 
while(!feof( fp))
{
   //declear a new stucture to store flights
   Flights* newFlights;

   fscanf( fp,"%s",flight_num);fgetc(fp);//get eoln char
   fscanf( fp,"%s",destination);fgetc(fp);//get eoln char
   fscanf( fp,"%s %s",&dep_date,&dep_time);fgetc(fp);
   fscanf( fp,"%d %f",&EC_seats,&EC_price);fgetc(fp);
   fscanf( fp,"%d %f",&FC_seats,&FC_price);fgetc(fp);

   newFlights = (Flights*)malloc(sizeof(Flights));

   strcpy(newFlights-> flight_num,fixString(flight_num)); 
   strcpy(newFlights-> destination,fixString(destination));
   strcpy(newFlights-> dep_date,fixString(dep_date));
   strcpy(newFlights-> dep_time,fixString(dep_time));
   newFlights-> EC_seats = EC_seats;
   newFlights-> EC_price = EC_price;
   newFlights-> FC_seats = FC_seats;
   newFlights-> FC_price = FC_price;

   //no place in the array 
   FDA[count]=newFlights;

   //printing the flight that was just added to the array
   printFlights(FDA[count]);
   //update count
   count++;
}//end while

return count;
}//end of load 

char *fixString(String line)
{
 for (int i=0; i<maxstringsize;> {
     if (line[i]=='\n')
     {
         line[i]='\0';
         break;
     }
 }

 return line;
}

void printFlights(Flights *newFlights)
{
   printf("////////////FLIGHT/////////////\n");
   printf( "%s\n",newFlights->flight_num);
   printf( "%s\n",newFlights->destination);
   printf( "%s %s\n",newFlights->dep_date,newFlights->dep_time);
   printf( "%d %.02f\n",newFlights->EC_seats,newFlights->EC_price);
   printf( "%d %.02f\n",newFlights->FC_seats,newFlights->FC_price);
   printf("\n\n");
}

int comparefirst(Flights *FDA[], String destination, String dep_date, int  FC_seats)
{
  float Price;
  int j=0; 
    for (int i=0;i<maxarraysize;i++)>
    {
        //test for null

        if (FDA[i]==NULL) 
        { 
            i++;//skip over this flight record         
        }
        else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FC_seats<=FDA[i]->FC_seats)) 
        {
            printFlights(FDA[i]);
            Price=(FC_seats*FDA[i]->FC_price);
            printf("Total cost of flight is $%.02f\n",Price);   
            getchar();
            j++;
            if(j >= 10)
            {
               break; // Stop Looping as Array is full
            }
        }
        else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FC_seats>FDA[i]->FC_seats))
        {
            printf("Not enough first class seats");
            getchar();
            getchar();
            j++;
            if(j >= 10)
            {
               break; // Stop Looping as Array is full
            }
        }
    }  
   return j; // Return Count of the Flights found.
}

解决方案

%.02f\n",Price); getchar(); j++; if(j >= 10) { break; // Stop Looping as Array is full } } else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FC_seats>FDA[i]->FC_seats)) { printf("Not enough first class seats"); getchar(); getchar(); j++; if(j >= 10) { break; // Stop Looping as Array is full } } } return j; // Return Count of the Flights found. }


This is easily accomplished implementing a simple Finite State Machine, see this Wikipedia page: "Finite-state machine"[^].


这篇关于如果用户输入错误的值,我需要一种方法返回主菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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