-
March 7th, 2002, 07:41 AM
#11
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
-
March 7th, 2002, 08:18 AM
#12
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.
-
March 8th, 2002, 01:47 PM
#13
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
-
March 8th, 2002, 04:35 PM
#14
hehe classes..... my favorite
-
March 9th, 2002, 07:03 AM
#15
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.
-
March 9th, 2002, 11:27 AM
#16
Senior Member
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
-
Forum Rules
|
|