简单程序省略了第一个字符
First char omitted by simple programm
任务是cin>>....
,只取字母,将大写字母改为小写字母,并用小写字母重写行。我不明白为什么我的代码忽略了输入的第一个字母。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cin >> ch;
while (ch != '@'){
if (cin.get(ch))
{
if (isalpha(ch)){
if (isupper(ch)){
cout <<(char)tolower(ch);
}
else
cout << ch;
}
if (ch == 'n')
cout << "nNie zakonczyles ciagu znakiem @" << endl;
}
else{
cin.clear();
}
}
}
因为循环使用cin.get(ch)
来获取要打印的字符,但实际上第一个字符是用cin >> ch
读取的;然后将结果丢弃。
您可能想要摆脱cin>>ch;
指令并将ch
初始化为与'@'
不同的值,或者将循环转换为do-while
循环,类似于:
char ch;
do
{
if (cin.get(ch))
{
/* Do what is needed */
}
}
while (ch != '@')
cin >> ch; <- read first letter
while (ch != '@'){
if (cin.get(ch)) <- read next letter which tosses out the first letter
设置ch
为某个值,然后去掉cin >> ch;
因为在cin >> ch;
之后是cin.get(ch)
相关文章:
- 如何忽略第一个字符输入后的所有内容
- 增量生成 - 错误:字符串的第一个字符应'0'或'1'
- 对c++中的输入使用getline函数时忽略第一个字符
- C++如何通过忽略每行的第一个字符来读取 unicode 文件
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- 检索字符串的第一个字符,并在C++中按字母顺序进行比较
- 这个RLE解码功能只解码第一个字符,我做错了什么?
- 如何忽略除第一个字符之外的所有字符
- C++ 如何从用户输入中读取第一个字符并忽略 rest(字符类型)
- CIN.IGNORE删除第一个字符的第二次函数被调用
- 使用CTYPES将字符串从Python传递到C - 仅发送第一个字符
- 为什么在使用循环时忽略输入的第一个字符
- 仅将输入的第一个字符用于 char 变量
- getline删除第一个字符;删除cin.ignore();无法解决
- 仅分配第一个字符,将 LPCTSTR 转换为字符*
- 如何获取指向字符数组中第一个字符的常量指针
- 访问没有字符的字符串的第一个字符
- 如何将 char 类型数组传递给函数并将第一个字符数组复制到第二个图表数组并使用 c++ 进行比较
- 文本文件中的第一个字符会导致 .substr() 错误
- RegSetValueEx 仅显示写入第一个字符