Ok, Im not going to pretend to know I know that much C++ compared to most things I have read on this site. I am taking a highschool class in c++ and basically I thought it was way too simple and I actually want to learn something. OK, so I made this code for a base converter, and right now its working with bases 2-36. However, I'm pretty sure I must've wasted alot of time with some things. Anyone who wishes to can make suggestions on how to make it better. I've taken the class for about 5 weeks and am like a year ahead of the class so I hope this isn't that bad. If you think it sucks just say so, any constructive critisism helps. Thanks.
P.S i hope those tags worked =\Code:#include<conio.h> //Includes are from my Start.cpp #include<iostream.h> #include<iomanip.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<stdio.h> #include<dos.h> #define MAX 20 //I Need a better way to do this class Base //First real time using classes, trying it out { public: long int real; char fake; private: }; long int power(int x, int y); // Since I don't know an exponent function void clear (int x, Base y []); // Clean-Up arrays main() { int base1; int base2; Base num1[MAX]; Base num2[MAX]; int y = MAX; int flag = 0; clrscr(); clear (MAX, num1); clear (MAX, num2); cout << "\nEnter Number to be converted: " << endl << endl; for (int x=0;x<=MAX; x++) // Asks for number in an array { if (x == MAX) { x--; break; } num1[x].real = getche(); if (num1[x].real == 13) { num1[x].real = 0; x--; break; } else if ((num1[x].real >=48) && (num1[x].real <=57)) num1[x].real -= 48; else if ((num1[x].real >=65) && (num1[x].real <=90)) num1[x].real -= 55; } // Was there an easier way to do that? cout << endl << endl << "Enter base of that number: "; cin >> base1; cout << endl << "Enter base to be converted to: "; cin >> base2; int w = x; for (x; x >= 0; x-- ) // This loop does oldbase->base10->newbase { num2[0].real += num1[x].real * power(w-x, base1); for (y=0, num2[y].real; num2[y].real >= base2; y++) { num2[y+1].real += num2[y].real / base2; num2[y].real %= base2; } } cout << "\nYour new number is: "; if (base2 <= 10) { for (y=MAX;y>0;y--) { if ((flag == 0) && (num2[y-1].real != 0)) flag = 1; if (flag==1) cout << num2[y-1].real; } } else if (base2 > 10) { for (y=MAX;y>0;y--) if (num2[y-1].real >= 10) num2[y-1].fake = 55 + num2[y-1].real; for (y=MAX;y>0;y--) { if ((flag == 0) && (num2[y-1].real != 0)) flag = 1; if (flag==1) { if (num2[y-1].real < 10) cout << num2[y-1].real; else if (num2[y-1].real >= 10) cout << num2[y-1].fake; } } } getch(); return 0; } void clear (int x, Base y []) { for (x; x>=0; x--) { y[x].real = 0; } } long int power (int x , int y) { if (x == 0) return 1; else return ( y * power(x-1, y)); }



LinkBack URL
About LinkBacks



