help please in the problem

i have (array[0]= sam,john)( array[1]= alex,yusha,cesar,mark ) and so on according to user input no specific order or relation between number of names or number of arrays he will enter.
i put the on another array but every name alone ex array2[0]= sam array[1]=john and so on .
the issue is i want to cout only array [0] or array[1] in the form of array2 ex :

sam
john

i cant know how to make it while the number of names and arrays is known only by user
Last edited on
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
50
51
52
53
54
#include <iostream>
#include <sstream>

int main(){

    std::string array[20];
    std::string array2[20];
    int totalArray = 0;
    int totalArray2 = 0;

    /* INPUT */
    std::string input;

    std::cout << "How many: ";
    std::getline(std::cin, input);

    std::stringstream ss(input);
    ss >> totalArray;

    for(int a = 0; a < totalArray; a++){
        std::cout << "array[" << a << "]: ";
        std::getline(std::cin, array[a]);
    }

    // another input method without specifying how many input there's going to be
    // while(std::getline(std::cin, array[totalArray])){
    //     ++totalArray;
    // }

    /* PROCESS */
    for(int a = 0; a < totalArray; ++a){

        array[a].push_back(',');

        int first = 0;
        int last = array[a].find(",");

        while(last != std::string::npos){
            array2[totalArray2++] = array[a].substr(first, last - first);
            first = last + 1;
            last = array[a].find(",", last + 1);
        }

        array[a].pop_back();

    }

    /* OUTPUT */
    std::cout << "\nOutput:\n";
    for(int a = 0; a < totalArray2; a++){
        std::cout << "array2[" << a << "]: " << array2[a] << "\n";
    }

}


Trying to understand your question, correct me if I misunderstood it.

How many: 2
array[0]: sam,john
array[1]: alex,yusha,cesar,mark
Output:
array2[0]: sam
array2[1]: john
array2[2]: alex
array2[3]: yusha
array2[4]: cesar
array2[5]: mark
you are mostly understand but i want only one array tto cout which i dont know its number i want a way to know the numbers of array2[] which included in array[] for only one array .

what i want exacly how to know the start and end of array[] in array2[] :D .
I don't get your question.

Why don't you give an example?
what's the input and the output?

INPUT : ?
OUTPUT : ?
input : array[1]

output :
array2[2]: alex
array2[3]: yusha
array2[4]: cesar
array2[5]: mark


or input :array[0]
output :
array2[0]: sam
array2[1]: john


the user will enter all that arrays they are already saved with no relation between them i want to cout only one array when the user write something refer to it but my problem i dont know how to know the number of ',' in each array that i can make a for loop from number the array[] start in array2[] and end .
closed account (48T7M4Gy)
madomado60 this is not a homework site and it's now your turn to write some code. You input a (single) simple array of 5 names. And then you input a couple of index numbers and print out the corresponding names.
thats an example not the whole problem with me. in the project i do the user will input up to 100 line of format (name1,name2..........) in array[] as user decides and each contains up to 10 names in that format also as user decides i put them in the array2[]. it isnt the problem.
my problem how to output one of the 100 arrays in the format:
array2[2]: alex
array2[3]: yusha
array2[4]: cesar
array2[5]: mark
i want it as a base for construction not only for cout 1 array as i will use it in about 12 operation easily if i can do it
i only need to know how to do it not the code

i tried only i can get the number of arrays the user input and the number of arrays after cutting each name.

exactly what i want
for(int i=range1;i<=range2;i++){
cout<<array2[i]<<endl;
}

to get a way to compute range1 and range2 for any array[].

sorry as i am a super beginner i dont know any tricks and i tried what i can do only that i ignore making array2[] and cutting array[] cout it by one for loop but it wont help me in my project next operations



oh i just get the idea after posting the replay thx i will make an index[]. index[0] as i finish the first cutting it will store the number of names in first array then in next index[1] store number of names in first and second array and so on by that way i get range1 and range 2 am i right ?
closed account (48T7M4Gy)
Wrong! You only need one array. Each name stored in the array is an element of the array. The elements are numbered and that number is the index value. All the information, including index values is in the one and only array. You don't need to build a new index array.

Please read and study the tutorial here on arrays http://www.cplusplus.com/doc/tutorial/arrays/ and then write some code. If you don't know how to do that then perhaps show this thread to your teacher or tutor so you can get some intensive help. :)
i do it and it worked it isn't wrong :D.
closed account (48T7M4Gy)
Excellent.
Topic archived. No new replies allowed.