first thing i enter into my array dissapears

Ok so ive been working on a calculator for binary numbers using chars not ints and the input was working fine i dont know what i did but now the first digit you enter into the array doesnt store it works for the second array you enter but the first one always skips the first digit someone please help me figure out what i need to change ill post all my codes up for the differnt files im working with

so if you entere 1001 as num one and 1000 as num 2
num one comes out as 001 and num 2 comes out as 1000
im confused because i didnt change anything and it was working just fine but now isnt im guessing i fucked my pointers up or something dumb Please help

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  
#include "binNum.h"
#include "misc_ops.h"
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;



int main()
{
	

	char num1[4];
	char num2[4];
	char loop = 'y';
	
	
	do {
		printMenu();
		
		 cin >> num1; // this is the one thats not working
		 cout << "\n";
		 cin >> num2; // this one gets the input just fine 

		 checkMe(num1); // comment this out 
		 checkMe(num2); // comment this out 

		

		BinNum YourNum1(num1); // you can display the array a differnt way if you want to not have to set up the class files 
		BinNum YourNum2(num2); // same with this 
		
		cout << "\n";
		cout << YourNum1; // ovverloaded class 
		cout << "\n";
		cout << YourNum2; // ovverloaded class


		char answer1 = wouldYou();
		do {
			int opperation;
			cout << "\n What would you like to do?\n";
			cout << " 1:Addition 2:Multiplication\n";
			cin >> opperation;
			switch (opperation)
			{
			case 1:
				cout << "\nAdding Goes Here";
				answer1 = 'N';
				break;
			case 2:
				cout << "\nMultiplying Shizz";
				answer1 = 'N';
				break;
			}

			
			answer1 = 'N';
		} while (answer1 == 'y' || answer1 == 'Y');







		cout << "\n Would you like to restart? y or n\n ";
		cin >> loop;

	} while (loop == 'Y' || loop == 'y');
	
	system("pause"); // holds window 
	return 0;

}



#include "binNum.h"
#include  <iostream>
using namespace std;




/******************************************************************************************
*
*   Function Name: 	BinNum
*
*   Purpose: 		user supplied default constructor. Creates a BinNum object which is
*			initalized to 0000. The BinNum object is a char array of size 
*			NUM_BITS		
*
*
*   Input Parameters:	None
*
*   Output parameters:	None
*
*   Return Value:	None
*
******************************************************************************************/



BinNum::BinNum()
{
  int i;

  for( i = 0; i < NUM_BITS; i++ )
    the_num[ i ] = '0';
  
}



/******************************************************************************************
*
*   Function Name:	getBit
*
*   Purpose:		gives the bit at the specified input postion i for 
*			the supplied object
*
*
*   Input Parameters:	one; an int i. i represents the location of the ith bit 
*			of the binary num this
*
*   Output parameters:	none
*
*   Return Value:	one, a char representing the bit ( 0 or 1 ) of the binary number
*
******************************************************************************************/



char BinNum::getBit( int i)
{
  return  (  (*this).the_num[i] );
}



/******************************************************************************************
*
*   Function Name:	<<
*
*   Purpose:		overloaded insertion operator for the BinNum struct. 
*			Friend of the struct BinNum
*
*
*   Input Parameters:	two an ostream object s, and a BinNum b
*
*   Output parameters:	none
*
*   Return Value:	an ostream object s containing the binNum b followd by a newline
*
******************************************************************************************/


ostream &operator << ( ostream &s, BinNum &b )
{
  int i;
  for( i = 0; i < NUM_BITS; i++ )
    s << b.the_num[i];
  s << endl;

  return s;
}



/******************************************************************************************
*
*   Function Name:	>>
*
*   Purpose:		overloaded extraction operator for the BinNum struct. 
*			Friend of the struct BinNum
*
*
*   Input Parameters:	two an istream object s, and a BinNum b
*
*   Output parameters:
*
*   Return Value:	an istream object s containing the binNum b
*
******************************************************************************************/


istream &operator >> ( istream &s, BinNum &b )
{
  int i;
  for( i = 0; i < NUM_BITS; i++ )
    s >> b.the_num[i];


  return s;
}




