Thread: problem with char() feature

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    problem with char() feature

    i am trying to make my program output values 10 or greater as the capitol letters of the alphabet, but i am having no luck, my code thus far is

    Code:
    void convert(vector<int>& rev_rems,int base,int num_div,vector<int>& rems)
    {
       int num;
       int rem;
       for(num=num_div;num!=0;num/=base)
       {
          rem=num%base;
          rev_rems.push_back(rem);
       }
       for(rev_rems;rev_rems.size()!=0;rev_rems.pop_back())
       {
          rems.push_back(rev_rems[rev_rems.size()-1]);
       }
    }
    
    void hex_convert(vector<int>& rems,vector<int>& rev_hex)
    {
       for(int i=0;i<rems.size();i++)
       {
          if(rems[i]>=10)
          {
             int x=rems[i]-10;
    	 rev_hex.push_back(char(65+(x)));
          }
          else
          {
             rev_hex.push_back(rems[i]);
          }
       }
    }
    every time i try to make it output a letter it always spits out the number itself instead, when i try to get it to spit out the equivilent of 1,8,12 (285 base 13) it ends up spitting out 1,8,67, which if i could get it to display correctly 67 would be C. I have a feeling this problem is something obvious, but i just cannot seem to find it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So 'C' is 67 in ASCII, it is true. We're back to the same impasse as before: you should either store characters in your vector, or ints, but not both at the same time. If you decide to store characters, then change your vector appropriately (and then store '0' through '9' instead of 0 through 9). If you decide to store numbers (which I would suggest) then you need to make the change at the point of printing.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    ok, that makes more sense than what i was trying to decipher from the previous post, thank you

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    problem has been solved, thank you for the idea to take the char values at the end when i am displaying the information

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. C++ (char *) problem?
    By Teegtahn in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2008, 01:23 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

Tags for this Thread