String Conversion Function Problem

Hi, I'm relatively new to C++. I was trying to make a header set of functions for easier access (this may have already been done before but I just decided to take a shot at it.)

I have this so far, but when I try to compile (using http://cpp.sh/) I get this error:

 
error: invalid conversion from 'std::string (*)() {aka std::basic_string<char> (*)()}' to 'char' [-fpermissive]


I am not at home currently so I do not have access to Visual Studio until it finishes installing, so I am using the online shell. I don't know if that has anything to do with this, but I assume it's probably not the shell itself causing the error. But if you don't recognize it, that's probably why. I know kind of why this is happening, but the thing is, I'm not trying to convert it to a char, I'm trying to convert it to a std::string.

Here's the program itself (has a main function for testing):

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Easy C++ functions set (Header)
 
// Standard/required includes (comment out if you are already including them in your main file)
 
#include <iostream>
#include <string>
#include <ctime>
 
 
// printOut(x) - prints x to current line
 
void printOut(std::string x)
{
        std::cout << x
}
 
 
// getLine - gets current line
 
std::string getLine()
{
    std::string x
        cin >> x
        return x
}
 
 
// randomInt(min, max) - generate random number from min to max
 
int randomInt(int min, int max)
{
        int x = rand()%(max-min+1)+min;
        return x;
}
 
// testing function
 
 
int main()
{
    std::string lol;
    int randy;
   
        printOut("test \n");
        lol = getLine;
        printOut(lol);
        randy = randomInt(1, 10);
        printOut(randy);
       
        return 0;
}


Thanks if you can help.
Your function printOut() requires a string as a parameter not an int, randy is an int not a string.
closed account (E0p9LyTq)
There are numerous errors with your code.

1. your getLine() function has no terminating semicolon on all three lines.

1a. cin needs to be std::cin, otherwise cin won't be declared.

2. your randomInt() function won't ever give a true random number, you never seeded the random number generator. Every time you run this program you will get the same number.

3. in main() lol = getLine; is missing the needed () to make getLine an actual function call.

4. as previously mentioned printOut() takes only a string, you need to either overload printOut(), create another printOut() function, to take in int as a parameter or convert randy to a string.
Last edited on
A couple other points:

1) Your getLine() function will not get the whole line but only the first "word".
std::cin >> x; will only read up to the first whitespace on the line, so if you entered "Hello, World!" the variable x would only contain "Hello,", which given the name of your function is not what you want. There are several getline functions in the standard library.
See: http://www.cplusplus.com/reference/string/string/getline/?kw=getline

You could rewrite your getLine() function as follows:

1
2
3
4
5
6
std::string getLine()
{
    std::string x;
    getline( std::cin, x );
    return x;
}


But the string library getline functions are so simple to use that there is no point in creating your own. You can just change line 45, lol = getLine; to getline(std::cin, lol);

2) In your printOut function, since your are not modifying the string parameter in any way, just pass the string by const reference to avoid making an unnecessary copy of the string.

Change:

1
2
3
4
void printOut(std::string x)
{
        std::cout << x
}


to

1
2
3
4
void printOut( const std::string& x)
{
        std::cout << x;
}


Alright thanks, this was kind of just some testing code I wrote while I was bored, and I realize all the mistakes I made :P thanks for the help anyway. I need to familarize myself with the standard library before trying to make stuff that doesn't need to be made ^_^
Topic archived. No new replies allowed.