#include<iostream>
#include<string>
using namespace std;
int main()
{
string fn;
string ln;
string name;
int age;
char grade;
std::cout << "What is your first name: ";
getline(cin, fn); //std::cin >> fn;两者区别在于使用getline可以避免在识别到空格就跳过下阶段的输入
std::cout << "\nWhat is your last name : ";
getline(cin, ln); //std::cin >> ln; 使用getline需要头文件string
name = ln + "," + fn;
std::cout << "\nWhat letter grade do you deserve?";
std::cin >> grade;
grade += 1;
std::cout << "\nWhat is your age?";
std::cin >> age;
std::cout << "Name: " << name << "\nGrade: " << grade << "\nAge: " << age;
return 0;
}
题目要求使用string来获取到交互的喜爱甜点和名字,再输出(类似题1)
代码:
#include<iostream>
#include<string>
int main()
{
using namespace std;
const int ArSize = 20;
string name;
string dessert;
std::cout << "Enter your name:\n";
getline(cin, name);
std::cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
std::cout << "I have some delicious [" << dessert;
std::cout << "] for you , [" << name<< "].\n";
return 0;
}
使用拼接和复制的函数来对字符串数组进行操作。
代码:
#include<iostream>
#include<cstring>
int main()
{
char fn[20];
char ln[20];
std::cout << "Enter your first name: ";
std::cin >> fn;
std::cout << "Enter your last name: ";
std::cin >> ln;
char name[50];
strcpy_s(name, fn);
strcat_s(name, ",");
strcat_s(name, ln);
std::cout << "Here is the information in a single string : " << name;
return 0;
}
改写第三题中使用的char数组操作部分为string操作部分
代码:
#include<iostream>
#include<string>
int main()
{
using namespace std;
string fn;
string ln;
std::cout << "Enter your first name: ";
std::cin >> fn;
std::cout << "Enter your last name: ";
std::cin >> ln;
string name;
name = fn + "," + ln;
std::cout << "Here is the information in a single string : " << name;
return 0;
}
Pycharm无法添加Conda新建的虚拟环境,点击没反应,在idea.log文件中报错:CondaPythonLegacy - Can’t find python path to use, will use conda run instead
1.问题描述🔍 在PyCharm中,依次单击File>Settings>P…