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;	
}
相关推荐
tankeven8 分钟前
HJ182 画展布置
c++·算法
W230357657320 分钟前
【改进版】C++ 固定线程池实现:基于调用者运行的拒绝策略优化
开发语言·c++·线程池
星辰_mya1 小时前
PV之系统与并发的核心wu器
java·开发语言·后端·学习·面试·架构师
谭欣辰1 小时前
C++ 控制台跑酷小游戏
c++·游戏
做时间的朋友。1 小时前
Java虚拟线程详解:从原理到实战,解锁百万并发新姿势
java·开发语言
一只大袋鼠1 小时前
MyBatis 从入门到实战(二):代理 Dao 开发与多表关联查询
java·开发语言·数据库·mysql·mybatis
明月醉窗台1 小时前
Python-opencv批量处理文件夹中图像操作
开发语言·python·opencv
周末也要写八哥1 小时前
C++实际开发之泛型编程(模版编程)
java·开发语言·c++
好家伙VCC1 小时前
**发散创新:用 Rust实现游戏日引擎核心模块——从事件驱动到多线程调度的实战
java·开发语言·python·游戏·rust
Dxy12393102162 小时前
Python在图片上画圆形:从入门到实战
开发语言·python