How to read first and last name with void function - c++

I can easily create a void function to output something like a header, but I cannot get a program to read a user's input and then output it to them.
ie. Have a program say "Please enter your first name" and then output "You entered: (what they put)"
An example of what I have:
#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;
}

Use cin to read, is in iostream.
i.e.
cin >> firstName;

cout << "enter your first and last name:";
cin >> firstName >> lastName;
cout << firstName << " " << lastName;
if you want the program to display the first name and last name separately then this:
cout << "enter you first name:";
cin >> firstName;
cout << firstName << endl;
cout << "Enter your last name:";
cin >> lastName;
cout << lastName << endl;

Try this:
#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;
}
I think, that you want something like this.
Or with something like "header"
#include <iostream>
#include <string>
void displayFirstAndLast(const std::string& firstName, const std::string& lastName);
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;
}
void displayFirstAndLast(const std::string& firstName, const std::string& lastName)
{
std::cout << "You are " << firstName << " " << lastName << std::endl;
}

Related

Using cin.ignore AFTER cin.get to ignore extra inputs

The user is prompted to "enter a middle initial". What happens if they enter a space, full name, or maybe a letter followed by a period '.' ?
How can we modify the program to handle this using cin.ignore?
This is the code I currently have:
I commented out the area I'm having trouble with.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string fname, lname;
char MI;
cout << "Please enter your first name: ";
cin >> fname;
cout << "Please enter your middle initial: ";
cin.ignore(1, '\n');
cin.get(MI);
cout << "Please enter your last name: ";
//cin.ignore('\n')
cin >> lname;
cout << "Your name is " << fname << " " << MI << " " << lname << endl;
return 0;
}
When I have this other cin.ignore in it still doesn't do anything and the last name reads the extra inputs. I've tried adding a number of characters to read and it still doesn't fix the problem. When I run it it just skips the input for last name. I also tried changing the last name input to getline but if still didn't do anything.
You can just use std::getline and std::istringstream:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string fname, lname;
std::string MI;
std::cout << "Please enter your first name: ";
std::getline(std::cin, fname);
std::istringstream iss(fname);
iss >> fname;
do
{
std::cout << "Please enter your middle initial: ";
std::getline(std::cin, MI);
} while (MI.size() != 1);
std::cout << "Please enter your last name: ";
std::cin >> lname;
std::cout << "Your name is " << fname << " " << MI << " " << lname << std::endl;
return 0;
}
Here for fname I have used std::getline to get user input and then I've used std::istringstream to get only one word of the input.
For MI I have made it a string and until and unless the user doesn't provide a single character, the program doesn't continue.
And the lname part is the same.
You should change:
cin.ignore(1, '\n');
cin.get(MI);
To simply:
cin >> MI;
Let operator>> ignore any white space, including line breaks, between the first name and the middle initial.
After reading MI, you can then use the following to ignore everything up to the next input:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Try this:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main ()
{
string fname, lname;
char MI;
cout << "Please enter your first name: ";
cin >> fname;
cout << "Please enter your middle initial: ";
cin >> MI;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter your last name: ";
cin >> lname;
cout << "Your name is " << fname << " " << MI << " " << lname << endl;
return 0;
}

Can Someone Explain Why My Getter Function Doesn't Call?

I'm writing some code to create a student class and a menu that allows the user to input and display information. Im running into some issues getting my GetName function to call. Here is my code:
#include <iostream>
using namespace std;
class Student {
string FirstName, LastName, Birthday;
int MNumber, Age;
float GPA;
public:
void GetName(){
cout << "Please enter your First Name." << endl;
cin >> FirstName;
cout << "Please enter your Last Name." << endl;
cin >> LastName;
cout << FirstName << ", " << LastName << endl;
}
};
int main () {
Student GetName;
return 0;
}
}
You're not calling the "Getname" method inside main program.
#include <iostream>
using namespace std;
class Student {
string FirstName, LastName, Birthday;
int MNumber, Age;
float GPA;
public:
void GetName(){
cout << "Please enter your First Name." << endl;
cin >> FirstName;
cout << "Please enter your Last Name." << endl;
cin >> LastName;
cout << FirstName << ", " << LastName << endl;
}
};
int main () {
Student getName;
getName.GetName();
return 0;
}

Why is fullName not displaying name after space

The final cout is not displaying the cin fullName:
int main()
{
string fullName;
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName; //this final cout is not displaying the cin fullName
system("pause>0");
}
cin does not work with spaces in the input, the rest of your input is in the buffer.
Try using getline like this:
int main()
{
string fullName;
cout << "Type your full name:" ;
getline(cin,fullName);
cout << "Your name is: " << fullName;
system("pause>0");
}
This way it will save into your variable everything until it hits a \n.

How can I access elements of a vecotor in C++

