-
March 5th, 2002, 09:51 AM
#1
Help in C++
i'm fairly new to programming in C++ and i am stuck on something i am trying to create. it is a very simple program in which you type in a word and it searches for your word in a database within the program to tell you whether you have typed a word that is in the database or it isn't in the database.
for example, a band. I want it so when you type in a band it searches through a structure and when it finds it, prints out whatever their lead singers name is. here is where i am at and i don't know why it doesn't work.
PHP Code:
#include <iostream>
#include <string>
using namespace std;
void getSingerName(string x); /*function for putting a singer to a band*/
int main()
{
struct band{ /*set up the band names structure*/
string databaseBand;
};
band names[2];
names[0].databaseBand="metallica";
names[1].databaseBand="pantera";
const char total=20;
char typedInBand[total]; /* setting up variables for a cin.get() */
cout<<"Type in the band name\n";
cin.get(typedInBand, total).get(); /*get the band name*/
for (int loopIndex=0; typedInBand[total] != names[loopIndex]; loopIndex++)
{getSingerName();}
return 0;
}
void getSingerName(string typedInBand)
{
if (typedInBand = "metallica")
{ cout<<"james\n";}
else {cout<<"phil\n";}
}
is there any way to make an array or struct with strings and search throught them?
i really need help
-
March 5th, 2002, 10:02 AM
#2
ok this will take a while but its easier this way hehe
int search()
{
for(int i=0;i!=2;i++) // not the best way to do it but im in a rush hehe
{
if(!strcmp(typedInBand,names[i].databaseBand)
{
cout<<names[i].databaseBand<<endl; they both work but i like endl better hehe
}
return 0;
}
theirs the basic thing u can work on that i hope
hope it compiles
RiOtEr
-
March 5th, 2002, 10:03 AM
#3
sorry add another } in their sorry bout that
-
March 5th, 2002, 11:40 AM
#4
In the function getSingerName, the if statement looks like it is assigning a value to the variable typedInBand, because one '=' assigns a value, two of them ('==') will evaluate it. That just stuck out to me, but there may be more... I'm not too sure...
It should be like this (you can format it differently if you want):
if (typedInBand == "metallica") {
cout<<"james\n";
}
Hope this helps...
-Tim_axe
-
March 5th, 2002, 11:51 AM
#5
looks we need a tut on whatever their called hehe
ok lets begin
= this assigns somthing so x=4 will make x have 4 in its memory
== this checks wheather its equal to so x==2 means is x equal to 2
!= means is not equal so x!=2 x is not equal to 2
<
>
<=
>=
i hope their self explanetory hehe cuase im lazy atm and i need to go do maths sorry
thanks
RiotEr
-
March 5th, 2002, 05:13 PM
#6
Member
Though what, RiOtEr says is correct, If i were you i would use while/do-while loops to go about while doing comparsion of names .
[PS]
Its quite a coincidence, im also writing a program in c++ to create a personal music database. Nothing big just storing of songname/artist/album with input checks.
-
March 5th, 2002, 05:18 PM
#7
if your eanting to make an array of strigns...you could make a 2 demensional array....
Char* MyMusicDB[100][100]
its kinda slow.... .but its what you asked for....
-
March 5th, 2002, 05:56 PM
#8
Why are you even creating a struct the way you have this? You only have one sting in it. Wouldn't it make more sence to do something like:
struct band{ //set up the band names structure
string databaseBand; //Band name
string leadsinger; //Singer name
};
band names[2];
names[0].databaseBand = "metallica";
names[0].leadsinger= "james";
//etc etc....I think you get the point here. Then once you have the name typed in, just do:
for(int i=0;i!=2;i++) // Stolen from Rioter cause I am to lazy to right while or do.while loop
{
if(!strcmp(typedInBand,names[i].databaseBand)
{
cout<<names[i].leadsinger<<endl;
}
This sets up your struct a lot more like a database, which I believe is your goal in this anyway.
\"Ignorance is bliss....
but only for your enemy\"
-- souleman
-
March 5th, 2002, 06:07 PM
#9
Why doesn't your version work? A few things..
for (int loopIndex=0; typedInBand[total] != names[loopIndex]; loopIndex++)
{getSingerName();}
You need to pass something in your function call. You also need to pass a string. It shoule be getSingerName(typedInBand[total]) or something along those lines...maybe (*typedInBand[total]) Don't remember right off hand.
Also, I don't think that == and != etc etc work with strings. I may be wrong here, but that is why the strcmp function was written. Make it something like:
for (loop=0; loop !=2; loop++)
{ strcmp(typedInBand[total], names[loop]); }
I know there is probably some error in there anyway, but oh well. C'est la vie.
\"Ignorance is bliss....
but only for your enemy\"
-- souleman
-
March 7th, 2002, 06:35 AM
#10
for (loop=0; loop !=2; loop++) u should use 2 cause what if theirs 3 soon u should use a variable like how many things are in the array etc think about that for a bit lol
but yeah = wont work with strings
u need strcpy
hehe
just thought id tell ya
RiOtEr
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
|
|