Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i just want that no REDUNDANCY of idn..if it will d same then it will prompt id exist..

C++
#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();
}
Posted
Updated 14-Jan-12 3:15am
v2
Comments
Richard MacCutchan 14-Jan-12 9:30am    
i just want that no REDUNDANCY of idn..if it will d same then it will prompt id exist..
What?
reikuxian 14-Jan-12 10:37am    
it can still write eventhough the idnumber is exist..what i want is that no redundancy of idnumber is restored in a textfile.
Satheesh1546 14-Jan-12 10:46am    
Please make it clear, what you need..
reikuxian 16-Jan-12 12:05pm    
what i want is when an ID: 1 is already entered and save in an text file the ID number should not be allowed to enter again..but im having difficult on it..

1 solution

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]
 
Share this answer
 
v2
Comments
Richard MacCutchan 15-Jan-12 7:26am    
Numbering problem in list, fixed
Or you could use <ol> tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900