Int Not Returning Expected Number

So I am writing a program that takes user input and stores it as an integer inside of a class. for some reason right now the program is returning 1974509108 whenever I type in an integer. Segments of the code are below any help would be 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
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 <cstdlib>
#include <iostream>

using namespace std;
class Box {
      public:
           bool Cert;
           int Num; 
           
           int NumAssign(int Num){
               Num = 90;
               cout <<"Square: ";
               cin >> Num;
               return 0; 
               } 
           
           int  InputCheck(int Num){
                if(Num == 0){
                       Cert == false;
                       return 1;
                       }
                else if (Num == 1 || Num == 2 || Num == 3 || Num == 4 || Num == 5 || Num == 6 || Num == 7 || Num == 8 || Num == 9){
                     Cert == true;
                     return 1;
                     }
                else {
                     cout << "Wrong Number Input";
                     return 1;
                     }
               }
             
           int Reprint(int Num){
               cout<< "Square: "<< Num <<endl;
               }    
      };

int UserPrint(Box& Box1, Box& Box2, Box& Box3, Box& Box4, Box& Box5, Box& Box6, Box& Box7, Box& Box8, Box& Box9, Box& Box10, Box& Box11, Box& Box12, Box& Box13, Box& Box14, Box& Box15, Box& Box16, Box& Box17, Box& Box18, Box& Box19, Box& Box20, Box& Box21, Box& Box22, Box& Box23, Box& Box24, Box& Box25, Box& Box26, Box& Box27, Box& Box28, Box& Box29, Box& Box30, Box& Box31, Box& Box32, Box& Box33, Box& Box34, Box& Box35, Box& Box36, Box& Box37, Box& Box38, Box& Box39, Box& Box40, Box& Box41, Box& Box42, Box& Box43, Box& Box44, Box& Box45, Box& Box46, Box& Box47, Box& Box48, Box& Box49, Box& Box50, Box& Box51, Box& Box52, Box& Box53, Box& Box54, Box& Box55, Box& Box56, Box& Box57, Box& Box58, Box& Box59, Box& Box60, Box& Box61, Box& Box62, Box& Box63, Box& Box64, Box& Box65, Box& Box66, Box& Box67, Box& Box68, Box& Box69, Box& Box70, Box& Box71, Box& Box72, Box& Box73, Box& Box74, Box& Box75, Box& Box76, Box& Box77, Box& Box78, Box& Box79, Box& Box80, Box& Box81);

int main(int argc, char *argv[]){
Box Box1 (Skiping 80 Lines were I put the other 80 objects)

 UserPrint(Box1, Box2, Box3, Box4, Box5, Box6, Box7, Box8, Box9, Box10, Box11, Box12, Box13, Box14, Box15, Box16, Box17, Box18, Box19, Box20, Box21, Box22, Box23, Box24, Box25, Box26, Box27, Box28, Box29, Box30, Box31, Box32, Box33, Box34, Box35, Box36, Box37, Box38, Box39, Box40, Box41, Box42, Box43, Box44, Box45, Box46, Box47, Box48, Box49, Box50, Box51, Box52, Box53, Box54, Box55, Box56, Box57, Box58, Box59, Box60, Box61, Box62, Box63, Box64, Box65, Box66, Box67, Box68, Box69, Box70, Box71, Box72, Box73, Box74, Box75, Box76, Box77, Box78, Box79, Box80, Box81);
    system("PAUSE");
    return EXIT_SUCCESS;
}

