i'm new to c++. i started with the following code and i have an, ISO C++ forbids comparison between pointer and integer ERROR. My my program is for a library. i am now trying to have it pull up my file and read then edit a bookfile. the area in red is where i started having problems. im not sure where im going wrong please help:

#include <iostream>
#include <fstream>


using namespace std;

int main()
{ 
    
    int m_menuvalue; 
    const char SIZE=20;
      
         
             cout << "1. Manage Books\n";
             cout << "2. Manage Magazines\n";
             cout << "3. Manage Audio Tapes\n";
             cout << "4. Manage CDs\n";
             cout << "5. Manage Videos\n";
             cout << "6. Check Out Items\n";
             cout << "7. Check In Items\n";
             cout << "8. Search Inventory\n";
             cout << "9. Exit the system\n\n";
             

         cout << "Please select a menu option by typing the corresponding number.\n\n";
         cin >> m_menuvalue;
         
             
             if ( m_menuvalue == 1 ){	  
                 system("cls");
     
               
               cout<<"1. Add\n";
               cout<<"2. Edit\n";
               cout<<"3. Delete\n";
               cout<<"4. Search\n";
               cout<<"5. Return to the Main Menu\n";
 
               cout<<"Select an option: ";
                  
                  int bookChoice;  
                  char bookName[SIZE], authorName[SIZE], isbnNumber[SIZE],publishYear[SIZE], bookNumber[SIZE];
                  ofstream bookFile; 

                  bookFile.open("bookfile.txt");  
                  assert (!bookFile.fail( ));     

                  
                      cin>>bookChoice; 
                      
              /***************************
              *           Add           *
              ***************************/
                                if ( bookChoice == 1 ){
                                     cin.ignore(130,'\n');
                                     system("cls");
                                     cout<<"Please enter book number.\n";
                                         cin.getline (bookNumber,SIZE);
                                     cout<<"Please enter book name?\n";
                                         cin.getline (bookName,SIZE);  
                                     cout<<"Please enter book author's name?\n";
                                         cin.getline (authorName,SIZE);
                                     cout<<"Please enter book ISBN#?\n";
                                         cin.getline (isbnNumber,SIZE);
                                     cout<<"Please enter book publish year? ex.(20xx)\n";
                                         cin.getline (publishYear,SIZE);
                                         
                                         bookFile<<bookNumber<<endl;
                                         bookFile<<bookName<<endl;   //send to file
                                         bookFile<<authorName<<endl;
                                         bookFile<<isbnNumber<<endl;  
                                         bookFile<<publishYear<<endl<<endl;
                                     
                                     cout<< "The information has been saved to our Archives\n\n";
                                      bookFile.close( );       //close file
                                      assert(!bookFile.fail( ));
                                      }
                                      
           
            
            
               
              

              /***************************
               *           Edit           *
               ***************************/
              if (bookChoice == 2){    
                             
                             ifstream readbookFile("readbookfile.txt");
              

            
               
               
                                         cin.ignore(80,'\n');
                                          readbookFile.getline(bookNumber,SIZE);
                                          readbookFile.getline(bookName,SIZE);
                                          cout<< "\n\n";
                                          
               cout<< "Please select the book that you would like to edit.\n";
               cin>>bookNumber;
                               
                            if (bookNumber == '1'){
                                       cin.ignore(80,'\n');
                                       system("cls");
                                                                   
                                       cout<<bookName<<endl<<authorName<<endl
                                       <<isbnNumber<<endl<<publishYear<<endl<<endl;
                                       readbookFile.close();
               
               
               
              ofstream bookFile; 
                  bookFile.open("bookFile.txt"); 
             
                                          
                                          
               
               
                 cout<<"Hit (ENTER), then enter the following book information.\n\n";
                       
                         cin.ignore(80,'\n');
                        
               cout<<"Book name:\n";
                                         cin.getline (bookName,SIZE);
               cout<<"Author's name:\n";
                                         cin.getline (authorName,SIZE);
               cout<<"Book's ISBN#:\n";
                                         cin.getline (isbnNumber,SIZE);
               cout<<"Publish year: ex.(20xx)\n";
                                         cin.getline (publishYear,SIZE);
               
                                         
                                         
             
             
                    bookFile<<bookName<<endl;   
                    bookFile<<authorName<<endl;
                    bookFile<<isbnNumber<<endl;  
                    bookFile<<publishYear<<endl<<endl; 
       cout<< "The information has been saved to our Archives\n\n";
                                          bookFile.close( );
               
               }
               }

bookNumber is an array of char, you're trying to compare it to one single char.

You can either make both of them std::string or put '1' into a char array and use strcmp.

Edited 6 Years Ago by jonsca: n/a

Actually you can put it in as a literal in other words: if (strcmp(bookNumber,"1")==0) (strcmp returns 0 if the strings are a match)

Appreciate that. It worked. If you can please help me with one more problem its giving me.

Its suppose to read before i select the booknumber(a few lines before the red area). But it skips over the read and asks me to select with out the read. It also will not let me pull the book number 1 from the file AFTER I ENTER IT. If you can help me with that, i would praise you

Edited 6 Years Ago by dls_20022002: n/a

I'm not sure about the skipping. I mean I know what's causing it, the mixing of the cins and getlines but you have ignores in place. Try popping in another one right before your "Add" comment block.

I'm not sure what would be causing the failure to read the file back in (are you speaking of the Edit step?). It looks like things get opened and closed properly. Have you looked (in a text editor) at what the file contains? If it outputs correctly you could see if giving it an (artificial) input file works any better.

I'm not sure about the skipping. I mean I know what's causing it, the mixing of the cins and getlines but you have ignores in place. Try popping in another one right before your "Add" comment block.

I'm not sure what would be causing the failure to read the file back in (are you speaking of the Edit step?). It looks like things get opened and closed properly. Have you looked (in a text editor) at what the file contains? If it outputs correctly you could see if giving it an (artificial) input file works any better.

that didnt work. i still cant see the read statement. im lost at the moment. i appreciate the help anyway. if you or anyone else can figure it out, let me know

This article has been dead for over six months. Start a new discussion instead.