Find char in string

May 10, 2012 at 11:13pm
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;
	
}
May 10, 2012 at 11:18pm
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 May 10, 2012 at 11:19pm
May 10, 2012 at 11:19pm
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.
May 10, 2012 at 11:27pm
@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!
May 11, 2012 at 4:40am
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;
	
}
May 11, 2012 at 8:10am
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 May 11, 2012 at 8:12am
May 11, 2012 at 12:33pm
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 May 11, 2012 at 12:33pm
May 11, 2012 at 2:10pm
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]
May 11, 2012 at 2:15pm
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.
May 11, 2012 at 2:29pm
My apologize, I missed his reply, thank you.
May 11, 2012 at 2:37pm
what's your code?
May 11, 2012 at 2:47pm
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;
	
}
May 11, 2012 at 3:07pm
 
Last edited on May 13, 2012 at 9:57am
May 11, 2012 at 3:11pm
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. :-)
May 11, 2012 at 4:16pm
hmm... talking about women... :)
Topic archived. No new replies allowed.