Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串赢了';用cout打印_C++_String - Fatal编程技术网

C++ 字符串赢了';用cout打印

C++ 字符串赢了';用cout打印,c++,string,C++,String,我要做的是将两个字符串复制到第三个字符串,在第三个字符串中留下与前两个字符串的字符总数相同的空格,而不使用strcpy或任何类似的方法,下面的代码不会打印stc字符串。它运行,但不会打印 #include <iostream> #include <conio.h> #include <string.h> using namespace std; int main() { int i, j = 0, k = 0, str_1 = 0, str_2 = 0

我要做的是将两个字符串复制到第三个字符串,在第三个字符串中留下与前两个字符串的字符总数相同的空格,而不使用
strcpy
或任何类似的方法,下面的代码不会打印
stc
字符串。它运行,但不会打印

#include <iostream>
#include <conio.h>
#include <string.h>

using namespace std;

int main()
{
  int i, j = 0, k = 0, str_1 = 0, str_2 = 0, str_3 = 0, space = 0;
  char sta[40];
  char stb[40];
  char stc[100] = { };

  cout << "Enter sentence: " << endl << endl;
  cin.getline(sta, 39);
  cout << "Enter Second Sentence: " << endl << endl;
  cin.getline(stb, 39);

  for (i = 0; i < 40; i++) {
    if (sta[i] == '\0') {
      break;
    } else {
      str_1++;
    }
  }
  cout << "The first sentence has: " << str_1 << " characters" << endl << endl;

  for (i = 0; i < 40; i++) {
    if (stb[i] == '\0') {
      break;
    } else {
      str_2++;
    }
  }
  cout << "The second sentence has: " << str_2 << " characters" << endl << endl;

  space = str_1 + str_2;
  for (i = 0; sta[i] != '\0'; i++) {
    stc[space] = sta[i];
    space++;
    str_3++;
  }
  for (j = 0; stb[j] != '\0'; j++) {
    stc[space] = stb[j];
    space++;
    str_3++;
  }
  stc[space] = '\0';
  cout << "The third sentence is: " << stc << endl << endl;
  cout << "And has " << str_3 << " characters " << endl;
  return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int i,j=0,k=0,str_1=0,str_2=0,str_3=0,space=0;
char-sta[40];
字符机顶盒[40];
char stc[100]={};

cout您的问题是,在stc数组中,开头有“\0”字符,因此当它开始读取它时,它已经认为它是字符串的结尾。 而不是做:

space=str_1+str_2;

照办

space=0;

别忘了像这样初始化字符串

char stc[100] = "";
如果你不知道,就会有奇怪的字符已经在里面了,你也不知道字符串什么时候结束


这样,数组将从一开始就开始填充,而不是从随机位置填充。

您的问题是您正在尝试初始化
stc
,但是:

char stc[100] = { };
没有执行您认为它正在执行的操作。事实上,它应该使用所有零值初始化数组,这与NUL终止符
'\0'
字符相同,这会导致以后在第一个字符上停止打印数组。不幸的是,我认为标准不允许您默认使用anyt初始化元素不是0,所以说
char stc[100]={''}
不正确

您可以在测试
sta
stb
中的字符时添加空格:

char stc[100];
// ...
for (i = 0; i < 40; i++) {
    if (sta[i] == '\0')
      break;
    else {
      str_1++;
      stc[space++] = ' ';
    }
  }
// and the same for stb. Now you don't need to assign space = str_1 + str_2
charstc[100];
// ...
对于(i=0;i<40;i++){
if(sta[i]='\0')
打破
否则{
str_1++;
stc[space++]='';
}
}
//stb也是一样,现在不需要分配space=str_1+str_2


当然,如果您使用
std::string
和std::algorithm中的算法,这一切都会变得更容易,但您似乎想以一种艰难的方式来完成。也许可以从编写一个用于计数字符的帮助函数开始?

一些格式设置会很好。还有,您认为它不打印是什么意思?您是否遇到错误或其他问题?没有Works但它不会打印名为stc的字符串它会留下一个空白我建议您使用调试工具逐步检查代码,直到它打印字符串,并确保字符串符合您的预期。我尝试了您的代码,输出看起来都正常,除了stc数组,它一开始不是空的所以一些奇怪的角色出现了。除了STC字符串之外,它打印了所有其他东西。打我一分钟,很好的工作。我不希望它从TyHe开始填充。我想从两个字符串的字符的总和开始填充。为什么你想要这个?从中间开始没有意义。array@KarloShehaj你的问题tion大声要求输入/输出样本数据集。把它放在你的问题中。@KarloShehaj那么至少你需要将你跳过的所有空格设置为空字符串,因为现在你没有初始化stc
中的第一个字符,所以谁知道这些点中可能有什么问题解决了,但谢谢你的回答。