convert Num to Char**HELP PLS**

This is a discussion on convert Num to Char**HELP PLS** within the C++ Programming forums, part of the General Programming Boards category; hi, when i add something to the code there are some error, can someone please let me know or if ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Unhappy convert Num to Char**HELP PLS**

    hi, when i add something to the code there are some error, can someone please let me know or if possbile please correct it for me by letting me know as i want to know what is wrong with the code....

    when i run it the output is 66 62 12 15 28a s string, when i input Johnny.

    my target is to convert those number of string to characters as i assign A is 00, B is 01....till.......Z is 26. if the number >= 26, for example 44 is larger so 44%26 = 18 so the character output will be S.

    here is the code

    Code:
        stringstream numbers;
    
    /* Insert integer into stringstream*/
        numbers <<numLowerCaseLetter(name)<<sumAscii(name)<<lastValue<<counter<<sum;
    
    /* Convert stringstream into string.*/
        string str = numbers.str();
    
    
        if(str.size() % 2 == 0)
        {
            for(int i = 0; i < str[i]; i=i+2)
            {
                numOfTwo[i]=str.substr(i,2);
                cout<<numOfTwo[i]<<" ";
            }
        }    
        else
        {
            
            str.size()+1;
            str.append( 1, '0' );
            
            for(int i = 0; i < str[i]; i=i+2)
            {
                numOfTwo[i]=str.substr(i,2);
                cout<<numOfTwo[i]<<" ";
            }    
            
        }  
    
    // Final solution which convert number into character by producing a code.
    
     for(int i = 0; numOfTwo[i]; i++)
     {
         if(numOfTwo[i] >= 26)
         {
             modLeft = numOfTwo % 26;
             cout<< something[modLeft];
         } 
         else
         {
            cout<<something[numOfTwo[i]];
         }
    when i run the code, this error comes out

    In function `int main()':
    could not convert `numOfTwo[i]' to `bool'
    no match for 'operator>=' in 'numOfTwo[i] >= 26'
    invalid operands of types `std::string[15]' and `int' to binary `operator%'
    no match for 'operator[]' in 'something[numOfTwo[i]]'
    syntax error before `{' token

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if numOfTwo is an array of ints, then you can't assign a string to an int.
    Code:
     numOfTwo[i]= str.substr(i,2);<<<this won't work!
    try this instead
    Code:
     numOfTwo[i]= atoi(str.substr(i,2).c_str());

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by gtr_s15
    my target is to convert those number of string to characters as i assign A is 00, B is 01....till.......Z is 26. if the number >= 26, for example 44 is larger so 44%26 = 18 so the character output will be S.
    Are you trying to do something like this?
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    int main()
    {
       std::string text("Johnny");
       cout << text << " = ";
       for ( const char *ch = text.c_str(); *ch; ++ch )
       {
          // assume a character set with a contiguous alphabet, such as ASCII
          int value = toupper(*ch) - 'A';
          cout << value << ' ';
       }
       cout << endl;
       return 0;
    }
    
    /* my output
    9 14 7 13 13 24
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a searching tree
    By lindaonline15 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 07:06 AM
  2. Num to String? (atoi inverse)
    By hajas in forum C Programming
    Replies: 4
    Last Post: 06-22-2007, 04:22 PM
  3. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 10:19 AM
  4. Problem Using bsearch
    By Zildjian in forum C Programming
    Replies: 4
    Last Post: 11-13-2003, 08:14 PM
  5. Optimization of code
    By dpro in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21