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;	
}
相关推荐
六bring个六3 分钟前
文件系统交互实现
开发语言·c++·qt·交互
jingyu飞鸟8 分钟前
Centos7系统(最小化安装)安装zabbix7版本详细文章、nginx源代码配置、php源代码、mysql-yum安装
开发语言·php
dhxhsgrx18 分钟前
PYTHON训练营DAY27
开发语言·python
小山菌18 分钟前
mac中加载C++动态库文件
开发语言·c++·macos
疯狂学习GIS25 分钟前
Windows配置VS Code详细流程
c++·学术工作效率
__BMGT()26 分钟前
C++ QT图片查看器
前端·c++·qt
关于不上作者榜就原神启动那件事38 分钟前
Java基础学习
java·开发语言·学习
Echo``42 分钟前
1:OpenCV—图像基础
c++·图像处理·人工智能·opencv·算法·计算机视觉·视觉检测
橙子1991101643 分钟前
在 Kotlin 中,什么是解构,如何使用?
android·开发语言·kotlin
Q_Q19632884751 小时前
python的家教课程管理系统
开发语言·spring boot·python·django·flask·node.js·php