int UserPrint(Box& Box1, Box& Box2, Box& Box3, Box& Box4, Box& Box5, Box& Box6, Box& Box7, Box& Box8, Box& Box9, Box& Box10, Box& Box11, Box& Box12, Box& Box13, Box& Box14, Box& Box15, Box& Box16, Box& Box17, Box& Box18, Box& Box19, Box& Box20, Box& Box21, Box& Box22, Box& Box23, Box& Box24, Box& Box25, Box& Box26, Box& Box27, Box& Box28, Box& Box29, Box& Box30, Box& Box31, Box& Box32, Box& Box33, Box& Box34, Box& Box35, Box& Box36, Box& Box37, Box& Box38, Box& Box39, Box& Box40, Box& Box41, Box& Box42, Box& Box43, Box& Box44, Box& Box45, Box& Box46, Box& Box47, Box& Box48, Box& Box49, Box& Box50, Box& Box51, Box& Box52, Box& Box53, Box& Box54, Box& Box55, Box& Box56, Box& Box57, Box& Box58, Box& Box59, Box& Box60, Box& Box61, Box& Box62, Box& Box63, Box& Box64, Box& Box65, Box& Box66, Box& Box67, Box& Box68, Box& Box69, Box& Box70, Box& Box71, Box& Box72, Box& Box73, Box& Box74, Box& Box75, Box& Box76, Box& Box77, Box& Box78, Box& Box79, Box& Box80, Box& Box81){

 cout << "Please Enter Data For Cubes" << endl;
    cout << "Cubes Are Numbered Left To Right Across" << endl;
      Box1.NumAssign(Box1.Num);
      Box1.InputCheck(Box1.Num);
      Box1.Reprint(Box1.Num); (80 More Lines with same format minus the reprint function)
return 1;
}  

When I run the code in whole I get the number mentioned above no matter what my input is.
Local variables with the same name as variables in a broader scope "shadow" or hide the variable of the same name in a broader scope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int num = 0;

void change_num_a(int num) {
    num = 42;
}

void change_num_b(int value) {
    num = value;
}

int main() {
    std::cout << "Value of num: " << num << '\n' ;

    change_num_a(99);
    std::cout << "change_num_a(99) called.\n";
    std::cout << "Value of num: " << num << '\n' ;

    change_num_b(99);
    std::cout << "change_num_b(99) called.\n";
    std::cout << "Value of num: " << num << '\n' ;
}
The Num parameter is conflicting with the Num member variable on line 10.
Whoah - before going any further, I think you should learn about arrays.
Any time you have lots of similarly-named variables (Box1, Box2, Box3 ...) it's probably a place where you should be using an array.

Lines 19 Cert == false;
and 23 Cert == true;
don't do anything. I get a compiler warning message:
// [Warning] statement has no effect [-Wunused-value]


At the end of function int Reprint(int Num) there's another warning:
[Warning] no return statement in function returning non-void [-Wreturn-type]

Your code promised that the function would return a value, but didn't do so. That means whatever garbage happened to be lying around on the stack will be interpreted, rightly or wrongly, as the value which should have been returned.

However, the real cause of the troubles here is the passing of unnecessary parameters,
 
Box1.NumAssign(Box1.Num);
should be simply
 
Box1.NumAssign();
and so on.

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
56
57
58
59
#include <iostream>

using namespace std;

class Box {

public:
    bool Cert;
    int Num; 
           
    int NumAssign()
    {
        Num = 90;
        cout <<"Square: ";
        cin >> Num;
        return 0; 
    } 
           
    int  InputCheck()
    {
        if (Num == 0)
        {
            Cert = false;  
            return 1;
        }
        else if (Num >= 1 && Num <= 9)
        {
            Cert = true;
            return 1;
        }
        else 
        {
            cout << "Wrong Number Input";
            return 1;
        }
    }
             
    void Reprint()
    {
        cout<< "Square: "<< Num <<endl;
    }   
};

void UserPrint(Box& Box1);

int main()
{
    Box Box1;
    UserPrint(Box1);
}

void UserPrint(Box& Box1)
{
    cout << "Please Enter Data For Cubes" << endl;
    cout << "Cubes Are Numbered Left To Right Across" << endl;
    Box1.NumAssign();
    Box1.InputCheck();
    Box1.Reprint();
} 

I thank you all for your help :). This fixed the issue I was having. I am glad that the people here are so helpful :)
Topic archived. No new replies allowed.