Thread: New-Coder needing suggestions

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    New-Coder needing suggestions

    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.

    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));
    }
    P.S i hope those tags worked =\
    Last edited by BakAttack; 01-16-2003 at 01:05 AM.
    ~fin

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    A couple of things I see:

    >#define MAX 20 //I Need a better way to do this

    const int MAX = 20;

    void clear (int x, Base y [])
    {
    for (x; x>=0; x--)
    {
    y[x].real = 0;
    }
    }
    set the memory in Base's constructor: If you want a function like your clear() try memset()

    Code:
    class Base    //First real time using classes, trying it out  
    {
        public:
    long int real;
    char fake;
    Base ();
        private:
    };
    Base::Base()
    {
        real = 0;
    }
    Also the data members would normally be private, and accessed through class member functions.


    long int power (int x , int y)
    {
    if (x == 0)
    return 1;
    else
    return ( y * power(x-1, y));
    }
    There's a function called pow() in <cmath> formerly known as <math.h>

    -Futura
    Last edited by Codulation; 01-16-2003 at 02:27 AM.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    other things to be addressed...use the new standard headers, i.e. <iostream> <cmath> <cstdlib>, etc. also, you should declare main like this...

    int main()...I'm not sure if the way you have it will compile on newer compilers. old headers, like iostream.h, would produce warnings with newer compilers...newer compilers are more standard compliant.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    Wow, Ok thanks. I'm going to go try to implement some of that stuff. BTW for the Const int MAX = 20; i know how to do that but i think a counter would be better. Also I'm having a problem with big numbers since like doing Z * 16 to the 10th power messes things up =\. I'll keep working on it, thanks again.
    ~fin

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    If you need more size you can use long double instead of long int, long double is 8 bytes (on my system) and long int is 4.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    modulus % operator does not work with double, unless im mistaken (could be).
    ~fin

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    You are correct

    Originally posted by BakAttack
    modulus % operator does not work with double, unless im mistaken (could be).
    The modulus "%" operator will work only with integral data types (char, int -- short, long, unsigned, signed)
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions and question about bool pointers
    By Akkernight in forum C++ Programming
    Replies: 15
    Last Post: 03-22-2009, 12:27 PM
  2. Suggestions?!
    By liljp617 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-20-2008, 11:12 AM
  3. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  4. Math Book Suggestions
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-09-2003, 10:43 AM
  5. Learning C for OSX, tutorial suggestions please
    By Nexum in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 04:57 AM