C++简单上手helloworld 以及 vscode找不到文件的可能性原因

helloworld

bash 复制代码
#include <iostream>

int main()
{
	std::cout << "hello world!" << std::endl;
	return 0;
}

输入输出小功能

bash 复制代码
#include <iostream>
using namespace std;
/*
*主函数
*输出一条语句
*/

int main()
{
	// 输出一条语句
	cout << "hello world" << endl;
	// 提示用户输入姓名
	cout << "请输入您的姓名:" << endl;  // 中文可能会乱码
	// 用一个变量保存键盘输入的信息
	string name;
	cin >> name;
	cout << "hello," << name << endl;
	// 等待键盘输入
	cin.get(); // 等待敲回车 // 在'cin >> name;'语句中已经敲过回车了
	cin.get();
	return 0;	
}

定义函数

bash 复制代码
#include <iostream>
using namespace std;
/*
*主函数
*输出一条语句
*/
void welcome()
{
	// 提示用户输入姓名
	cout << "请输入您的姓名:" << endl;  // 中文可能会乱码
	// 用一个变量保存键盘输入的信息
	string name;
	cin >> name;
	cout << "hello," << name << endl;
}
int main()
{
	// 输出一条语句
	cout << "hello world" << endl;
	//调用函数
	welcome();
	// 等待键盘输入
	cin.get(); // 等待敲回车 // 在'cin >> name;'语句中已经敲过回车了
	cin.get();
	return 0;	
}

另创建一个.CPP函数文件

此处出现一个问题:我使用的是vscode的C++环境,在编译时会有tasks.json文件,如果代码没问题还出现运行test.cpp文件时显示找不到'welcom'可能是因为只编译了test文件,而不编译welcome文件,需要更改json文件里的参数,如下,注释部分是默认的,更改为"*.cpp",意思是运行文件夹下所有.cpp文件。

bash 复制代码
#include <iostream>
using namespace std;

void welcome()
{
    cout << "please enter your name:" << endl;
    string name;
    cin >> name;
    cout << "hello," << name <<endl;
}
bash 复制代码
#include <iostream>
using namespace std;

void welcome(); // 声明

/*
*主函数
*/
int main()
{
	// 输出一条语句
	cout << "hello world" << endl;
	// 调用函数
	welcome();
	// 等待键盘输入
	cin.get(); // 等待敲回车 // 在'cin >> name;'语句中已经敲过回车了
	cin.get();
	return 0;	
}
相关推荐
沐怡旸3 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4166 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥8 小时前
C++ std::set
c++
侃侃_天下9 小时前
最终的信号类
开发语言·c++·算法
博笙困了9 小时前
AcWing学习——差分
c++·算法
echoarts9 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix10 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
青草地溪水旁10 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁10 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥10 小时前
C++ std::vector
c++