Searching through an array (with loops probably?)

Okay, so for my program, I need it to take input and put it into an array if the same thing string is not found within that array.

For example: First input is apples, the program searches through the list, and doesn't find apples, so it adds apples somewhere (I'm not sure where to add it to the array, as I'm not used to not being able to .append stuff to a list like in python).
Now the second input is oranges, oranges is found in the array, so it doesn't add it again.

I wrote like a test code that would try something like that, but it's just a basic algorithm that I can't get to work:

int yes = 0; // 0 means not found, 1 means found
while (size < capacity) // while the size (current element) is less than
// capacity of the list
{

if (*names == name)
{
yes = 1;
}
else yes = 0;

size++;
}
size = 0;
if (yes == 1){
// do something here

}
else *names = name; // add the name to the array
cout << *names;


Does this even make sense? lol

Note that I'm using loops, because this is beginners programming and we're not allowed to use anything we haven't learned in class.
Last edited on
Here a possible solution in pure C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int i, j;
bool exists;
int counter = 0;
static int allocated = 0;
int length;
char ch;
char name[100];
char **names = NULL;
while(1){
 printf("insert %i. name or press enter to abort: ", counter + 1);
 for(length = 0, ch = 0; length < 99; length++){
  scanf("%c", &ch);
  if(ch == 10 || ch == 13){break;}
  name[length] = ch;
 }
 if(!length){
  break;
 }
 name[length] = 0;
 length++;
 exists = false;
 for(i = 0; i < counter; i++){
  for(j = 0; name[j] && names[i][j] && names[i][j] == name[j]; j++){}
  if(!name[j] && !names[i][j]){
   exists = true;
   break;
  }
 }
 if(exists){
  printf("Name '%s' exists.\n\n", name);
 }else{
  if(counter + 1 >= allocated){
   allocated += 1024;
   names = (char **)realloc(names, allocated * sizeof(char *));
  }

  names[counter] = (char *)malloc(length);
  for(i = 0; i < length; i++){
   names[counter][i] = name[i];
  }
  counter++;
  printf("Name '%s' created.\n\n", name);
 }
}

for(i = 0; i < counter; i++){
 free(names[i]);
}
free(names);
This is a C++ forum, jmc...

Why are you teaching a C++ beginner to write code in C? You'll confuse him, and make it more difficult.

That's like trying to get a Perl programmer to do something in Python, fgs...
Last edited on
Afaik C++ doesn't exclude C.
Some parts of C++ are often very useful, but many parts of C++ implicit unnecessary procedures, which I do not need here for example.

[EDIT]You should even find every part of the code above in the C++ tutorial or in the C++ reference of this C++ website[/EDIT]
Last edited on
You should even find every part of the code above in the C++ tutorial or in the C++ reference of this C++ website


Actually there is no tutorial on printf, free, malloc, realloc, or scanf in the c++ tutorial on this site.
Last edited on
But it is in the reference.
C Library
The elements of the C language library are also included as a subset of the C++ Standard library

http://cplusplus.com/reference/clibrary/cstdio/
http://cplusplus.com/reference/clibrary/cstdlib/
Last edited on
But it is in the reference.


I`m fully aware of that, I don`t think anyone is debating whether or not it is possible to use C in C++. But the point is, when the very intelligent people who made this site wrote their tutorial to teach C++, they omitted the C stuff. And on these boards we are supposed to be doing the same, teaching c++. Do you really think that if the TS of this thread handed in your solution to his instructor he would get 100%? No, he is going to probably get failed for blatant plagiarism because he copied someone else's code. That isn't very helpful of you to do to him.
C is part of C++ and therefore included in the stadnard library. There was nothing to be omitted.
This is also the reason why it is not called D, but C and an expansion of C (++).

As for the rest you are contradicting yourself. You say he does not get 100% if he copied this post and he would probalbly fail because of copying the code. He can't copy the code since his teacher certainly expects him to use other functions for the output as well as most probably for the memory allocation. The failure of realloc is not considered in the code above too. This is also something he would have to do himself.

At last, if he really were as stupid as to hand in a copy of the code above without to rewrite it in his own way he would definitely deserve a fail.
C is part of C++


Gee that's some real Nancy Drew work there, Nancy. If you read my post
I don`t think anyone is debating whether or not it is possible to use C in C++
you would see that
I`m fully aware of that
.

his teacher certainly expects him to use other functions for the output as well as most probably for the memory allocation


So you gave him completely useless and overly convoluted code then? Because you sure didn't explain any of that code to him so he could substitute the stupid things you told him to do with a real solution, you just posted the code and said "Here, this is how it's done". What else can he do besides copy it?

As Helios told you a couple of days ago:

jmc: Are you trying to break some sort of "most bad advices given in a single day" record or something?


Last edited on
Jmc, the point still stands that this is a C++ forum. C isn't part of C++. They're two seperate languages. C++ is a superset of C, it is true, but there is some major differences. Not all C functions are included in C++ as standard.

So again, why post "pure C" on a C++ forum? It doesn't make a lick of sense.

Also, why are you trying to get him to use pointers? He states that he can't use anything he's not yet learned in class; if he had learnt pointers then he would be using pointers. So you're going to get him a fail anyway, even if he doesn't plagiarise your code.

Go to a C forum if you want to talk C.
1. He uses pointers in his example code.
2. Give me an example how it could be realised withoud using addresses (pointers).
3. Where the hell did you all learn that C is not part of C++ (sry for generalising and being so rude). Even Wikipedia will tell you it is. I hope you agree on this point:It was developed by Bjarne Stroustrup. If you don't I can't help you. If you do get a proper book from him like "Bjarne Stroustrup - The C++ Programming Language - 3rd edition" or "Bjarne Stroustrup - The Design and Evolution of C++". You can even find it on http://www.research.att.com/~bs/homepage.html.

If your opinion is still that "C is not part of C++" and "C++ is a better C is wrong" and "C++ is not an extension of C" then please ask your prof or whatever you have at hand or give my some reasons of a reliable source why it is not.

I need it to take input and put it into an array if the same thing string is not found within that array.

Why do you want to use an array? There are other data structures that automatically reject duplicates. Here is an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <string>

const size_t number_of_words = 3;

int main()
{
    std::set<std::string> words;
    std::cout << "Please enter " << number_of_words << " words:\n";

    while (words.size() < number_of_words)
    {
        std::string word;
        std::getline(std::cin, word);
        words.insert(word);
    }

    std::cout << "\nYou entered:\n";

    std::copy(words.begin(), words.end(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
}
Topic archived. No new replies allowed.