如何使用void函数

时间:2017-04-15 01:31:26

标签: c++ void

我可以轻松地创建一个void函数来输出类似标题的东西,但我无法获得一个程序来读取用户的输入然后将其输出到它们。

即。让程序说“请输入你的名字”,然后输出“你输入:(他们放的是什么)”

我所拥有的一个例子:

#include<iostream>
#include<conio.h>
#include<iomanip>
#include<fstream>

using namespace std;

void namefn()//namefn prototype

int main(){
    string firstName,lastName;
    return 0;
}

void namefn(string firstName, string lastName){
    cout<<"please enter your first and last name "<<endl;
}

3 个答案:

答案 0 :(得分:0)

使用cin阅读,位于iostream

cin >> firstName;

答案 1 :(得分:0)

cout << "enter your first and last name:";
cin >> firstName >> lastName;
cout << firstName << " " << lastName;

如果您希望程序分别显示名字和姓氏,请执行以下操作:

cout << "enter you first name:";
cin >> firstName;
cout << firstName << endl;
cout << "Enter your last name:";
cin >> lastName;
cout << lastName << endl;

答案 2 :(得分:0)

试试这个:

mapAdd n lst = map (+ n) lst

我想,你想要这样的东西。

或者像“标题”

#include <iostream>
#include <string>

void displayFirstAndLast(const std::string& firstName, const std::string& lastName)
{
     std::cout << "You are " << firstName << " " << lastName << std::endl;
}


int main(){
     std::string firstName;
     std::string lastName;
     std::cout << "Please enter your first and last name " << std::endl;
     std::cin >> firstName >> lastName;
     displayFirstAndLast(firstName, lastName);
     return 0;
}