String/Char data types

Oct 24, 2013 at 1:32pm
Hello, I'm trying to write a program that will a overload a function named as the function in the code. So, if the user enters characters like 5 '%' then the program will output 5 other character like '*'. But I've got an error.

If the user will type 10
**********

It will also return a 10 different character

none of the 2 overloads could convert all the argument types
How should I fix this. Also, any help with my code and see what's wrong with it is greatly appreciated

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
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

void linechar(char);
int linechar (int);

int main()
{
	char str[100];
	cout << "Please enter characters: ";
	cin.getline(str, 100);
	linechar(str); 
}

int linechar(char x[100])
{
	int length=strlen(x);
	int k=linechar(length);
	return k;
}
int linechar (int x)
{
	for (int i=1; i<=x; i++)
	{
		cout << "@";
	}
}
Last edited on Oct 25, 2013 at 9:17am
Oct 24, 2013 at 1:36pm
Your function prototype (for char) and definition do not match.
Oct 25, 2013 at 8:58am
I'm still having the same problem

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
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

char linechar(char);
int linechar (int);

int main()
{
	char str[100];
	cout << "Please enter characters: ";
	cin.getline(str, 100);
	linechar(str); 
}

char linechar(char x[100])
{
	int length=strlen(x);
	int k=linechar(length);
	return k;
}
int linechar (int x)
{
	for (int i=1; i<=x; i++)
	{
		cout << "@";
	}
}
Oct 25, 2013 at 9:29am
The prototype still does not match. Try this.

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
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

int linechar(char []);
int linechar (int);

int main()
{
	char str[100];
	cout << "Please enter characters: ";
	cin.getline(str, 100);
	linechar(str); 
}

int linechar(char x[])
{
	int length=strlen(x);
	int k=linechar(length);
	return k;
}
int linechar (int x)
{
	for (int i=1; i<=x; i++)
	{
		cout << "@";
	}
}

Oct 25, 2013 at 10:42am
Thank you!! Last question though, can i use srand() to generate random characters because I would to replace cout << "@"; with that. Or is there another function for it. I can't find anything on the internet.
Last edited on Oct 25, 2013 at 10:42am
Oct 25, 2013 at 10:56am
You will need to seed the random generator through srand() once. It usually done through current time.

srand( time( NULL ) );

After that you can call the rand() function (any number of times) that will generate a random number between 0 and RAND_MAX.

Note: The above is the C way of doing it.

The C++ way is to use the <random> header. You can go through its documentation on this website. Just type in random on the search bar on top and it will get you the docs.
Last edited on Oct 25, 2013 at 10:57am
Topic archived. No new replies allowed.