0

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];
         }
     }

}
Community
  • 1
  • 1
kd7vdb
  • 87
  • 1
  • 1
  • 8
  • You might want to be more explicit with your comma operator. Use && or || instead. – xikkub Mar 24 '12 at 03:12
  • Please mark this as homework, I know you're previous post mentioned it, but this one does not. Also, a hint: if you select the wrong choice how can I choose again? – David D Mar 24 '12 at 03:13
  • thanks for marking this as HW – kd7vdb Mar 24 '12 at 03:24
  • What would be the best way to get the function to return to the while statement after the function completes. BTW I cant get the searches to work properly – kd7vdb Mar 24 '12 at 03:28
  • The `while-loop` in your search function will never end if your data file is formatted incorrectly. Check that and see my answer below. – xikkub Mar 24 '12 at 03:56
  • changed it to 'if', I really would like to use a goto type function to make it return to the part where the user inputs what they want to do. But alas goto is bad c++ form. – kd7vdb Mar 24 '12 at 04:05
  • I made a mistake in my last comment. You need to *fix* your prototype parameters at the top of your source file. They're inconsistent. – xikkub Mar 24 '12 at 04:09

2 Answers2

2

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.

Community
  • 1
  • 1
xikkub
  • 1,641
  • 1
  • 16
  • 28
  • 1
    Just pointing out that `&&` and `||` are the logical `and` and `or`. The bitwise ones are `&` and `|`. – chris Mar 24 '12 at 03:52
  • If I get rid of the type specifier in front of authorsearch it will not compile. – kd7vdb Mar 24 '12 at 04:01
  • You need to fix your prototype definitions at the top of your source file. They're inconsistent with the implementations you provided at the bottom of your source file. – xikkub Mar 24 '12 at 04:08
  • they are now currently fixed, thank you. Does anyone have any Idea why im getting some sort of hex code back for the showall function? – kd7vdb Mar 24 '12 at 04:16
  • `bookTitle` and `bookAuthor` are string arrays, not strings. You need to index them with `bookTitle[index]`. Printing `bookTitle` prints the memory location of the beginning array. – xikkub Mar 24 '12 at 04:21
0

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.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70