Hi, I was making a program like an array which reads the list and tells what position is it in the list.
For example:
For letters its: list[2]{'A','B'};Its like a search program and it works because it reads the list. So now I was figuring out on how it will read words that are in arrays like dog, cat, mouse etc.. is it like this? list[][3]={"dog","cat"}? Any suggestions on how it can be done by using the concept of arrays?

#include <string>
...
string myArray[] = {"Cat", "Dog", "House"};

Hope that helps,
Chris

I
It says string undeleclared and My Array also Undeclared. It does not recognize the that line. Even with the <string.h>

I should of added its a member of the std namespace.

#include <string>
#include <iostream>

int main(void){
    
    std::string myArray[] = {"Cat", "Dog", "House"};
    
    for(int i = 0; i < sizeof(myArray)/sizeof(*myArray); i++){
            std::cout << myArray[i] << std::endl;
            }
    std::cin.get();
    return 0;
}

Chris

Chris Thx for the Solution. I was making a selection search like when you open the program if the user enters Dog it tells him that the text is in the list and its in position 2. I cant seem to make it detect the position. Attached is my .cpp file can you please comment.

Please just post the code, the download doesn't work anyway.

Doesn't matter how long it is. And make sure you use [code] tags

Chris

Edited 3 Years Ago by Dani: Formatting fixed

#include <stdio.h>
#include <conio.h>
#include <string>

int linearsearch(char a[], char v, int l, int r)
  { int i;
    for (i = l; i <= r; i++)
      if (v == a[i]) return i;
    return -1;
  };
//--------------------

int binarysearch(char a[], char v, int l, int r)
  { 
    while (r >= l)
      { int m = (l+r)/2;
        if (v == a[m]) return m;
        if (v < a[m]) r = m-1; else l = m+1;
      }
    return -1;
  };
//------------------------- 
void selection(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      { int min = i;
        for (j = i+1; j <= r; j++) 
            if (a[j] <  a[min]) min = j;
        t = a[i];
        a[i]= a[min];
        a[min] = t;
      } 
  };

//------------------------------------

void bubble(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      for (j = r; j > i; j--) {
        t = a[j-1];
        a[j-1] = a[j];
        a[j] = t;
        }  
  };

main(void)
{
  int i;
  char findme;
  int found;
  std::string list[] = {"Cat", "Dog", "House"};
  selection(list,0,25);  
  printf("Enter a character to find: ");
  scanf("%c", &findme);
  found = binarysearch(list,findme,0,25);
  if (found == -1) printf("\n\n%c is not in the list.",findme);
  else printf("\n\n%c is in position %d of the list.",findme,found);
  getch();
};

You need to update your functions to take string arguments rather than char's.

Also your selection sort will not work on strings, as far as i am aware.

You should be using int main(void) and return 0 !
Also are you sure that this is C++ and not C?

It certainly looks like C to me

Chris

Its C but C and C++ are almost similar thats why I posted here. I know I have minor problem here because the program works fine if it is for chracters like 'A' 'B' but for words or strings it gives me only 2 errors whichi is

cannot convert `std::string*' to `char*' for argument `1' to `void selection(char*, int, int)'

and

cannot convert `std::string*' to `char*' for argument `1' to `int binarysearch(char*, char, int, int)'

Hope I can find the solution for these 2 errors so that my program will work.

I did tell you the solution above, but they still wont wor either way!

You should of posted in the C forum! Rather than C++.

string object do not exsist in C, you have to use an array of chars so to speak

char *words[] = {"This", "should", "be", "in", "the", "C", "Forum"};

Chris

Thx for that tip Chris... Here is my last problem

Invalid conversion from `const char*' to `char'

Dude thx to you its apearring now but I got a small problem which is the program runs but it displays that the word is not in the list.

#include <stdio.h>
#include <conio.h>

int linearsearch(char a[], char v, int l, int r)
  { int i;
    for (i = l; i <= r; i++)
      if (v == a[i]) return i;
    return -1;
  };
//--------------------

int binarysearch(char a[], char v, int l, int r)
  { 
    while (r >= l)
      { int m = (l+r)/2;
        if (v == a[m]) return m;
        if (v < a[m]) r = m-1; else l = m+1;
      }
    return -1;
  };
//------------------------- 
void selection(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      { int min = i;
        for (j = i+1; j <= r; j++) 
            if (a[j] <  a[min]) min = j;
        t = a[i];
        a[i]= a[min];
        a[min] = t;
      } 
  };

//------------------------------------

void bubble(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      for (j = r; j > i; j--) {
        t = a[j-1];
        a[j-1] = a[j];
        a[j] = t;
        }  
  };

main(void)
{
  int i;
  char findme[20];
  int found;
  char *list[] = {"Cat", "Dog", "House"};
  
  printf("Enter a character to find: ");
  scanf("%s", &findme);
  selection(*list,0,0);
  found = binarysearch(*list,findme[20],0,0);
  
  if (found == -1) printf("\n\n%s is not in the list.",findme);
  else printf("\n\n%s is in position %d of the list.",findme,found);
  getch();
};

This code is working if we use the functions for a one letter character. Can you suggest me specifically what parts to change so that I can make this program work.

Please read the links, i'm not going to do everything for you. They are clear enough. You can realte them to your code and see what is different.

If when trying to replicate one of the solution you have a problem or do not understand a function for example strcmp() then ask about it.

Chris

This article has been dead for over six months. Start a new discussion instead.