Actually, I am rookie, and I started to learning this language a few months age. Now, I confronted with an insoluble issue. I devised a program to get information of numerous employee's specification and store it in a vector. In order to acquire better performance, I designed a class named "Employee", and defined a function to give some options to user, like adding new employee, promotion a specific employee's salary and so on.
I defined my matching criterion through operators, but there is an error in program, and that is when I want to display, the output is completely weird. it looks like a some kind of Chinese letter :-)
here is my code:
Thanks
My major problem is when it comes displaying employee's specification
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
using namespace std;
class Employee
{
public:
Employee(string FirstName, string LastName, int age , double Salary, bool Status ) ://constructor
fName(FirstName), lName(LastName), EmpAge(age), EmpSalary(Salary), RecruitmentStatus(Status) {};
void setSalary(const double&);
void SetRecruitmentStatus(const bool&);
void Promote(const double&);
//====================================//
operator const char* ()
{
ostringstream formattedOutput;
formattedOutput << this->fName.c_str() << " " << (this->lName.c_str()) << " | " << this->EmpAge << " | " << this->EmpSalary << ((RecruitmentStatus) ? "Working" : "Fired");
string output = formattedOutput.str();
return output.c_str();
}
bool operator == (const Employee& anotherEmployee)const
{
return (this->lName == anotherEmployee.lName);
}
bool operator < (const Employee& anotherEmployee)const
{
return (this->EmpSalary < anotherEmployee.EmpSalary);
}
private:
string fName, lName;
int EmpAge;
double EmpSalary;
bool RecruitmentStatus;
};
void Employee::setSalary(const double& income)
{
this->EmpSalary += income;
}
void Employee::SetRecruitmentStatus(const bool& Status)
{
this->RecruitmentStatus = Status;
}
void Employee::Promote(const double& Promotion)
{
this->EmpSalary += Promotion;
}
template <typename T>
void displayElements(T& input)
{
for (auto iterator = input.begin();
iterator != input.end(); iterator++)
{
cout << *iterator << endl;
}
}
void selection(int&);
Employee getNewEmp(void);
int main()
{
vector<Employee> Records;
int ID;
do
{
selection(ID);
switch (ID)
{
case 1:
{
Records.push_back(getNewEmp());
break;
}
case 2:
{
cout << "please enter last name of the employee you want to change his/her recruitment'status: ";
string lastName; cin >> lastName; cout << endl;
auto memberLocation = find(Records.begin(), Records.end(), lastName.c_str());
break;
}
case 3:
{
cout << "please enter last name of the employee you want to promote his/her recruitment'salary: ";
string lastName; cin >> lastName; cout << endl;
auto memberLocation = find(Records.begin(), Records.end(), lastName.c_str());
break;
}
default:
{
if (ID !=4)
{
cout << "Wrong selection!\nThe program will be closed after a few seconds\n";
exit(1);
}
}
}
} while (ID != 4);
displayElements(Records);
cout << "Thanks for using this program\n";
return 0;
}
void selection(int& input)
{
system("cls");
cout << "please Choose one of these option:\n"
"1. add a new employee\n"
"2. changing an employee status\n"
"3. Promote an employee's salary\n"
"4. exit : ";
cin >> input;
}
Employee getNewEmp(void)
{
system("cls");
string firstName, lastName;
cout << "first name:"; cin >> firstName; cout << endl;
cout << "last name:"; cin >> lastName; cout << endl;
int age;
cout << "Age: "; cin >> age; cout << endl;
double salary;
cout << "Offered Salary: "; cin >> salary; cout << endl;
bool Status;
cout << "Is he/she working(1) or got fired(0) : "; cin >> Status; cout << endl;
return Employee(firstName, lastName, age, salary, Status);
}

Functions declared in header file gives not in scope error

