康卡特程序,奇怪的符号
Concat Program, Weird symbols
我正在遵循"傻瓜C++"部分关于连接字符串。但是,我下面的程序输出连接的两个字符串,但中间有很多奇怪的符号。
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
void concatString(char szTarget[], const char szSource[]);
int main()
{
//read first string
char szString1[128];
cout << "Enter string #1";
cin.getline(szString1, 128);
//second string
char szString2[128];
cout << "Enter string #2";
cin.getline(szString2, 128);
//concat - onto first
concatString(szString1, " - ");
//concat source onto target
concatString(szString1, szString2);
//display
cout << "n" << szString1 << endl;
system("PAUSE");
return 0;
}
//concat source string onto the end of the target string
void concatString(char szTarget[], const char szSource[])
{
//find end of the target string
int targetIndex = 0;
while(szTarget[targetIndex])
{
targetIndex++;
}
//attach the source string onto the end of the first
int sourceIndex = 0;
while(szSource[sourceIndex])
{
szTarget[targetIndex] = szSource[sourceIndex];
targetIndex++;
sourceIndex++;
}
//attach terminating null
szTarget[targetIndex] = '/0';
}
输出显示为
输入字符串 #1hello输入字符串 #2world
你好 - 0╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Óu¬ñ°'world0按任意键继续 . . .
问题就在这里:
//attach terminating null
szTarget[targetIndex] = '/0';
字符文字应' '
。 表示法是一个反斜杠,后跟一到三个八进制数字:它创建一个具有编码值的字符。 char(0)
==