[英]C++ Arrays and writing of values
I'm trying to write a pu.nett square generator for a biology class, it's quite simple, but I can't figure out how to get the values to write to the other blocks.我正在尝试为生物 class 编写一个 pu.nett 方形生成器,它非常简单,但我无法弄清楚如何获取要写入其他块的值。 I can get the individual variables in, but I can't get the values to combine in the lower squares.我可以获取单个变量,但无法获取要在下方方块中组合的值。 Any help would be appreciated.任何帮助,将不胜感激。 Code is attached below.代码附在下面。
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
char p_s[2][2] = {0};
int i = 0;
int j = 1;
cout << "Input first parent's first gene.\n";
cin >> p_s[i][j];
j++;
cout << "Input first parent's sencond gene.\n";
cin >> p_s[i][j];
system("Pause");
cout << "First Gene: " << p_s[0][1] << endl << endl << "Second Gene: " << p_s[0][2];
j = 0;
i++;
cout << endl << endl << "Input second parent's first gene: ";
cin >> p_s[i][j];
i++;
cout << "Input second parent's second gene: ";
cin >> p_s[i][j];
cout << "First Gene: " << p_s[1][0] << endl << endl << "Second Gene: " << p_s[0][2];
system("PAUSE");
p_s[1][1] = p_s[0][1] p_s[1][0];
cout << p_s[1][1];
return 0;
}
First of all, I can see a problem with int j
:首先,我可以看到int j
的问题:
In line three you initialize it to one在第三行,你将它初始化为 1
int j = 1;
Three lines later you increment it up by one:三行之后你将它加一:
j++; // j is now equal to 2
//...
cin >> p_s[i][j]; // j is still equal to 2, which is outside of the array bounds!
This same thing happens in lines 10 and 18, where you attempt to access p_s[0][2]
, which is outside of the array's bounds.同样的事情发生在第 10 行和第 18 行,您在其中尝试访问数组边界之外的p_s[0][2]
。
Also, if you're looking to make a traditional pu.nett square, each square would require two char
's worth of storage, which just isn't available with your current system (You'd need 8 char
's, and p_s
only has four).另外,如果你想制作一个传统的 pu.nett 正方形,每个正方形需要两个char
的存储空间,这在你当前的系统中是不可用的(你需要 8 个char
和p_s
只有四个)。
However, if you were looking to revise your program, you could always try an object oriented approach, by splitting your pu.nett and parents into classes like this:然而,如果你想修改你的程序,你总是可以尝试一种面向 object 的方法,将你的 pu.nett 和父母分成这样的类:
struct Parent
{
std::string a;
std::string b;
Parent(std::string _a, std::string _b) : a(_a), b(_b) {}
}
And和
struct Punnett
{
std::vector<std::string> GeneCombos;
Punnett (Parent one, Parent two) {
GeneCombos.push_back(one.a + two.a);
GeneCombos.push_back(one.a + two.b);
GeneCombos.push_back(one.b + two.a);
GeneCombos.push_back(one.b + two.b);
}
}
This way you don't have to worry about the values of the Pu.nett square: They're created in the constructor.这样您就不必担心 Pu.nett 正方形的值:它们是在构造函数中创建的。
If you wanted to go a step further, you could add to the constructors of both classes to make them assign their variables based on user input:如果你想 go 更进一步,你可以添加到两个类的构造函数中,使它们根据用户输入分配变量:
struct Parent
{
std::string a;
std::string b;
Parent(std::string _a, std::string _b) : a(_a), b(_b) {}
Parent() { //If no values are passed, it asks for them
std::cout << "\nEnter Gene One: ";
std::cin >> a;
std::cout << "\nEnter Gene Two: ";
std::cin >> b;
}
};
And for the Pu.nett class:对于 Pu.nett class:
class Punnett
{
std::vector<std::string> GeneCombos;
public:
void draw() {
std::cout << "\nResulting Combinations:\n";
for (unsigned int i = 0; i < 4; ++i)
std::cout << GeneCombos[i] << '\n';
}
Punnett (Parent one, Parent two) {
GeneCombos.push_back(one.a + two.a);
GeneCombos.push_back(one.a + two.b);
GeneCombos.push_back(one.b + two.a);
GeneCombos.push_back(one.b + two.b);
}
};
Using the modified classes, your main() would only be five lines long:使用修改后的类,您的 main() 将只有五行:
int main()
{
Parent One;
Parent Two;
Punnett punnett(One, Two);
punnett.draw();
return 0;
}
Hoped this helped.希望这有帮助。
EDIT*: As Ben Voigt wisely pointed out, the Pu.nett::draw() function didn't belong in the constructor, and was moved to main().编辑*:正如 Ben Voigt 明智地指出的那样,Pu.nett::draw() function 不属于构造函数,而是移到了 main() 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.