Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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...
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&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]Code block added, HTML characters encoded - OriginalGriff[/edit]
[edit]Clean Subject and Question body - johny10151981[/edit]
Posted
Updated 16-Jan-12 14:08pm
v3
Comments
Sergey Alexandrovich Kryukov 16-Jan-12 12:42pm    
The question is not answerable because this is not a question, just a code dump. No question, no explanation...
--SA
Albert Holguin 17-Jan-12 12:44pm    
Your question is not clear...

1 solution

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
 
Share this answer
 
Comments
Albert Holguin 17-Jan-12 12:44pm    
+5, good advice
JackDingler 17-Jan-12 17:32pm    
And 'ch' needs to updated to a new value to get it to trigger a different case statement.

Beelzebub is responsible for getting the ANSI committee approval of the goto statement. No other explanation rings true....
JackDingler 17-Jan-12 17:33pm    
Maybe Loki... :)
Mohibur Rashid 17-Jan-12 23:51pm    
haha, I never knew that. But in assembly its a big deal, hard to avoid :)
Emilio Garavaglia 18-Jan-12 3:06am    
The reason why goto is there is that the only way to code a finite state machine without introducing fuzzy logic to bypass it.

There are no devils, loki or whatever religious reasons. Just well precise technical reasons.

Goto is the only readable way to escape more than one block (have you ever need to escape from two nested for loops? For one you have "break", for two you can either introducing a meaningless variable to force the exit condition or use a goto. There are entire programming school that consider the first practice more harmuful than the second.

Please stop blaming goto without reasons.
In these context goto-s are fine. The problem is a misuse of break statement in the switch.

If you are interesten in a more neutral analysys of what goto-s are for, read this: http://david.tribble.com/text/goto.html

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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