Functions Question

Hello,
I want to make a function which will cin a string will count the characters and will give the word and the number of the characters back to main(). i wrote the following code but i don't know what type to give to my function.

type getword(word,x){

string word;
cin>>word;
x=word.size();
return word,x;
}

that's close to what i want to write

main.cpp:8: error: cannot convert 'std::string' to 'char' in return

getting this error as well



Your best bet would probably be to pass a string by reference, and return and int. The function would look something like this
1
2
3
4
5
int getWord(string& w)
{
    cin>>w;
    return w.size();
}
So there isn't a way to make a function which would return an integer and a array or char or string
i wrote new code like you said :
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>
using namespace std;

int wordsize (string word,int size){
	
	size=word.size();
	return size;
	
}



int main (int argc, char * const argv[]) {
	int size;
	string word;
	cin >> word;
	wordsize(word, size);
	
    return 0;
}


but i get an error :
run
[Switching to process 8970]
Running…
hello
Hangman(8970) malloc: *** error for object 0x100003140: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb)
That is not the function I gave you.

Here is an example of how it would work.
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>

using namespace std;

int getWord(string& w)
{
    cin>>w;
    return w.size();
}

int main()
{
    int wordSize;
    string word;
    cout<<"Enter a word, and I will tell you the size.\n";
    wordSize=getWord(word);
    cout<<"The word was "<<word<<", and the size is "<<wordSize<<".\n";
    return 0;
}


To understand how passing by reference works check out this tutorial. It is discussed in the section "Functions(II)"

http://www.cplusplus.com/doc/tutorial/
Thanks but i was looking for a function which would ask for the word and would return in the main program the word and the size of the word.
Also i tried to run your code but i get the same error as mine it runs without any errors but when i give a word something goes bad and it writes
hello
Hangman(9212) malloc: *** error for object 0x100004200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb)
Last edited on
If you want to return several values, put them in a struct.
It seems that somethings wrong with my Xcode i get always the same error

#include <iostream>
#include <string>

int main(){

using namespace std;

string a;
cin>>a;

cout << a;

return 0;

}

even this doesnt work i get some weird errors

0x00007fff8534e87c <+0000> mov $0x2000025,%eax
0x00007fff8534e881 <+0005> mov %rcx,%r10
0x00007fff8534e884 <+0008> syscall
0x00007fff8534e886 <+0010> jae 0x7fff8534e88d <__kill+17>
0x00007fff8534e888 <+0012> jmpq 0x7fff853eefc8 <cerror>
0x00007fff8534e88d <+0017> retq
i figured out that this problem is only when i use a string anyone got a clue why is this happening ?
hey,in which project and file u do these programs?
Well i uninstalled all the developers tools and downloaded them from Apple and problem got fixed just like that. By the way the tools i installed from apple were 9 GB :o .

I got a question about the program i want to write.
i want to make a function which will get a char array and will populate it with '-' for all the values.

i wrote this but it doesnt work


char Setsolution(array[p])
{
for (int i=0; i<=p; i++) {
array[i]='-';
}

}



from my main program i call the array
Setsolution(solution[wordsize]);

Can someone please tell me how i must construct this function ?
thanks
1
2
char Setsolution(char array[], int n);
Setsolution( solution, wordsize );
Topic archived. No new replies allowed.