Error assigning value to char

So I can't figure out how to assign a value to a char. What I have below won't compile but if someone can suggest a better solution i would appreciate it.

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;
	
}
I'm not really sure what output you want in the end (line 23), but your function dastrings is of type char, which means it should return a char (ie one character), but you try to return an entire string.

Your argument strlen(tmp) should be tmp.length().


Here is your code formatted, but all it does it output "placeholder"

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 <string.h>

using namespace std;

string dastrings (string line, int lp)
{
	int i;
	char s;
	for ( i = 0; i <= lp; i++)
	{
		s= line[i]; //this adds the characters of tmp to char s, but on each iteration it overwrites the previous value.
	}
return ("placeholder"); //function returns this value, regardless of what code your function executes
}



int main ()
{
	string tmp = "poptarts";
	cout << dastrings(tmp, tmp.length()) << endl;
	return 0;
}
Right, I should be more specific, I have some sudo code and am trying to convert from pascal to c++, the return is not needed at this point but will be needed later, so I just made a place holder. what I want to do is be able to pick a specific number out of a given string and return that char, everything you see is the beginning of that but just not usable.

What I am having issue understanding is why it wont compile saying that "unable to convert char to char[1]"
Is that the exact error you are getting for the original code?
 
Last edited on
Awesome! thank you,
Topic archived. No new replies allowed.