c++ - 存储和打印字符,但输出不是预期的
问题描述
我对 C++ 很陌生。这段代码应该存储并打印出每隔一个数字并在给定符号时停止#
,但输出很奇怪。它输出类似0x6fdd90
. 任何帮助将非常感激。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s[11];
int count = 1, wordlength = 0;
char a;
cin.get(a);
while (a != '#'){
if (wordlength == 10)
break;
if (count % 2 != 0){
s[wordlength] = a;
wordlength++;
}
cin.get(a);
count++;
}
s[wordlength] = '\0';
cout << s;
return 0;
}
解决方案
cout << s;
正在打印数组中第一个元素的地址s
。您可能需要循环s
打印所有元素。
for (int i =0; i < sizeof(s)/sizeof(s[0]); i++) {
cout<< s[i] << "\n";
}
出于您的目的,使用 char 数组比使用字符串数组更好。
推荐阅读
- image - 如何从 Rust 中的 camera_capture 灰度图像
- ios - cordova-plugin-purchase:即使在调用命令之后,iOS 也不做任何事情
- azure - Fluentd 不输出到文件
- javascript - D3从节点隐藏.text元素
- java - 如何在android中打开日期选择器时设置默认年份(但是在滚动年份时它需要显示其他年份直到当前年份)
- python - 如何捕获包含在 boost python 模块中的 C++ 代码中抛出的 Python 异常
- java - Eclipse Gradle 不支持请求的构建类型“java-library”
- amazon-web-services - AWS 云形成。从帐户架构创建堆栈
- javascript - Vuex 持久化认证状态
- node.js - 将缓冲区转换为 blob 并将其设置为 Nodejs 中的 FormData