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.
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
#include <iostream>
#include <sstream>
#include <vector>
usingnamespace 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;
}
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
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.