为什么我从数组中得到这么多数字
why am i getting so many numbers from my array c++
我仍然在这个项目上工作,我已经修复了我的大部分问题。问题在使用选项B时显示出来,但在选项A中,并将数组发送到.txt文件。有很多额外的数字被添加到.txt文件中。当我计数时,它像它应该的那样显示数组中的数字,但在文本文件中它看起来是这样的anthony201910181114 - 8589934604445358131768008182078541176802418196927161130726102120444535893-之前的数都是正确的,但后面的数都要去掉。为什么会发生这种情况,我该如何解决?谢谢你。
int main()
{
int Stats[6];
char Selection;
string Character1;
int i;
char keep;
do
{
cout << "Hello, welcome to my character generator. Please select an option" << endl;// Menu Options
cout << "A: Create Character Name and generate stats" << endl;
cout << "B: Display Name and Stats" << endl;
cout << "C: Quit" << endl;
cin >> Selection; // Menu Selection
cout << endl;
if ( (Selection == 'a') || (Selection == 'A') )// if user selects a, this happens
{
cout << "Welcome, Before you can start your adventures you must name your character." << endl;
do
{
cout << "Please enter your a name." << endl;// prompts user to enter a name for their Caracter
cin >> Character1;
cout << "Thank you now lets generate your stats." << endl;
for (i=0; i<6;i++)// I Want this to run the function GenerateScore() 6 times and input each result into the next element of Stats[6]
{
Stats[i]=GenerateScore();
}
for(i=0;i<6;i++)// displays your scores to the player.
{
cout << Stats[i]<< endl;
}
cout << "would you like to keep this name and these Stats?"<< endl;
cout << "Y/N" << endl;
cin >> keep;
break;
}
while ( (keep=='n') || (keep=='N') );
ofstream savecharinfo("charactersave.txt");// saves the Name and the filled array Stats[6] to the charactersave.txt file
if(savecharinfo.is_open())
{
savecharinfo << Character1;
for(int i = 0; i<6; i++)
{
savecharinfo << Stats[i];
}
}
else cout << "File could not be opened." << endl;
}
else if ( (Selection =='b') || (Selection == 'B') )
{
cout << " Welcome back, here are is your character." << endl;
ifstream displaycharacter("charactersave.txt");
if(displaycharacter.is_open())
{
while ( getline (displaycharacter,Character1) )
{
cout << Character1 << endl;
}
displaycharacter.close();
}
else cout << "File could not be opened";
}
else
break;
}
while ( (Selection != 'c') || (Selection == 'C') ); // ends the program is c or C is entered.
return 0;
}
int GenerateScore()
{
int roll1 = rand()%6+2;
int roll2 = rand()%6+2;
int roll3 = rand()%6+2;
int sum;
sum=roll1+roll2+roll3;
return sum;
}
就像评论中提到的:
while ( (keep='n') || ('N') );
应该(至少)改为
while ( (keep == 'n') || (keep == 'N') );
同样,这个循环:
for(int i = 0; Stats[i]; i++)
仅在Stats[i]
求值为false时终止,而Stats[i] == 0
求值为false时才会终止。这个循环导致文件中出现额外的字符:代码不断访问超出边界的数组元素,直到超出边界的元素随机== 0。你可能需要for(int i = 0; i < 6; i++)
相关文章:
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 数组/c++中的大量数字
- 遍历并行数组以确定C++中的最大数字
- 整数区间(或 int 数组)中每个数字的出现次数
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 获取 2D 数组 c++ 中的所有数字对
- 为什么使用数组元素查找最大数字的程序不起作用?
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 以十为基数的数字到布尔数组,该数组要求二进制/基数为 2 的数字
- C++ 检查结果数组中有多少次数字
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 如何在C++中有效地将数字值重新分配给字符数组
- 查找数组中的最小数字
- 查找数组中重复(重复)数字的索引
- 我必须找到给定数组中所有数字的周期,就像有很多解决方案,但数组的大小是 10^5
- 类、数字/数组或对象中的对象