Find char in string

I am starting to learn c++ by recreating my FreePascal code in C++

In the scenario below I am trying to cout the 3rd char in a given string, however the only thing returning is the number 112. am I doing something wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <iostream>

using namespace std;

int dastrings (string line)
{

	return (line[2]);

}



int main ()
{
	
	cout << dastrings("poptarts \n") << endl;
	return 0;
	
}
closed account (zb0S216C)
korbel wrote:
"however the only thing returning is the number 112. am I doing something wrong?"

That's to be expected. Each character in C++ is secretly an integer (represented in decimal format) that ranges from 0:127. When you pass a character to an int, the compiler assigns the character's integral equivalent to the int. C++ follows the ASCII character table, which can be found here: http://www.asciitable.com/

To fix this, swap int with char in dastrings() definition.

Wazzak
Last edited on
The return type for the dastrings function should be char. At the moment it is returning an integer, and this is interpreted as a numerical value by the << operator. The function should return a char, which will be interpreted as an ascii character, and should print correctly.
@Framework

Yup that did the trick, and I just learned something new so golf claps all around.
A bit different from the function / procedure stuff I am used to. Thanks again!
So I ran into a new problem, and was hoping you could help me out again. I am getting another error while working this w/ strings, was wondering what I am doing wrong.

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
#include <string.h>
#include <iostream>
#include <stdio.h>

using namespace std;

char dastrings (string line, int lp)
{
	int i;
	char s;
	for ( i = 0; i <= lp; i++)
	{
		s= line[i];
	}
return ("placeholder");
}



int main ()
{
	string tmp = "poptarts";
	cout << dastrings(tmp, strlen(tmp) << endl;
	return 0;
	
}
closed account (zb0S216C)
A char can only store a single character. To return a string, you have two options:

1) Return a pointer to a constant char: const char *

1
2
3
4
const char *ReturnAString() 
{
    return("A String");
}

2) Return a std::string

1
2
3
4
5
6
7
8
9
10
std::string ReturnAString()
{
    return("A String");
}

int main()
{
    std::string ReturnedString(ReturnAString());
    std::cout << "Size of the string: " << ReturnedString.length() << '\n';
}

I should point out that both "string.h" and "stdio.h" are C headers. "string.h" should be "string" and "stdio.h" should be "cstdio". Note the absence of file extensions. This is because C++ headers do not have extensions.

Wazzak
Last edited on
Your code contains a bug.

Instead of

for ( i = 0; i <= lp; i++)

there should be

for ( i = 0; i < lp; i++)

in your function dastrings.

Otherwise when you call it the way dastrings(tmp, strlen(tmp) you are trying to access memory outside your string.

Last edited on
you are correct, however I didn't even get to that point since I am getting the following error when compiling

error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
This was already explained by Framework. String liiteral has type const char[] which can not be converted to char

You could write for example

return ("placeholder"[0] );

However I do not see any sense in your return value.
My apologize, I missed his reply, thank you.
what's your code?
Thank you again Framework, it compiled and worked
So now I know that you cannot mix char and string, which is just silly, but if I just stick to strings then I think my conversion will go smoother. Good thing almost everyone has like 2 gigs of ram these days.

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
#include <string.h>
#include <iostream>
#include <stdio.h>

using namespace std;

string dastrings (string line, int lp)
{
	int i;
	string s;
	bool foundone = false;

	for ( i = 0; i < lp; i++)
	{
		s = line[i];
		if (s == "@") { foundone = true; }
	}
	
	if (foundone == true)
	{ return ("ready"); }
	else { return ("end"); }
}



int main ()
{
	string tmp = "poptarts";
	cout << dastrings(tmp, tmp.length()) << endl;

	return 0;
	
}
 
Last edited on
You are right, I started reading "how to convert char to string" and saw there is a process for doing that. Coming from Delphi and Freepascal I can tell that c++ is much more finicky, but there is way more code examples, documentation and great people willing to help so it really is not all bad. I have dated some rigid women with very specific demands and this is a lot like that. :-)
hmm... talking about women... :)
Topic archived. No new replies allowed.