Jan 30, 2010 at 6:14am UTC
How can I create a separate function to add a character (prompt user to enter a character) to my ElementA & display the result?
I will have to make use of cstring function...
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 52 53 54 55
#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX = 10;
void displaySet1 (char *, int );
int main ()
{
char x[MAX];
char * a = x;
srand (time(NULL));
cout << "Element A: " ;
displaySet1 (a, 10);
cout << endl;
cout << "Enter a ASCILL letter: " << endl;
cout << "Final Array: " ;
}
//Construct Element A
void displaySet1 (char * a, int size)
{
size = rand () % 9 + 2;
cout << "{" ;
for (int i = 0; i < size; i++)
{
a[i] = static_cast <char > (rand () % 26 + 70);
cout << a [i];
if (i < size - 1)
cout << "," ;
}
cout << "}" ;
}
Last edited on Jan 30, 2010 at 6:20am UTC
Jan 30, 2010 at 9:47am UTC
To insert a char into a cstring you have to create a new cstring:
1 2 3 4 5
char input[10];
char output[11];
memcpy(output, input, 3);
output[3] = 'X' ;
memcpy(output + 4, input + 3, 7);
Last edited on Jan 30, 2010 at 9:48am UTC
Jan 30, 2010 at 6:11pm UTC
What does memcpy, input, output does?