I am working on a project which has no globally defined variables, or class objects or structs in it. I have a bunch of variables defined in the main of Voter.cpp and have different databse functions defined in VoterDB.cpp with the declarations made in VoterDB.h
VoterDB.h ---> VoterDB.cpp
|
|
|
Voter.cpp
Voter.cpp
#include <iostream>
#include <stdlib.h>
#include "VoterDB.h"
using namespace std;
class Voter {
public:
private:
};
int main(int argc, char *argv[])
{
string lastname="";
string firstname="";
int age=0;
int stnum=0;
string street="";
string town="";
string zip="";
float amtdonated=0;
cout << ">: ";
string command;
getline (cin, command);
while(command != "Quit"){
if(command=="New"){
cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
} else if(command=="Update"){
cmd_update(lastname, firstname, age, stnum,street,town,zip);
} else if(command=="View"){
cmd_view(lastname,firstname,age,stnum,street,town,zip,amtdonated)
} else if(command=="Donate"){
} else if(command=="Report"){
}
}
}
VoterDB.cpp
#
include < iostream > #include < stdlib.h > #include < string > #include "VoterDB.h"
using namespace std;
void cmd_new(string & lastname, string & firstname, int & age, int & stnum, string & streetname, string & town, string & zip, float & amtdonated) {
cout << "\nEnter First Name\n";
cout << ">: ";
getline(cin, firstname);
cout << "\nEnter Last Name\n";
cout << ">: ";
getline(cin, lastname);
cout << "\nEnter Age\n";
cout << ">: ";
stringstream(cin, age);
cout << "\nEnter Street Number\n";
cout << ">: ";
stringstream(cin, stnum);
cout << "\nEnter Street Name\n";
cout << ">: ";
getline(cin, streetname);
cout << "\nEnter Town\n";
cout << ">: ";
getline(cin, town);
cout << "\nEnter Zipcode\n";
cout << ">: ";
getline(cin, zip);
amtdonated = 0.0;
return;
}
void cmd_update(string & lastname, string & firstname, int & age, int & stnum, string & streetname, string & town, string & zip) {
string input = "";
cout << "\nEnter Y or N\n";
cout << "Change First Name? (" << firstname << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, firstname);
input = "";
}
cout << "\nChange Last Name? (" << lastname << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, lastname);
input = "";
}
cout << "\nChange Age? (" << age << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
stringstream(cin, age);
input = "";
}
cout << "\nChange Street Number? (" << stnum << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
stringstream(cin, stnum);
input = "";
}
cout << "\nChange Street Name? (" << streetname << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, streetname);
input = "";
}
cout << "\nChange Town? (" << town << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, town);
input = "";
}
cout << "\nChange Zipcode? (" << zip << ")\n";
cout << ">: ";
if (getline(cin, input) == "Y" || getline(cin, input) == "y") {
cout << "\n>: ";
getline(cin, zip);
input = "";
}
return;
}
void cmd_view(string & lastname, string & firstname, int & age, int & stnum, string & streetname, string & town, string & zip, float & amtdonated) {
cout << firstname << " " << lastname << ", " << age << "\n"
cout << stnum << " " << streetname << "\n"
cout << town << " " << zip << "\n"
cout << "Amount Donated: $" << amtdonated;
return;
}
VoterDB.h
#ifndef VOTERDB_H
# define VOTERDB_H
# include < iostream > #include < stdlib.h > #include < string >
using namespace std;
class VoterDB {
public:
static void cmd_new(string lastname, string firstname, int age, int stnum, string streetname, string town, string zip, float amtdonated);
static void cmd_update(string lastname, string firstname, int age, int stnum, string streetname, string town, string zip);
static void cmd_view(string lastname, string firstname, int age, int stnum, string streetname, string town, string zip, float amtdonated);
private:
};
#endif
makefile
##
Specifiy the target
all: Voter
# Specify the object files that the target depends on# Also specify the object files needed to create the executable
Voter: Voter.o VoterDB.o
g++Voter.o VoterDB.o - o Voter.exe
# Specify how the object files should be created from source files
VoterDB.o: VoterDB.cpp
g++ - c VoterDB.cpp
Voter.o: Voter.cpp
g++ - c Voter.cpp
# Specify the object files and executables that are generated# and need to be removed to re - compile the whole thing
clean:
rm - f * .o Voter.exe
My issue is that whenever I attempt to compile this i get the error
$ make
g++ -c Voter.cpp
Voter.cpp: In function ‘int main(int, char**)’:
Voter.cpp:28:66: error: ‘cmd_new’ was not declared in this scope
cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
^
Voter.cpp:30:61: error: ‘cmd_update’ was not declared in this scope
cmd_update(lastname, firstname, age, stnum,street,town,zip);
^
Voter.cpp:32:67: error: ‘cmd_view’ was not declared in this scope
cmd_view(lastname,firstname,age,stnum,street,town,zip,amtdonated)
^
makefile:15: recipe for target 'Voter.o' failed
make: *** [Voter.o] Error 1
I have tried different combinations of declaring the different cmd_* functions as static, and as VoterDB::cmd_* but none of these have fixed the issue so far.
edit: i changed the function calls to VoterDB::cmd_* and switched the functions from static void to void. but I now get an error message
Voter.cpp:32:76: error: cannot call member function ‘void VoterDB::cmd_view(std::__cxx11::string, std::__cxx11::string, int, int, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string, float)’ without object
VoterDB::cmd_view(lastname,firstname,age,stnum,street,town,zip,amtdonated)
This answer should be deleted as a typo, as NathanOliver said. I'm writting it just to clarify you because you requested it.
To call a static member function of a class you must provide the name of the class.
So, in main(), instead of
cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);
use
VoterDB::cmd_new(lastname,firstname,age,stnum,street,town,zip,amtdonated);