/******************************************************************************************
*
*   Function Name:
*
*   Purpose:
*
*
*   Input Parameters:
*
*   Output parameters:
*
*   Return Value:
*
******************************************************************************************/



BinNum operator+ ( BinNum &b1, BinNum &b2 )
{
 // the next 2 lines are suppiled just for this stub to
 // function correctly. Programmer must remove them after supplying required code
  BinNum temp;
  return temp;
}




BinNum operator* (BinNum &b1, BinNum &b2)
{


}


BinNum::BinNum(char yourNum[])
{

int i;

for (i = 0; i < NUM_BITS; i++)
	the_num[i] = yourNum[i];

}


#ifndef BINNUM_H_
#define BINNUM_H_

#include <iostream>
using namespace::std;


const int NUM_BITS = 4;		//  number of bits in binary number


class BinNum
  {
public:
    BinNum();
    char getBit( int i );
	BinNum(char yourNum[]);


    friend ostream &operator << ( ostream &s, BinNum &b );
    friend istream &operator >> ( istream &s, BinNum &b );
    friend BinNum operator+ (BinNum &b1, BinNum &b2 );
	friend BinNum operator* (BinNum &b1, BinNum &b2);


private:
    char the_num[NUM_BITS];
  };




#endif



#include "misc_ops.h"
#include  <iostream>
using namespace std;



/******************************************************************************************
*
*   Function Name:
*
*   Purpose:
*
*
*   Input Parameters:
*
*   Output parameters:
*
*   Return Value:
*
******************************************************************************************/


void printMenu()
{
	cout << "Welcome to Binary Bonkers";
	cout << "\nLet's Get Started!";
	cout << "\nPlease Enter two Binary Numbers in the following form: ";
	cout << "\n 1001 0001 1010 1010\n\n";
	cout << "\nPress enter after each number is entered\n";
	


}

/******************************************************************************************
*
*   Function Name:
*
*   Purpose:
*
*
*   Input Parameters:
*
*   Output parameters:
*
*   Return Value:
*
******************************************************************************************/


char wouldYou()
{
	char c;
	cout << "\n Would you like to Add Or Multiply?\n";
	cout << "Y/y for yes N/n for no\n";
	cin >> c;
	return c;
}


void checkMe(char yourNum[])
{
	int i;
	for (i = 0; i < 4; i++)
			if (yourNum[i] == '1' || yourNum[i] == '0')
	       {
		       cout << ".";
	       }
           
			else
			{
				cerr << "\nOne Of the entered Binary Numbers are Invalid\n";
				system("pause"); // holds window for error check 
				exit(0);
			}
	//cout << "\n";
		
}



#ifndef MISC_OPS_H_
#define MISC_OPS_H_

#include <iostream>
using namespace::std;

void printMenu();
char wouldYou();
void checkMe(char yourNum[]);








#endif
Welcome to Binary Bonkers
Let's Get Started!
Please Enter two Binary Numbers in the following form:
1001 0001 1010 1010


Press enter after each number is entered
1010

1010
....
010

1010

Would you like to Add Or Multiply?
Y/y for yes N/n for no

this is what the console looks like where it messes up
To store a string of 4 characters you need an array of at least 5 characters because the end of a string is marked with a null character '\0'.
Last edited on
why does the second one work then when they are both the same size char arrays? but thank you i shall see if that fixes it
ahh yes that fixes it thank you peter ! you rock
why does the second one work then when they are both the same size char arrays?

It's hard to know exactly why it happens without taking a close look at the assembly code that the compiler produces for the program. The C++ standard just say this is undefined behaviour which means anything is allowed to happen.

A guess is that num1 is stored after num2 in memory. If num1 is "1001" and num2 is inputted as "1000" it will write the null termination character beyond the end of num2, overwriting the first char in num1.

         Before reading num2

num2            num1            end of num1
|               |               |
--------------------------------------
| ? | ? | ? | ? |'1'|'0'|'0'|'1'|'\0'|
--------------------------------------
         After reading num2

num2            num1             end of num1
|               |                |
---------------------------------------
|'1'|'0'|'0'|'0'|'\0'|'0'|'0'|'1'|'\0'|
---------------------------------------
Last edited on
Topic archived. No new replies allowed.