Thread: question about base 10 to base N convertor lab program

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    question about base 10 to base N convertor lab program

    this is my code, i am not sure how to use tags or even what that is, so i apologize if its in the wrong format. it compiles but when i run, say i take the number 26, if i try to take the base 4 of it, it works, or if i take the base 8 it works, but if i take the base 2 it doesnt work? i cant figure it out:? any advice
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*This handles up to B64*/
    #define B10MAXVAL 100 
    
    void convertToBase(int, int, char [], int);
    char toChar(int);
    char ans;
    void swapspace(char []);
    int main()
    
    {
      for (;;) // why doesnt this loop work ?
    {
          
    char value[B10MAXVAL];
    int input = 0;
    int base = 10;
       
       /*using scanf %[*][width][modifiers]type*/
      
       
       printf("Please enter the number you wish to convert:\n");
       scanf("%d", &input); 
       fflush(stdin);
       printf("Please enter the base you want to convert to:\n");
       scanf("%d", &base);
       fflush(stdin);
       convertToBase(base, input, value, 0);
       printf("%d converted to base-%d is %s", input, base, value);
       // getchar();
       printf("\n\nDo you wish to run my aswesome program again Y/N?");
       ans = getchar();
       ans = getchar ();
       if (ans=='N' || ans=='n')
          break;
       
    }
       printf("\n");
       system("PAUSE");
    
    
       return 0;
    }
    
    void convertToBase(int base, int number, char outputstring[], int i) {
    
    /*do this by repeatedly dividing and taking the modulus (remainder). */
      int r = 0;
      int result = 0;   
      if (number == 0) { 
    	 /*Making sure there is no 0*/       
    	 outputstring[i] = 0x30;           
    	 outputstring[i+1] = 0;}          
    	 
    	 /*Divide using mod operator until 0 is reached*/
    	 
      r = number % base;
         if(((number-r) == 0)&& ((number - r)!= number)) {
    	    outputstring[i] = toChar(r);
    	    outputstring[i + 1] = 0;}
      
               else if (((number - r) == number)){
    	          outputstring[i] = toChar(number-1);
    	          outputstring[i + 1] = 0; }
      
                     else {
                     outputstring[i] = toChar(r);
                     convertToBase(base, ( (number-r)/base ), outputstring, (i+1)); 
         
          /*call swapspace, recursion owns, one of many options, reverse iteration
          or copy_backwards algo in C++ library*/
     }                                         
         if (i == 0) {
    	    swapspace(outputstring);}
    }
    
        char toChar(int i){
    	char alphabet[] = {'0','1','2','3','4','5','6','7','8','9',
    'A','B','C','D','E','F','G','H','I','J','K','L','M',
    'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    'a','b','c','d','e','f','g','h','i','j','k','l','m',
    'n','o','p','q','r','s','t','u','v','w','x','y','z','+','/','='};
        return alphabet[i];
    }
    
    void swapspace(char string[]){
    /*Because we're adding in the wrong direction, we need to swap the order of our value*/
    	int i = 0;
    	int x = 0;
    	char *original;
    	
    /*here we allocate memory and determine the size of the space needed with 
    then we move the string into original, then we free the stack*/
     
    	original = (char*)malloc(sizeof(char) * (strlen(string) + 1));//Leave room for null 0
    	strcpy(original, string); 
    	x = strlen(string); 
    	
        while (i < x){
    	    string[i] = original[(x-1) - i]; //-1 to kill null zero
    	    i++;
    	}
    	
    	string[i] = 0;
        free(original);  
    
    }
    Last edited by Salem; 11-30-2010 at 11:11 PM. Reason: Wrap long line

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include <iostream>
    If you remove this line, you have a C program.

    > fflush(stdin);
    SourceForge.net: Fflush - cpwiki

    > original = (char*)malloc(sizeof(char) * (strlen(string) + 1));
    If you really compile this as C, then you don't need the cast.

    > but if i take the base 2 it doesnt work? i cant figure it out:?
    So what numbers did you try?
    0 in base 2
    1 in base 2
    2 in base 2
    What output did you see?

    I notice your function has if / else if / else (3 branches), yet binary can only ever be if / else




    If you really want this to be C++, then drop all the printf/scanf/str... functions and find out all the good stuff that is in
    #include <iostream>
    #include <string>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    help

    thank you for the advice, this is a old c program that i am trying to convert to c++ for this lab, so if you have time could you maybe explain some of your ideas better?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > this is a old c program that i am trying to convert to c++ for this lab
    How about trying to write it yourself?

    I've no particular interest in helping people pass a course based on their ability to google and getting others to fix up the code they find to fit their requirements.

    Unless you actually understand how to analyse and solve problems yourself, you would be utterly useless in the workplace. Your degree might get you a job, but it would do nothing to help you keep it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    thanks

    actually dude, i did write that c code myself thank you very much and i am not a computer sci major, but thank you for your condensending attitude after i asked a simple question, all i wanted was a little bit further explaination, congrats! though thanks for the tip about binary and if else, as that was the problem
    Last edited by ethankins; 12-01-2010 at 10:47 AM. Reason: grammar and makin sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Binary to Base 10 decimal conversion
    By JFonseka in forum C Programming
    Replies: 13
    Last Post: 11-20-2007, 04:14 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  5. Question about "Answer Key" Program
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 11-14-2001, 09:55 PM