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
#include "binNum.h"
#include "misc_ops.h"
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace 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>
usingnamespace 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>
usingnamespace::std;
constint 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>
usingnamespace 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>
usingnamespace::std;
void printMenu();
char wouldYou();
void checkMe(char yourNum[]);
#endif
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'|
---------------------------------------