- run it and enter your first name,
- run it and enter your full name – use space(s)
- modify the program so that the string “name” contains your full name
- read in “function1” a new string that contains your nickname and transfer it back to main;
- read a “function1” a new numeric array that contains the month and year you started B-Tech program and transfer it back to main
- print the three matrices (full name, nickname, month and year) in a separate function
- add “endl” at the end of each “cout” (eliminate “\n” if it exists)
First <iostream> and <stdio.h> are about the same. <iostream is the C++ io header file and <<stdio.h> is a C header file for io. Yoy only need <iostream>.
You should also include the <string> header file and use "std::string" in place of the character array.
Line 22. Is known as formatted input and will extract up to and including the first white space leaving the rest in the input buffer for the next "cin". To get a full name you will either have to prompt for first and last name or use "std::getline()" to extract everything up to the new line.
Your version is known as pass by "value", so the function will need to return the input value. See following code.
The programs are untested for now, but hould work.
// Psaa by value version
#include <iostream>
#include <string>
//using namespace std; // <--- Best not to use
std::string functionl();
int main()
{
std::string name; // <--- Bestnot to use "string"
functionl();
std::cout << "Hello there " << name << "!\n\n";
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
std::string functionl()
{
std::cout << "Enter your first name => ";
std::getline(sdtd::cin, name);
return name;
}
// pass by reference version
#include <iostream>
#include <string>
//using namespace std; // <--- Best not to use
void functionl(std::string& name);
int main()
{
std::string name;
functionl(name);
std::cout << "Hello there " << name << "!\n\n";
return 0;
}
void functionl(std::string& name)
{
std::cout << "Enter your first name => ";
std::getline(sdtd::cin, name);
}
In the first program I added the three lines before the "return" to keep the console window open . This is needed for some people. when the program is working you an either comment out these lines or remove them.
The second version is the pass by reference version. Notice the differences between the two.