Ok, so I'm really really honestly new to c++, and I know very little about it.
The thing I want to do for my project is a number system converter. Binary, Decimal, Octal, Hexadecimal, and my professor said the fractional part of the number need be included, e.g., .25 (base ten) to its binary (and octal and hexadecimal) equivalent, and vice versa.
So, I'm working on the first part, and I'm stuck.
The problem: I cannot reset my pointer, when a user gives an invalid input.
Scenario: When a user wants to convert a binary to decimal (fractional part not yet included), and that user inputs characters other than 0 and 1, input process will have to be repeated, previous pointer value will have to be deleted.
Thing is: I somehow cannot find a way to do that. I tried "delete pointer_here" and "delete[] pointer_here" with and without "new" but it returns something like "Null pointer assignment"...
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
|
#include <iostream.h>
void bininput();
int bin2dec(const char *bin)
{
int result = 0;
if ((*bin!='0') && (*bin!='1')) {
cout<<"\nYou have not followed correctly the instructions. Only 0's and 1's are allowed.";
bininput(); }
for (;*bin;bin++)
{
result = result*2+(*bin-'0');
}
return result;
}
void bininput() {
char * entree;
cout<<"\nEnter number: ";
cin>>entree;
cout<<bin2dec(entree);
}
int main() {
cout<<"Binary? ";
char n;
cin>>n;
if ((n=='y') || (n=='Y'))
bininput();
else
return 0;
return 0;
}
|
Note: We are so using Turbo C++.
I'll appreciate any kind of help. ;)