How do you input size into a Class 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
class Class1{
private:
int size;
int x1[40];

public:
void sayit(int size);
void sayit(int size);
void doit();
void writeit(); 
}; // cs1

void Class1::sayit(int size){
cout <<"\n\n\t Random Numbers\n\n";
}//sayit

void Class1::sayit(int size){
cout <<"\n\n\t Random Numbers\n\n";
}//sayit

void Class1::doit(){
for(int i=0; i<size; i++) x1[i] = random(999); 
}//doit

void Class1::writeit(){
cout << "\n\n\t Random Numbers...\n";
for(int i=0; i<size; i++) cout << "\t" << x1[i]; 
}//writeit

int main(){
clrscr();
randomize();
Class1 c1,c2;
c1.sayit(20); 
c1.doit();
c1.writeit();
getch();
c2.sayit(40); <----- Number of random numbers to be this.
c2.doit();
c2.writeit();

I'm trying to get the random numbers to output different sizes. I'm not really sure how to do it so if you could look at it and see what I'm doing wrong. I know in a regular function you would just put the number in there, so any help is great.
I get the feeling that you are using code tags wrong because you have code in the output panel. http://www.cplusplus.com/articles/firedraco1/ (But at least you used them, although that code should still be indented.)
Anyway, I don't see how you can double-define sayit (int). That really shouldn't work. In addition, there's a lot of stuff you have calls to that you don't show us. What are random() and randomize()?
And lastly, I really don't understand what your objective is. What is it you have in mind?
Last edited on
sorry, i tried fixing the code, for some reason they keep showing on the output.

randomize is for making the numbers random every time i run the program. random(999) is to get at most a 3 integer number.

I am trying to make the program output random numbers. I want to make it so that I can put the size of the array in the c1.sayit() and then it runs for that number you put in there.

so for example

c1.sayit(20)
getch();
c1sayit(40)

after I would output it I want to see that the size has actually changed.


also I was trying random things to make it work so if you see something wrong there is a high possibility it is.


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
class Class1{
  private:
    int size;
    int x1[40];
    
  public:
    void sayit(int size);
    void sayit();
    void doit();
    void writeit();  
}; // cs1

void Class1::sayit(int size){
  cout <<"\n\n\tRandom Numbers\n\n";
}//sayit 

void Class1::doit(){
   for(int i=0; i<size; i++) x1[i] = random(999);           
}//doit
 
void Class1::writeit(){
  cout << "\n\n\t Random Numbers...\n";
  for(int i=0; i<size; i++) cout << "\t" << x1[i];               
}//writeit



int main(){
   clrscr();
   randomize();
   Class1 c1,c2;
   c1.sayit(20);
   c1.doit();
   c1.writeit();
   getch();
   c2.sayit(40);
   c2.doit();
   c2.writeit();


I changed the code to this, when I run it, it says

Parameter 'size' is never used in function Class1::
sayit(int)
Last edited on
What you have posted still won't compile because it isn't complete. How do you expect anyone to help you when you won't even show us what the problem is?
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
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>


class Class1{
  private:
    int size;
    int x1[40];
    
  public:
    void sayit(int s1);
    void sayit();
    void doit();
    void writeit();  
}; // cs1

void Class1::sayit(int size){
  cout <<"\n\n\tRandom Numbers\n\n";
   
}//sayit 

void Class1::doit(){
   for(int i=0; i<size; i++) x1[i] = random(999);           
}//doit
 
void Class1::writeit(){
  cout << "\n\n\t Random Numbers...\n";
  for(int i=0; i<size; i++) cout << "\t" << x1[i];               
}//writeit



int main(){
   clrscr();
   randomize();
   Class1 c1,c2;
   c1.sayit(20);
   c1.doit();
   c1.writeit();
   getch();
   c2.sayit(40);
   c2.doit();
   c2.writeit();

   cout << "\n\n\n\tComplete";
     return 0;
} //MAIN 


Warning W8057 Parameter 'size' is never used in function Class1::
sayit(int)


^^^ thats the warning i get. when i run it, it just crashes.


thats all i have.
I think you believe that, when you say sayit (int size), the member size is going to get changed to a certain value. It won't. The variable size in sayit is at function scope - it is an argument of sayit, not a member of class1 - and it doesn't in any way affect the member size of the calling class. That's what the compiler is warning you about. When you try to access size, you get an undefined result that leads to this crash, potentially.
And where do you define sayit()?
Am I not defining sayit()?

I mean I did this and it worked.

1
2
3
4
void Class1::sayit(){
  cout <<"\n\n\tRandom Numbers\n\n";
   size = 20;
}//sayit  


How would I be able to input a number in the main and have it equal to the size, which is what I was trying to do when I did.

c1.sayit(20);


Well that should work. But when you do this:
sayit(int size)
That SIZE in the argument list is a completely separate thing from Class1::size. You get the argument size in the function, but that does absolutely NOTHING to the size member stored in your class object. You have no assignment in the function and so it changes NOTHING. Change that name to something else and do this:
1
2
3
4
5
void changingamember(int value) // even if this is the same name as someclassmember,
// it does not affect someclassmember EVER unless you perform an assignment in the function
{
    someclassmember = value;
}
Last edited on
okay ill try what you're saying. thanks for your help.
You do see what I mean, right?
yeah when i put the sayit(int size) it doesn't effect the class1size because I didn't assign it to anything.
Glad you see it.
Topic archived. No new replies allowed.