使用"if"打印大写字母
Printing uppercase letters using 'if'
我做错了什么?我需要这个,使用'if'打印所有大写字母,但是有些东西是错误的,因为它打印一个字母不止一次!感谢任何帮助:)
//Uppercase letters using if;
#include<iostream>
using namespace std;
int main()
{
char character='A';
label1:
if(character>='A')
if(character>='Z')
goto label2;
else
{
cout<<character<<endl<<character++<<endl;
goto label1;
}
label2:
cout<<"End"<<endl;
return 0;
}
将character
变量增加两次。我发布这个答案只是为了告诉你什么是技术上的错误,但有一个更大的概念上的错误,这是使用goto
。我建议你读一些c++的入门书籍。
//Uppercase letters using if;
#include<iostream>
using namespace std;
int main()
{
char character='A';
label1:
if(character>='A')
if(character>'Z'))
goto label2;
else
{
cout<<character<<endl;
character++;
goto label1;
}
label2:
cout<<"End"<<endl;
return 0;
}
每个字母多次打印的原因如下:
cout<<character<<endl<<character++<<endl;
^^^^^^^^^ ^^^^^^^^^
以上每一个都将导致character
被写入cout
。
有点晚了,但是:
#include "stdafx.h"
#include<iostream>
#include <string>
using namespace std;
int main()
{
string s("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
if (1) goto label1;
cout << "End" << endl;
return 0;
label1:
cout << s << endl;
return 0;
}
你的代码漏掉了两点-
- 未打印
Z
- 打印字符两次
cout<<character<<endl<<character++<<endl;
改变:if(character>='Z')
to if(character>'Z')
EDITED character++(这是打字错误)cout<<character<<endl<<character++<<endl;
至 cout<<character++<<endl;
相关文章:
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 如何在 c++ 中将所有大写字母转换为字符串中的小写字母,反之亦然?
- 大写字母
- 字符数组[]中的小写大写字母C++通过指针
- 如何在德语大写字母 Ä, Ö, Ü, ẞ 上正确应用 C++
- 删除 C 字符串中的大写字母
- 如何检查包含 utf8 文本的 std::string 在 Windows 中是否以大写字母开头?
- 用字母打印以 10-16 为基数(我被困在一个必须打印出任何数字的项目上)
- 正则表达式表示非连续大写字母和小写字母
- C++函数来检查 char 是否是大写字母,并且不计算给定字符串中的大写字母
- 如何用非重复字母打印随机字符串
- 将大写字母(从 cin 读取)更改为小写
- 如何验证小写和大写字母
- opencv :无法通过等待键读取大写字母
- 降低字符串字符,并在转换的大写字母前添加一个_
- 接收字符串,检查大写字母
- 如何计算字符串中大写字母、小写字母和数字的数量
- 如何在c++中用大写字母打印地址(十六进制值)
- 为什么c++在使用std::hex而不是大写时打印小写字母?
- 使用"if"打印大写字母