错误:案例1不应该重复 [英] Error: case 1 should not be repeated

查看:75
本文介绍了错误:案例1不应该重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在情况1中不应重复使用ID号:.[例如,当输入ID:1并不再允许保存在文本文件1中时...]
请帮我解决错误...

The ID no: in case1 should be not repeated..[e.g when ID:1 is enter and save in text file 1 is not allowed anymore to enter...]
please help me with my error to fix...

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

using namespace std;
main(){
       
       FILE *ptr;
       FILE *tptr;
       char wb[100];
       int idn, sidn,idFromFile;
       int ch, xidn;
       char menu;
       bool exist;
       
       system("cls");
        
       cout&lt;&lt;"Menu\n\n";
       cout&lt;&lt;"1. add\n";
       cout&lt;&lt;"2. view\n";
       cout&lt;&lt;"3. del\n";	   
	   cout&lt;&lt;"Choice: "; 
       cin&gt;&gt;ch;
	   
	   switch (ch){
              case 1:
                   {
                   ptr = fopen("add.txt", "a+");
                   if(ptr == NULL)
                          {
                          cout&lt;&lt;"\n\nFILE COULD NOT BE OPENED!";
                          }
                   else{
                        
                       /* the error start here*/
                        ifstream ViewLogin(ptr);
                        
                        cout&lt;&lt;"\nID: ";
                        cin&gt;&gt;idn; 
                        //cin.ignore(); //xidn = ptr.idn;
                        getline(ViewLogin, idnFromFile);
                        while(idn == idnFromFile)
                        {
                                  cout&lt;&lt;"\nID EXIST!";
                                  cout&lt;&lt;"\nID: ";
                                  cin&gt;&gt;idn;           
                        }
                        //if(
                        cout&lt;&lt;"\twebsite: "; cin&gt;&gt;wb;  
                        fprintf(ptr,"%d\n%s\n", idn, wb);
                        fclose(ptr);                        
                        }
                                 getch();
                                 goto repeat;
                    }                        
              case 2:
                   {
                   ptr = fopen("add.txt", "r");
                   if(ptr == NULL){
                          cout&lt;&lt;"\n\nFILE COULD NOT BE OPENED!";
                          }
                   else{
                        char xwb[100];
                        int xidn;
                        cout&lt;&lt;"Search ID: "; cin&gt;&gt;sidn;
                        
                        while(!feof(ptr)){
                                          fscanf(ptr,"%d%s\n", &idn, wb);
                                        if(sidn == idn){
                                                 xidn = idn;
                                                 strcpy(xwb, wb); 
                                                
                                                 }
                                        else{
                                             cout&lt;&lt;"ID NOT EXIST!";
                                             }
                        } 
                        cout&lt;&lt;"\nInfo\n";
                        cout&lt;&lt;"ID:"&lt;&lt;"\t"&lt;&lt;xidn&lt;&lt;endl;
                        cout&lt;&lt;"Website: "&lt;&lt;"\t\t"&lt;&lt;xwb&lt;&lt;endl;
                                
                        fclose(ptr);          
                      
                        }                                                
                   getch();
                   goto repeat;
                   }    
                        
             case 3:
                        {
                        ptr = fopen("add.txt", "r");
                        int del;
                        if(ptr == NULL){
                               cout&lt;&lt;"\n\nFILE COULD NOT BE OPENED!";
                               }
                        else{
                             
                        while(!feof(ptr))
                        {
                             fscanf(ptr,"%d%s\n", &idn, wb);
                                                              
                        cout&lt;&lt;"Info\n";
                        cout&lt;&lt;"ID:"&lt;&lt;"\t\t\t"&lt;&lt;idn&lt;&lt;endl;
                        cout&lt;&lt;"website: "&lt;&lt;"\t\t"&lt;&lt;wb&lt;&lt;endl;
                       
                          }
                             cout&lt;&lt;" delete all records? 1 - YES, 2 - NO\n"; 
                             cin&gt;&gt;del;
                             if(del == 1){
                                    ptr = fopen("add.txt", "w");
                                    if(ptr == NULL){
                                           cout&lt;&lt;"\n\nFILE COULD NOT BE OPENED!";
                                           }
                                    else{
                                         fprintf(ptr,"");
                                         }
                                    }
                             else if(del == 2){
                                               goto repeat;
                                               }
                             else{
                                  cout&lt;&lt;"Wrong Input!";
                                  goto repeat;
                                  }
                                  fclose(ptr);
                                  }
                             getch();
                             goto repeat;
                             }                  
                   //case 4:
                    default:
                            {
                            cout&lt;&lt;"Wrong choice!";
                            getch();
                            
                            goto repeat;
                            }}
                            
                    repeat:
                    cout&lt;&lt;"\nReturn to Main Menu? [Y/N]";
                    cin&gt;&gt;menu;
                    if((menu == 'Y')||(menu == 'y')){
                             return main();
                             }
                    if((menu == 'N')||(menu == 'n')){
                             return 0;
                             }
getch();
}



[edit]添加了代码块,对HTML字符进行了编码-OriginalGriff [/edit]
[edit]清理主题和问题正文-johny10151981 [/edit]



[edit]Code block added, HTML characters encoded - OriginalGriff[/edit]
[edit]Clean Subject and Question body - johny10151981[/edit]

推荐答案

愚蠢的人很少(对不起,我用了这个词,但它是它的意思)在您的程序中.
1.您使用了 goto 语句.它的语法正确,对学习无害.但我读到一些专家建议,请务必避免使用< goto> .无论算法多么复杂,您都必须有一种更好的方法来解决 goto 语句
2.大爆炸: 您递归主要对象 .绝对不要那样做.

当您说主菜单时,它并不意味着主功能.您知道如何使用while循环.因此请弄清楚如何迭代整个过程.

再次阅读 switch-case 语句.阅读有关 break 语句在 switch-case情况下如何使用的信息,如果您仍然有问题再次卷土重来
There is few stupidity(I am sorry I used that word but it is what it is) in your program.
1. You used goto statement. Its correct syntax, nothing harm to learn. But I read some experts suggestion to avoid <goto> by all means. No matter how complex the algorithm is you must have a better way to solve without goto statement
2. The big bang: You recursive your main . Never do that.

When you say main menu it does not mean main function. You know how to use while loop. so figure it out how you can iterate the entire process.

read switch-case statement again. read about how break statement is used in switch-case if you still have problem comeback again


这篇关于错误:案例1不应该重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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