-2

I took your advice to heart and spent last night reading up. The entire program works as it should now except for one last problem. I get an incorrect character at the end of my sequence array.

When I run my code I get a char from the next array at the end. It displays in my test loop: example

I'm hoping this is just a simple problem and not another conceptual one. Any help is appreciated!

My code is as follows:

void promptNames(int numberRelatives, char relative[][256])
    {
       char name[256];
       for (int a = 0; a < numberRelatives; a++)
       {
          cout << "Please enter the name of relative #" << a + 1 << ": ";
          cin  >> relative[a];
       }

       cout << endl;

    }

void promptSequence(int numberRelatives, char relative[][256],
                   char sequence[][9])
{
   string tempString;
   for (int b = 0; b < numberRelatives; b++)
   {
      cout << "Please enter the DNA sequence for ";
      cout << relative[b] << ": ";
      cin  >> tempString;
      for (int c = 0; c <= 9; c++)
      {
         sequence[b][c] = tempString[c];
      }
   }

   for (int k = 0; k <= numberRelatives; k++) //testing what sequence will be
   {
      for (int g = 0; g < 10; g++)
      {
         cout << sequence[k][g];
      }
      cout << endl;
      }

   cout << endl;
}
Kirby
  • 77
  • 9
  • 4
    That code in the `main` function does not do what you think it does. For example `const char startDNA[9] = {promptDNA()};` will initialize the *first* element, and set the rest to zero (and since `promptDNA` always return `0` the first element will *also* be zero). I suggest you [get a couple of good beginners books to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over (because it seems you have some very basic misunderstandings about how local variables work). – Some programmer dude Sep 16 '17 at 20:28
  • 2
    Please do not use `char[]` for storing strings if you have `std::string` available. It creates a lot of misunderstandings and countless potential errors. For example, your `promptDNA()` function is written very unsafely – Fureeish Sep 16 '17 at 20:30
  • In addition, your `promptDNA` function uses a totally different variable `startDNA[9]` that you declare in the function locally, and the function returns `0` and nothing else. That means you get the input from the user, but then throw it away, and set your original `startDNA` variable to zero when the function returns. All the rest of your processing is using invalid data. You definitely need to find a good book or tutorial. – Ken White Sep 16 '17 at 20:32
  • Your loops `for (int c = 0; c <= 9; c++)` and `for (int g = 0; g < 10; g++)` go out of bounds. Array of dimension 9 means the indices are 0 through 8. Also it is undefined behaviour to do `tempString[c]` beyond the end of `tempString`. – M.M Sep 18 '17 at 01:13
  • That was it. Thanks so much! – Kirby Sep 18 '17 at 01:21

1 Answers1

2

Here goes nothing. This is not your answer, you can't turn it in as your homework.

However, my goal is to make sure you've seen true C++ code at least once in your course:

Live On Coliru

#include <iostream>
#include <string>
#include <vector>

struct Relative {
    std::string name;
    std::string sequence;
};

using Relatives = std::vector<Relative>;

std::string promptDNA() {
    std::string dna;
    std::cout << "Enter your DNA sequence: ";
    std::getline(std::cin, dna);
    return dna;
}

auto promptRelatives() {
    Relatives relatives;

    while (1) {
        Relative r;

        std::cout << "Please enter the name of relative #" << (relatives.size() + 1) << ": ";
        std::getline(std::cin, r.name);

        if (r.name.empty())
            break;

        std::cout << "Please enter the DNA sequence for " << r.name << ": ";
        std::getline(std::cin, r.sequence);

        relatives.push_back(std::move(r));
    }

    std::cout << "\nReceived " << relatives.size() << " relatives\n";
    return relatives;
}

#include <algorithm> // std::minmax
#include <tuple>     // std::tie

double match(std::string const& seq1, std::string const& seq2) {
    size_t shortest, longest;
    std::tie(shortest, longest) = std::minmax(seq1.length(), seq2.length());

    double total = 0;
    for(size_t n = 0; n < shortest; ++n)
        if (seq1[n] == seq2[n]) total += 1;

    return total / longest;
}

#include <iomanip>

void displayPercent(std::string const& startDNA, Relatives const& relatives) {
    for (auto& relative : relatives) {
        std::cout 
            << "Percent match for " << relative.name << ": " 
            << std::fixed << std::setprecision(1) << (100.0*match(startDNA, relative.sequence)) << "%" 
            << std::endl;
    }
}

int main() {
    std::cin.exceptions(std::ios::failbit);
    try {
        auto const startDNA = promptDNA();

        auto relatives = promptRelatives();
        displayPercent(startDNA, relatives);

    } catch(std::ios::failure const& f) {
        std::cout << "Input error " << f.code().message() << "\n";
    }
}

Output:

enter image description here

sehe
  • 374,641
  • 47
  • 450
  • 633