-
array search
hi. i was looking through the "help" in C++ and found a search and attempted to mod it for searching through arrays. Unfortunately, it was unsuccessful.
say i have the an array of name[5] of:
1. KARL,GEORGE
2. SMITH,JOE
3. DEEN,LOW
4. SUPER,MAN
5. SPIDER,MAN
how would i be able to search through the array for the first letter.
User inputs:
if inputted:
S
then will display SUPER,MAN and SPIDER,MAN
Note: need examples which are compatible with <cstring.h>
-
While the cin was nice, where is the rest of your code?
I'll give you a hint:
Code:
loop
if(expression is true)
print
I know my code pales in comparison to your cin, but I hope it helps.
-
I'll go a little further, if you have an array of name[5] and element #0 is KARL,GEORGE then name[0][0] will return K. :]
-
i dont know how to compare the first character with the inputted value.
Code:
for(int x = 1; x <= 5;x++)
{
if(start_with == name[x][0])
{
cout << name[x];
}
}
i dont think this is right, and im pretty sure that will not work using cstring.h
-
i think what u need to do is
for(int c=0;c<user_input;c++)
{
cout<<name[c];
}
note that this only shows how u could do so
u need to change it so it suits your needs
-
Code:
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
using namespace std;
int main(void) {
char start_with;
cin >> start_with;
for(int x = 0; x < 5;x++) {
if(tolower(start_with) == tolower(name[x][0])) {
cout << name[x] << endl;
}
}
return 0;
}
I bet this is close to what you already have.
-
Try this...
Code:
/*Compiler : Turbo C++ v3.01*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define MAX 50
int main()
{
char array[5][MAX],character;
int ctr;
for(ctr=0; ctr<5; ++ctr)
gets(array[ctr]);
clrscr();
cout<<"Enter the character:"; cin>>character;
for(ctr=0; ctr<5; ++ctr)
{
if(array[ctr][0]==character)
cout<<array[ctr];
}
cout<<endl<<"Press a key..."; getch(); return(0);
}
-
Re: array search
Quote:
Originally posted by niroopan
hi. i was looking through the "help" in C++ and found a search and attempted to mod it for searching through arrays. Unfortunately, it was unsuccessful.
say i have the an array of name[5] of:
1. KARL,GEORGE
2. SMITH,JOE
3. DEEN,LOW
4. SUPER,MAN
5. SPIDER,MAN
how would i be able to search through the array for the first letter.
User inputs:
if inputted:
S
then will display SUPER,MAN and SPIDER,MAN
Note: need examples which are compatible with <cstring.h>
If you have a single subscripted array of chars called array, you can access the first element like this:
Code:
//the char array
char array[5];
//you access like this:
array[0];
If you have a double subscripted array and you want to access the first element in each row you do this:
Code:
char array[5][5];
for ( int i=0; i<5; i++ )
//this is how you access the first element in each row:
array[i][0];
And you can comopare the characters like this:
Code:
if ( array[0] == 'a' ) //returns true if the first char in the array is a
Hope this is what you are asking for...
Note: You don't need to use the string library <cstring> to do the above, and there is no <cstring.h> there is the old <string.h> and the new <cstring>
-
this code will only work for arrays declared as char]
i need the code for the declaration:
the 'string' class is found in the cstring.h library. That is why im having trouble. I need to search through an array which has the declaration of an array using the declaration type of 'string' instead of the type 'char'.