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;	
}
相关推荐
Want5953 分钟前
C/C++跳动的爱心
c语言·开发语言·c++
水瓶丫头站住4 分钟前
Qt中QDockWidget的使用方式
开发语言·qt
laimaxgg9 分钟前
Qt常用控件之数字显示控件QLCDNumber
开发语言·c++·qt·qt5·qt6.3
蓝天扶光13 分钟前
c++贪心系列
开发语言·c++
Alidme41 分钟前
cs106x-lecture14(Autumn 2017)-SPL实现
c++·学习·算法·codestepbystep·cs106x
奔跑吧邓邓子41 分钟前
【Python爬虫(44)】分布式爬虫:筑牢安全防线,守护数据之旅
开发语言·分布式·爬虫·python·安全
小王努力学编程42 分钟前
【算法与数据结构】单调队列
数据结构·c++·学习·算法·leetcode
C#Thread1 小时前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
牵牛老人2 小时前
Qt开发中出现中文乱码问题深度解析与解决方案
开发语言·qt
大脑经常闹风暴@小猿2 小时前
1.1 go环境搭建及基本使用
开发语言·后端·golang