kd7vdb
kd7vdb

Reputation: 87

Arrays and Searching Them

I am trying to finish my final debugging tonight. My problem is that I have been writing this code for a couple days and it has a few problems. Previous Post

It now compiles and does not crash, but there are a few issues with my functions not working properly (or at all).

#include <iostream>              
#include <string>
#include <fstream>
using namespace std; 

string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);         
int showall (int counter);
int authorSearch (string bookAuthor [50]);




int main ()

{  
    string pathname;
    int counter=0;
    char choice;

    cout<<"Input the name of the file to be accessed: ";
    cin>>pathname;
    loadData (pathname);
    showall (counter);

    cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):";
    cin>>choice;

    while (choice != 'Q' , choice != 'q')
    {
          if (choice == 'A', choice == 'a')
          {
                    int authorSearch (string bookAuthor [50], char choice);
          }

          if (choice == 'T', choice == 't')
          {
                     int   titleSearch (string bookTitle [50], char choice);   
          }   


    }

    cout<<"Press <Enter> to Exit";
    cin.ignore();
    cin.get();      
    return 0;              

    cout<<"Press <Enter> to Exit";
    cin.ignore();
    cin.get();      
    return 0;                
}


int loadData (string pathname) // Loads data from infile into arrays
{
    fstream infile; 
    int counter = 0;
    infile.open(pathname.c_str()); //Opens file from user input in main
    if( infile.fail() )
     {
         cout << "File failed to open";
         return 0;
     }   

     while (!infile.eof())
     {

           infile >> bookTitle [counter] ;  //takes input and puts into parallel arrays
           infile >> bookAuthor [counter];
           counter++;
     }

     infile.close();
}

int showall (int counter)        // shows input in title(author) format
{

     cout<<bookTitle<<"("<<bookAuthor<<")";   

}

void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array
{
     string target = "";
     cout<<"Which author would you like to search for: "<<target; //input
     for (int count = 0; count++;)
     {
         if(bookAuthor[count] == target) //tests input against array and outputs result
         {
                              cout<<bookTitle[count]<<bookAuthor[count];
         }
     }

}



void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array
{
     string target = "";
     cout<<"Which author would you like to search for: "<<target; //input
     for (int count = 0; count++;)
     {
         if(bookAuthor[count] == target) //tests input against array and outputs result
         {
                              cout<<bookTitle[count]<<bookAuthor[count];
         }
     }

}

Latest Version , no major improvements. I am having trouble getting the functions to work after the menu selection. ShowAll seems to work but outputs hex. Thanks again everyone!

#include <iostream>              
#include <string>
#include <fstream>
using namespace std; 

string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);         
int showall (int counter);
void authorSearch (string bookAuthor [50]);
void titleSearch (string bookTitle [50]);



int main ()

{  
    string pathname;
    int counter=0;
    char choice;

    cout<<"Input the name of the file to be accessed: ";
    cin>>pathname;
    loadData (pathname);
    showall (counter);

    cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):";
    cin>>choice;

    while (choice != 'Q'|| choice != 'q')
    {
          if (choice == 'A'|| choice == 'a')
          {
                   void authorSearch (string bookAuthor [50], char choice);
          }

          if (choice == 'T'|| choice == 't')
          {
                    void titleSearch (string bookTitle [50], char choice);   
          }   


    }

    cout<<"Press <Enter> to Exit";
    cin.ignore();
    cin.get();      
    return 0;              

}


int loadData (string pathname) // Loads data from infile into arrays
{
    fstream infile; 
    int counter = 0;
    infile.open(pathname.c_str()); //Opens file from user input in main
    if( infile.fail() )
     {
         cout << "File failed to open";
         return 0;
     }   

     while (!infile.eof())
     {

           infile >> bookTitle [counter] ;  //takes input and puts into parallel arrays
           infile >> bookAuthor [counter];
           counter++;
     }

     infile.close();
}

int showall (int counter)        // shows input in title(author) format
{

     cout<<bookTitle<<"("<<bookAuthor<<")";   

}

void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array
{
     string target = "";
     cout<<"Which author would you like to search for: "<<target; //input
     for (int count = 0; count++;)
     {
         if(bookAuthor[count] == target)
         {
                              cout<<bookTitle[count]<<bookAuthor[count];
         }
     }

}



void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array
{
     string target = "";
     cout<<"Which title would you like to search for: "<<target; //input
     for (int count = 0; count++;)
     {
         if(bookAuthor[count] == target) //tests input against array and outputs reults
         {
                              cout<<bookTitle[count]<<bookAuthor[count];
         }
     }

}

Upvotes: 0

Views: 319

Answers (2)

Blastfurnace
Blastfurnace

Reputation: 18652

1) The showall() function outputs hex because you can't display arrays that way, you need some kind of loop. It's just printing the starting address of each array.

2) In your search functions you never read the target string from the user.

3) These for() loops will never execute:

for (int count = 0; count++;)
{
    ...
}

You set count to 0 and then test the value before incrementing. The test fails and the loop body isn't executed.

4) Take care when you fix the for() loops. I don't see any tests to prevent using an invalid index past the (hardcoded) size of your arrays.

Upvotes: 0

xikkub
xikkub

Reputation: 1660

The comma operator should be replaced with logical and or or, && and || respectively. See uses of the comma operator. Also, authorSearch is a void function. If you want to call authorSearch, simple write authorSearch(...) instead of int authorSearch(...).

Additionally, you need to make sure your prototypes are consistent with your implementations. int authorSearch (string bookAuthor [50]) is not the same as void authorSearch (string bookAuthor [50], char choice). You've mismatched their types and their parameters.

Upvotes: 2

Related Questions