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;	
}
相关推荐
nbsaas-boot1 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
风无雨2 小时前
GO 启动 简单服务
开发语言·后端·golang
斯普信专业组2 小时前
Go语言包管理完全指南:从基础到最佳实践
开发语言·后端·golang
我是苏苏3 小时前
C#基础:Winform桌面开发中窗体之间的数据传递
开发语言·c#
斐波娜娜4 小时前
Maven详解
java·开发语言·maven
小码氓4 小时前
Java填充Word模板
java·开发语言·spring·word
暮鹤筠4 小时前
[C语言初阶]操作符
c语言·开发语言
蜉蝣之翼❉6 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc