[英]how can i initialize a dynamically allocated array of characters so i don't get those weird characters?
I want to use that array of characters in my game of "hangman" to view to tha user his current progress. 我想在我的“hangman”游戏中使用那个字符数组来查看用户当前的进度。
#include <iostream>
#include "randword.h"
#include <fstream>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
InitDictionary();
string tixaio=Randomword();
int m = tixaio.length();
int guesses=8;
char *charptr= new char[m];
for(int aa=0;aa<m;aa++){
charptr[aa]='-';
}
cout << "The word now looks like this: "<<charptr;
}
As soon as i try to cout<< charptr my array i got the usual "---------" plus some weird characters. 一旦我尝试cout << charptr我的数组我得到了通常的“---------”加上一些奇怪的字符。 How can i prevent those characters from showing up??
我该如何防止这些角色出现?
Strings in C and C++ must end in a "\\0" character: a character that is the value 0. C和C ++中的字符串必须以“\\ 0”字符结尾:一个值为0的字符。
However, you should be using std::string
instead of an array of characters. 但是,您应该使用
std::string
而不是字符数组。 Then you won't have to worry about ending in a null character. 然后你不必担心以空字符结尾。
When you print an array of characters in C++, cout (or any ofstream object, for that matter) looks for the null terminating character to determine when to stop printing. 当您在C ++中打印字符数组时,cout(或任何流对象)会查找空终止字符以确定何时停止打印。 Now, if you've only allocated
m
bytes for the array, and cout tries to access memory outside of those m
bytes, you could possibly even get a segmentation fault. 现在,如果你只为数组分配了
m
个字节,并且cout尝试访问那些m
个字节之外的内存,你甚至可能会遇到分段错误。
One way to avoid this is to allocate space for one more character than what's needed, and assign '\\0' to that extra character. 避免这种情况的一种方法是为一个或多个字符分配空间,并为该额外字符分配'\\ 0'。
char *charptr= new char[m+1];
charptr[m] = '\0';
Since you're using C++, it has a built in std::string class which you can use for your purpose. 由于您使用的是C ++,因此它具有内置的std :: string类,您可以将其用于您的目的。 It automatically appends the null terminating character to the string.
它会自动将空终止字符附加到字符串。 This behavior can be verified by calling the
c_str()
function on an object of type std::string (this converts std::string to char*). 可以通过在类型为std :: string的对象上调用
c_str()
函数来验证此行为(这会将std :: string转换为char *)。 It'll always return n+1
characters, where n is the number returned by std::string's length()
function. 它总是返回
n+1
字符,其中n是std :: string的length()
函数返回的数字。 And as you can guess, the last character will be \\0. 正如你猜测的那样,最后一个字符将是\\ 0。
它是一个char *,AFAIK,你需要在末尾有一个\\ 0终止符,以便打印知道何时停止。
clear the array to 0 将数组清除为0
memset (array, 0, size);
Or after you make the string, append a '\\0'
just after the end of the string, which will indicate the end of string. 或者在创建字符串之后,在字符串结尾之后追加一个
'\\0'
,这将指示字符串的结尾。 This will prevent the printing routing go beyond the limit which you want. 这将阻止打印路由超出您想要的限制。 In your case you should place
charptr[aa] = '\\0'
just after the for
loop. 在你的情况下,你应该在
for
循环之后放置charptr[aa] = '\\0'
。 Make sure that you have enough allocated memory block to charptr
you need to allocate the length of string + 1 (for the '\\0'
character). 确保你有足够的分配内存块来
charptr
你需要分配字符串+ 1的长度(对于'\\0'
字符)。
你必须在末尾插入'\\ 0'字符,因为它是一个以空字符结尾的字符串
You should add a \\0
as the last element after you fill up the array. 填充数组后,应该添加
\\0
作为最后一个元素。
Better option is to use std:;string
that is the C++ way of doing things better and in a elegant manner, without having to bother about null terminating as in case of char*
更好的选择是使用
std:;string
,它是C ++以更好的方式处理事务的方式,而不必像char*
那样烦扰null终止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.