Strings

- 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)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <stdio.h>

using namespace std;

void functionl(char name[]);

int main()
{
	char string[20];

	functionl(string);
	cout << "Hello there " << string << "!\n\n";

	return 0;
}

void functionl(char name[])
{
	cout << "Enter your first name => ";
	cin >> name;
}




Appreciate your help...
Hello procppgram,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//  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.

Hope that helps,

Andy
Topic archived. No new replies allowed.