2012-03-24 9 views
0

Ich versuche, mein finales Debugging heute Abend zu beenden. Mein Problem ist, dass ich diesen Code für ein paar Tage geschrieben habe und es ein paar Probleme hat. Previous PostArrays und Suche nach ihnen

Es kompiliert jetzt und stürzt nicht ab, aber es gibt einige Probleme mit meinen Funktionen, die nicht richtig funktionieren (oder überhaupt).

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

} 

Neueste Version, keine wesentlichen Verbesserungen. Ich habe Probleme, die Funktionen nach der Menüauswahl zu funktionieren. ShowAll scheint zu funktionieren, gibt aber hex aus. Danke nochmal allen!

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

} 
+0

Sie möchten vielleicht expliziter mit Ihrem Komma-Operator sein. Verwenden Sie && oder || stattdessen. – collinjsimpson

+0

Bitte markieren Sie dies als Hausaufgabe, ich weiß, dass Sie vorherige Post erwähnt, aber dies tut nicht. Auch ein Hinweis: Wenn Sie die falsche Auswahl auswählen, wie kann ich wieder wählen? –

+0

danke für die Kennzeichnung dieser als HW – kd7vdb

Antwort

2

Der Komma-Operator sollte jeweils mit logischen and oder or, && und || ersetzt werden. Siehe uses of the comma operator. Außerdem ist authorSearch eine void Funktion. Wenn Sie authorSearch anrufen möchten, schreiben Sie einfach authorSearch(...) anstelle von int authorSearch(...).

Darüber hinaus müssen Sie sicherstellen, dass Ihre Prototypen konsistent mit Ihren Implementierungen sind. int authorSearch (string bookAuthor [50]) ist nicht dasselbe wie void authorSearch (string bookAuthor [50], char choice). Sie haben ihre Typen und ihre Parameter nicht übereinstimmen.

+1

Nur darauf hinweisen, dass '&&' und '||' sind die logischen 'und' und' oder'. Die bitweisen Einsen sind '&' und '|'. – chris

+0

Mein Fehler. Vielen Dank! – collinjsimpson

+0

Wenn ich den Typ-Spezifizierer vor der Authorsearch loswerde, wird es nicht kompilieren. – kd7vdb

0

1) Die showall()-Funktion gibt hex aus, da Sie Arrays auf diese Weise nicht anzeigen können, benötigen Sie eine Art von Schleife. Es wird nur die Startadresse jedes Arrays gedruckt.

2) In Ihren Suchfunktionen lesen Sie nie die target Zeichenfolge vom Benutzer.

3) Diese for() Schleifen wird nie ausführen:

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

Sie setzen count-0 und dann den Wert testen, bevor erhöht wird. Der Test schlägt fehl und der Schleifenkörper wird nicht ausgeführt.

4) Seien Sie vorsichtig, wenn Sie die for() Schleifen reparieren. Ich sehe keine Tests, um zu verhindern, dass ein ungültiger Index nach der (fest codierten) Größe Ihrer Arrays verwendet wird.

Verwandte Themen