【发布时间】:2020-01-23 02:36:26
【问题描述】:
一个作业要我创建一个函数,将所有小写字符变为大写。
这是我的代码:
main.cpp:
#include <iostream>
#include "Function.h"//Includes function file in main file
using namespace std;
int main(){
char a;
cout<<"Enter some words:";
cin.get(a);//Collects info from user
strcap(a);
cout<<a;
}
function.cpp:
#include <iostream>
#include "Function.h"
using namespace std;
char strcap(char a){
while (a!='\n'){
if (a>='a' && a<='z'){
a-=32;//
}
cin.get(a); //get the next letter
}
}
函数.h:
#include <iostream>
char strcap(char a);
【问题讨论】:
-
您认为
char a;中可以存储多少个字符?请改用std::basic_string。 -
阅读How to Ask。然后告诉我们你认为你的程序有什么问题?你告诉我们你的任务是什么,但你没有描述你的调试工作或给我们任何输入和输出示例。
-
这段代码是完全错误的方法。您需要重新开始并采取不同的方法。查看this
std::toupper()documentation中提供的示例代码 -
使用
std::toupper将小写转换为大写。 -
您正在更改
strcap中的局部变量并将其丢弃,它从未使用过,从未返回。
标签: c++