Help in C++ - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Help in C++

  1. #11
    Old-Fogey:Addicts founder Terr's Avatar
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    2,007
    A large percentage of the bugs (and ones hard to trace too!) inside programs I've worked on have involved accidentally using a single equals sign within a If statement. I think it ranks up there somewhere high on the bugscale.
    [HvC]Terr: L33T Technical Proficiency

  2. #12
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    You probably want to do something like this:

    Code:
    // file: DataBase.cpp
    #include <vector>
    #include <string>
    using namespace std;
    
    #include "Band.cpp"
    
    int main() {
    
        vector<Band> bands;
        bands.push_back(Band("Metallica", "James Hetfield"));
        bands.push_back(Band("Pantera", "Phil Anselmo"));
    
        string input;
        cout << "Please enter the name of the band: " << endl;
        cin >> input;
    
        vector<Band>::iterator iter;
        iter = bands.begin();
        while(iter!=bands.end()) {
            if (iter->name()==input) {
                cout << "Found it!" << endl;
                break;
            }
            iter++;
        }
    
        return 0;
    }
    
    
    
    // file: Band.cpp
    #include <iostream>
    using namespace std;
    
    class Band {
    
    public:
        Band() {};
        Band(string, string);
        ~Band() {};
        void name(string);
        string name();
        void singer(string);
        string singer();
    protected:
        string _name;
        string _singer;
    };
    
    Band::Band(string n, string s) {
        _name = n;
        _singer = s;
    }
    
    void Band::name(string s) {
        _name = s;
    }
    
    string Band::name() {
        return _name;
    }
    
    void Band::singer(string s) {
        _singer = s;
    }
    
    string Band::singer() {
        return _singer;
    }
    Put the first bit into a file called DataBase.cpp and the section bit into a file called Band.cpp. I'd be happy to explain any of this. Hope this helps .
    OpenBSD - The proactively secure operating system.

  3. #13
    Banned
    Join Date
    Sep 2001
    Posts
    853
    lol smirc if he cant get a simple search function working i think hes going to have problems with classes atm hehe sorry just had to add that but yeah lol
    RiOtEr

  4. #14
    The Lizard King SarinMage's Avatar
    Join Date
    Jan 2002
    Location
    California
    Posts
    561
    hehe classes..... my favorite

  5. #15
    Senior Member
    Join Date
    Aug 2001
    Posts
    356
    just a few quick notes...
    use "band.h" not "band.cpp"
    cpp should be used for source code, h should be used for header files. that is most definatly a header file

    you dont include var names in the member-function prototypes, just types, and you arent consistant with your var name usage.

    you didnt protect against accidental multiple inclusion.

    i've never seen "protected" used, always "private".

    you call 2 member functions that dont exist( begin() and end() )

    you dont define iter

    all in all, i'd say this is not even microsoft quality code.

    now, to the original code/question:
    Code:
    #include <iostream.h>
    #include <string.h>
    using namespace std;
    
    #define TOTAL 20
    #define NUM_OF_BANDS 2
    
      struct band{                    /*set up the band names structure*/
           string databaseBand;
           string leadSinger;
           };
    
    
    int main()
    {
           band bands[2];
           char typedInBand[TOTAL];            /* setting up variables for a cin.get() */
           int i;
           bool found = false;
    
           strcpy(bands[0].databaseBand, "metallica");
           strcpy(bands[0].leadSinger, "james");
           strcpy(bands[1].databaseBand, "pantera");
           strcpy(bands[1].leadSinger, "phil");
    
           cout<<"Type in the band name: ";
           cin.getline(typedInBand, total);   /*get the band name*/
    
           for (i=0; i<NUM_OF_BANDS && !found; i++)
           {
                 if( !strcmp(bands[i].databaseBand, typedInBand) ) //strcmp returns 0 if the strings match   
                {
                     cout << "Lead singer is: " << bands[i].leadSinger << endl;
                     found = true;
                }
           }
           
           return 0;
    }
    a few comments on the original code you posted. it isnt good programming practice to define vars inside a for loop and your better off putting your definitions like that "const char"(which should be a const int) in the global section so you can use that in any loops which may come up in other functions. it would be easier to input all the band names from a file so you dont have to hard-code em all, but i bet you havent gotten that far yet, hehe.

    anyway, i think you'll find this code to be shorter and easier to understand than what you have(which doesnt work for a few reasons stated above) and all in all better.
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

  6. #16
    Senior Member
    Join Date
    Oct 2001
    Posts
    114
    void getSingerName(string typedInBand)
    {
    if (typedInBand = "metallica") ***
    { cout<<"james\n";}
    else {cout<<"phil\n";}
    }

    ***try using strcmp or atleast ==
    getSingerName(String blah blah) is called with void ??
    Better Laugh At Your Own Problems..
    Coz...The World Laughs At Them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides