C ++ char数组输入-说明 [英] c++ char array input - explanation

查看:374
本文介绍了C ++ char数组输入-说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个练习来编写一个程序,该程序接收一个句子,然后从每个单词中取出第一个字母,然后创建一个新单词.

I have an exercise to write a program that receives a sentence and then takes from each word the first letter and creates a new word.

我的代码:

int main(){

char* str = new char[50];

for (int i = 0; i < 50; i++)
    str[i] = NULL;

cin >> str; 

cout<<str;

for (int i = 0; i < 50; i++) 
    cout << str[i]; 


system("pause");
return 0;
} 

但是当我要打印句子时,它仅打印第一个单词.

But when I want to print the sentence it prints only the first word.

input: 
abcdef abc des

output: 
abcdef abc des
abcdef *******************************************

当我按下空格时,什么会进入数组? 我怎么知道我何时使用FOR循环在数组上运行?何时到达有空间的角色?

And when I press a space what goes into the array? How can I know when I'm running on the array with the FOR loop When do I get to the Character where there is space?

推荐答案

cin >> str;在找到空格字符时停止.

cin >> str; stops when it finds a space character.

使用它来读取整行:

std::string myStr;
std::getline( std::cin, myStr);

注意:您必须包括<string>

这篇关于C ++ char数组输入-说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