my code has a 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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

#include <iostream>
#include<iomanip>

using namespace std;

bool exists(int array[], int length, int newnum)
{
     for (int i=0; i<length; i++)
     {
         if (newnum == array[i])
         {
                     
                         cout<<"true"<<endl;
                      return true;
              
         }
       
         
     }
     return false;
}/*
void BubbleSort(int numLength,int array[])
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (array[j+1] > array[j])      // ascending order simply changes to <
              { 
                    temp = array[j];             // swap elements
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
for(i=0;i<numLength;i++)
{
                        cout<<array[i]<<" ";
                        }
}
  void compare( int cosc208[],int count208, int CTECstudents[],int countMajor,int Majornum)
{
       } 
*/




  


 

            
int main()
{
    
    int count208, num208=0, countMajor, Majornum=0;
    int cosc208[25];
    int CTECstudents[25];
        for(int index=0; index<25;index++)
        {
                cosc208[index]=-1;
        }
        cout << "Enter the number of students who are taking COSC 208"<<endl;
         cin >> count208;
         for(int i=0;i<count208;i++)
         {
            
                cout << "Enter the next id of a 208 student:" <<endl;
                 cin >> num208;
    
   
                 while(exists(cosc208,count208,num208) == true)
                 {
                       cout << "Enter a unsued id of a 208 student:" <<endl;
                        cin >> num208;
                 }


                 cosc208[i]=num208;
                 for(int t=0;t<i+1;t++)
                 {
                       cout << cosc208[t]<<" ";
                 }

         }

         cout << "Enter the number of students who are majoring in CTEC"<<endl;
           cin >> countMajor;
         for(int i1=0;i1<countMajor;i1++)
         {
               cout << "Enter the next id of a CTEC major" <<endl;
                cin >> Majornum;
                while(exists(CTECstudents,countMajor,Majornum) == true)
                {
                      cout <<"Enter a unsued id of a CTEC major:" <<endl;
                      cin >> Majornum;
                }

                CTECstudents[i1]=Majornum;
          }

          cout <<"This is the differense of the 2 arrays" << endl;
          int k = 0;
          int ntMajor[25];


          for(int w =0;w<count208;w++)
          {
               int a, count = 0;
               for(int k=0;k < countMajor;k++)
               {
                    if(cosc208[w]!=CTECstudents[k])
                         count++;
                    if(count==count208)                
                    {
                       ntMajor[a]=cosc208[w];
                       a++;
                    }
               }
           }
           for(int z=0;z < count208;z++)
                 cout << ntMajor[z]<<" ";
           
system("pause");
return 0;

}


I am not having an error but at the end of my program i am getting random values. what am i doing wrong
On line 117 you create int a, but you don't intialize it, then you use to access your ntMajor array and write probably wayyy out of bounds (I'm suprised it didn't segfault).

Also, don't use system(): http://www.cplusplus.com/forum/articles/11153/
On line 117 you create int a, but you don't intialize it, then you use to access your ntMajor array and write probably wayyy out of bounds (I'm suprised it didn't segfault).


I recently had this problem as well. It's seems weird, but mine never segfaulted either. It took me forever to figure it out.
Why did you double post?
Topic archived. No new replies allowed.