Thread: Hey guys, I need a little help here~

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Hey guys, I need a little help here~

    Hey guys,

    I'm doing a programing that will convert a number in one base to another base output.

    My program works fine now. It can convert any positive real number in one base to another base.

    So, if I input Value 21 in base 10 and wanted to convert to base 2
    it will print 10101....which is right


    But the problem is:

    if I input 21012 in base 2, it still execute it...

    the binary rule is, any number in the input can't be equal or greater than the base, so in this case, it can't have any number >= 2

    So, I want to output a warning message if this case happens, and indicates that the value is not valid.

    How do I do that?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By checking the input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Yes, I know by checking the input...But how can I do that?
    Do I need to check each number one by one like...

    21012 - > "2" "1" "0" "1" "2" ?

    If so...I have no idea how to do that.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you posted your current code, I might be more inclined to be less vague
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your compiler will have one or more functions to transform a number, into a string. So, you need a char array set up for it, and then see if your compiler has itoa. If not, peek around your help topics, because it will have something like it.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
       int number = 12345;
       char string[25];
    
       itoa(number, string, 10);
       printf("integer = %d string = %s\n", number, string);
       return 0;
    }
    You can also "roll your own...battery", <little inside joke>, but it's much faster to just use a function like itoa(), if you have it.

    <LOL> If you posted your current code, I might be more inclined to be less vague
    Last edited by Adak; 10-29-2010 at 12:14 AM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Here

    Here's my code:



    Code:
    #include <stdio.h>
    
    
    int main(){
    
        
        int b2, power=1;
    
    
    ...//Deleted //...
    
    
    
     
        printf("_%d", b2);
    
    
    
        scanf("\n");
    
        
        
        return 0;
    
    
    
    }
    Last edited by readytogo; 10-29-2010 at 01:07 AM.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Sorry to say this guys, I haven't learned array yet, and we are not allow to use it now until later. My teacher said so =( So is there any other ways?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes. Directly check every number by taking one digit at a time, off the right hand side (or some side, but right hand, for now), and comparing it with the base.

    We can treat the 21012 number as if it was any suitably sized base, so let's use base 10, just for convenience.
    Code:
    int num = 21012;
    int badnum, remNum, baseNum=2 
    
    badnum = 0;
    while(num > 0) {
      remNum = (num % 10);
      //remNum now equals 2 on the first loop, 1 on the second, etc.;
    
      if(remNum >= baseNum) {
        badNum = 1;
        break;
      }
      num /= 10; (num = num / 10)
    }
    which "peels off the most right hand digit, completely, with each loop. Continue looping until num < 1, and if you haven't got a badNumber==1, it's OK.

    I haven't run this code, but it's very close. Any errors, let me know.
    Last edited by Adak; 10-29-2010 at 12:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hey need help with a welcome screen
    By garry_b in forum C Programming
    Replies: 11
    Last Post: 04-27-2009, 02:50 PM
  2. Hey guys, back again with a question
    By Velocity in forum C++ Programming
    Replies: 10
    Last Post: 10-19-2008, 02:27 PM
  3. Hey Guys
    By D4050 in forum C Programming
    Replies: 0
    Last Post: 10-01-2001, 06:20 AM
  4. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM