String to multidimensional char vector

Hello, today I was working on a program which compares sequences of characters, counts the differences between them, and displays them. I had the sequence inputted as a string (into a vector so any number of sequences could be chosen), and then, the way I tried to check the strings for differences, was by converting the string to a (multidimensional) vector of chars:
vector< vector<char> > sequencesC;
1
2
3
4
5
6
7
8
9
10
11
12
for (int a = 0; a < sequenceCount; a++) {
    cout << "\nEnter sequence " << a+1 <<" name: ";
    cin >> sequenceNames[a];
    cout << "\nEnter " << sequenceNames[a] << "'s sequence: \n\n";
    cin >> sequences[a];
    std::vector<char> data(sequences[a].begin(), sequences[a].end());

    for (int b = 0; b <= sequenceSize; b++) {                   
    sequencesC[a][b] = data[b];                //crashes here                 
    }                                                           

    }

However, it crashes (as shown above) when I try to set, by a for loop, every char of a multidimensional vector (sequencesC) to the same char of the data vector. Is there any way I can convert the string to a char vector? Or is there a different way I should be doing it? Thanks.
Last edited on
You have a lot of these sequence(s) in your code:

sequenceNames, sequences, sequencesC...

Can you please show these so we can know if they are vectors or arrays.

That brings me to my next point, if you want to access the elements of a vector, use the std::vector::at() function because it is a much safer way of accessing the elements of a vector without going out of the range of the vector capacity. Now if these were arrays, then the method you used is the recommended way.

Also what is data? Is it a vector or array?

p.s. show full code? or just provide the definitions of the methods you used above
Sorry, trying to keep the post short. I'll go ahead and put all the code, but everything is a vector (no arrays).

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
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int sequenceCount;
    vector<string> sequences;
    vector<string> sequenceNames;
    int sequenceSize;
    vector< vector<int> > differences;
    vector< vector<char> > sequencesC;
    cout << "Welcome to Jake's sequence comparison tool! Enter the sequence size: \n\n";
    cin >> sequenceSize;
    cout << "\nEnter the number of sequences: \n\n";
    cin >> sequenceCount;
    sequences.resize(sequenceCount);
    sequenceNames.resize(sequenceCount);

    for (int a = 0; a < sequenceCount; a++) {
    cout << "\nEnter sequence " << a+1 <<" name: ";
    cin >> sequenceNames[a];
    cout << "\nEnter " << sequenceNames[a] << "'s sequence: \n\n";
    cin >> sequences[a];
    std::vector<char> data(sequences[a].begin(), sequences[a].end());

    for (int b = 0; b <= sequenceSize; b++) {
    sequencesC[a][b] = data[b];
    }

    }

    for (int a = 0; a < sequenceCount; a++) {
    for (int b = 0; b < sequenceCount; b++) {
    for (int c = 0; c <= sequenceSize; c++) if (sequencesC[a][c] != sequencesC[b][c]) differences[a][b]++;
        cout << "\nThe number of differences between " << sequenceNames[a] << " and " << sequenceNames[b] << " is " << differences[a][b] << ".\n\n";
    }
    }
}


I'll look into vector::at(), and sorry about all the sequences, didn't have long to work on this.

This works!

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
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
  int sequenceCount;
  vector<string> sequences;
  vector<string> sequenceNames;
  string temp = "";
  int differences = 0;
  cout << "Welcome to Jake's sequence comparison tool! Enter the number of sequences: \n\n";
  getline(cin, temp);
  stringstream m(temp);
  if (!(m >> sequenceCount))
  {
    cout << "\nYou must enter a valid number!\n";
    return 0;
  }
  
  for (int a = 0; a < sequenceCount; a++)
  {
    cout << "\nEnter sequence " << a+1 <<"'s name: ";
    getline(cin, temp);
    sequenceNames.push_back(temp);
    
    cout << "\nEnter " << sequenceNames.at(a) << "'s sequence: \n\n";
    getline(cin, temp);
    sequences.push_back(temp);  
  }
  
  
  for (int a = 0; a < sequenceCount; a++) 
  {
    for (int b = a+1; b < sequenceCount; b++) 
    {
      differences = 0;
      int d = ((sequences.at(a).size() < sequences.at(b).size() ? sequences.at(a).size(): sequences.at(b).size()));//Use the size of the smallest string
      for (int c = 0; c < d ; c++)
      {
	if (sequences.at(a).at(c) != sequences.at(b).at(c))
	  differences++;
      }
      cout << "\nThe number of differences between " << sequenceNames.at(a)<< " and " << sequenceNames.at(b) << " is " << differences << ".\n\n";
    }
  }
  return 0;
}
Smac89, doesn't work
I don't know what you mean. I tried at home and it worked perfectly. Can you post the part of it that doesn't work? Or the input you gave it; it should work for every input

E: Just ran it again and here is the output:

$ ./temp
Welcome to Jake's sequence comparison tool! Enter the number of sequences: 

3

Enter sequence 1's name: Testing with

Enter Testing with's sequence: 

This is a test of the string part

Enter sequence 2's name: Debuging's

Enter Debuging's's sequence: 

just trying to determine the bug in this code

Enter sequence 3's name: here we go

Enter here we go's sequence: 

after I press enter, this should give me the correct answer

The number of differences between Testing with and Debuging's is 30.


The number of differences between Testing with and here we go is 31.


The number of differences between Debuging's and here we go is 43.


Unless maybe you want it to find everything different and not just the differences between each string. The way I implemented it was to use the size of the smallest string (line 40) to check for differences. If you want to find ALL the differences, then you can just easily add to the differences varaible the (size of large string - size of small string) and this should give ALL differences and not just difference between strings
Last edited on
Nvm, worked.
Sorry for taking so long, that's what it was supposed to do, but would not work on my computer... I'll try again.
EDIT: Tried it and it worked - thanks! I didn't know about some of these functions (such as string.at()) or I would have used them. Thanks again.
Last edited on
Topic archived. No new replies allowed.