有人可以帮助我调试错误吗? [英] can anybody help me to debug the error?with this..

查看:73
本文介绍了有人可以帮助我调试错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望没有idn的冗余..如果它与d相同,那么它将提示id存在..

i just want that no REDUNDANCY of idn..if it will d same then it will prompt id exist..

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

推荐答案

否.

这样做有充分的理由:
1)我们不知道您在说什么或代码应该做什么.没有这些,我们将无法确定任何行为都是问题-我们只是不知道您想发生什么.
2)我们不知道要输入什么数据来引起问题,或者文件内容是什么.
3)您的代码到处都是缩进的!整理并保持一致,因此我们不必费力就可以得出结果.让我们轻松为您提供帮助!
4)调试是一种技能.像其他任何技能一样,如果您不练习它,那么您将永远也不会擅长.

第四可能是最重要的.

入门并不难-VS使它变得非常简单!您可以在任何行上放置一个断点,VS会一直运行直到到达该行.然后,在执行它之前,它会将控制权还给您.然后,您可以检查您的变量,单步执行程序,或者运行到相同或另一个断点.它还有很多其他功能,但是这三句话覆盖了所有调试的99%!

试试吧-这并不难-而且很值得擅长.您拥有我刚开始梦dream以求的工具:使用它们!

[edit]列表中的编号问题已修复-OriginalGriff [/edit]
No.

There are good reasons for that:
1) We don''t have a clue what you are talking about, or what the code is supposed to do. Without that, we can''t be sure that any behaviour is a problem - we just don''t know what you want to happen.
2) We don''t know what data you have to enter to cause the problem, or what your file contents are.
3) Your code is indented all over the place! Tidy it up and be consistent, so we don''t have to struggle to work out what goes with what. Make it easy for us to help you!
4) Debugging is a skill. Like any other skill, if you do not practice it, you will never get any good at it.

The fourth is probably the most important.

It''s not difficult to get started - VS makes it really easy! You can put a breakpoint on any line, and VS will run until it reaches the line. Then, before it does execute it, it returns control to you. You can then examine your variables, single step through the progam, or run on to the same or another breakpoint. It does a heck of a lot else, but those three sentences cover 99% of all debugging!

Try it - it isn''t difficult - and it''s well worth getting good at. You have tools I couldn''t even have dreamed of when I started: use them!

[edit]Numbering problem in list, fixed - OriginalGriff[/edit]


这篇关于有人可以帮助我调试错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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