【发布时间】:2014-11-22 12:49:56
【问题描述】:
如何逐个字符扫描字符串并将每个字符打印在单独的行中,我正在考虑将字符串存储在数组中并使用 for 循环进行打印,但我不知道如何... .请帮忙!!!
这是我的代码:
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str;
char option;
cout << "Do you want to enter a string? \n";
cout << " Enter 'y' to enter string or 'n' to exit \n\n";
cin >> option ;
while ( option != 'n' & option != 'y')
{
cout << "Invalid option chosen, please enter a valid y or n \n";
cin >> option;
}
if (option == 'n')
return 1;
else if (option == 'y')
{
cout << "Enter a string \n";
cin >> str;
cout << "The string you entered is :" << str << endl;
}
system("pause");
return 0;
}
【问题讨论】:
-
请正确格式化您的问题。
-
在 stdio.h 中定义的 c 中的 getchar() 函数在 c++ 中运行良好,逐个字符地读取输入。 stackoverflow.com/questions/3659109/string-input-using-getchar