string exercise

Sep 20, 2013 at 2:51am
I have written a small program to try to use string input. I have some errors:
lines 6, 18, and 23.

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
// Exercise in string manipulation

#include <iostream>
#include <string>

void prtname(str);

using namespace std;

int main()
{
	string input;

	cout << "Please enter the name of a bird with long legs: ";

	cin >> input;

	prtname(input);

	return 0;
}

void prtname(str)
{
	cout << "The birds name is: " << str;
}
Sep 20, 2013 at 3:03am
void prtname(str);

Do you know how to declare/define a function? You should go look it up.
Sep 20, 2013 at 3:32am
The only thing I can see is str is untyped? This is the beginner's forum, isn't it?
Sep 20, 2013 at 3:57am
Yes, because "str" is not given a type, the program doesn't know what type to give it. This is probably what you wanted to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

void prtname(std::string str);

int main() {
    std::string input;

    std::cout << "Please enter the name of a bird with long legs: ";
    std::cin >> input;

    prtname(input;

    return 0;
}

void prtname(std::string str) {
    std::cout << "The bird's name is: " << str;
}


Just in case you didn't know, std::string, std::cout etc. are the same as normal, it is just what you type if you don't use the std namespace. In general, its better to avoid using namespace std;, because it leads to some errors that may not be obvious, such as if you declare a global variable called "left" or something.
Sep 20, 2013 at 4:24am
Everything worked. Thanks for giving me some direction an some advice. I'll remember it.
Sep 20, 2013 at 6:25am
I spoke to soon. This line: cout << "The birds name is: " << str; only displays the first word in the string.
This line: cout << "The lenght of the name is: " << str.length();
only displays the length of the first word.
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

// Exercise in string manipulation

#include <iostream>
#include <string>

void prtname(std::string str);

using namespace std;

int main()
{
	string input;

	cout << "Please enter the name of a bird with long legs: ";

	cin >> input;

	prtname(input);

	return 0;
}

void prtname(std::string str)
{
	cout << "The birds name is: " << str;
	cout << "The lenght of the name is: " << str.length();
}


Sep 20, 2013 at 6:32am
Ah, thats because "cin" only takes in up to the first space, tab or linefeed. If you want to get everything up to the new line, use getline, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

void prtname(std::string str);

int main() {
	std::string input;

	std::cout << "Please enter the name of a bird with long legs: ";
	std::getline(std::cin, input);

	prtname(input);

	return 0;
}

void prtname(std::string str)
{
	std::cout << "The birds name is: " << str << std::endl;
	std::cout << "The lenght of the name is: " << str.length() << std::endl;
}


That should work.

[EDIT] Fixed formatting errors
Last edited on Sep 20, 2013 at 6:34am
Sep 20, 2013 at 11:34am
Yes, that works fine. I have been using arrays, pointers, and etc (c style) to manipulate strings. It looks like using strings are a little easier.
Thanks

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

// Exercise in string manipulation

#include <iostream>
#include <string>

void prtname(std::string str);

using namespace std;

int main()
{
	std::string input;

	std::cout << "Please enter the name of a bird with long legs: ";

	std::getline(std::cin, input);

	prtname(input);

	return 0;
}

void prtname(std::string str)
{
	std::cout << "The birds name is: " << str << std::endl;
	std::cout << "The lenght of the name is: " << str.length() << std::endl;
}
Topic archived. No new replies allowed.