converting letters into numbers.

This is a discussion on converting letters into numbers. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to convert the letters into numbers. But I keep coming into errors in the if loop of the ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    13

    converting letters into numbers.

    I'm trying to convert the letters into numbers. But I keep coming into errors in the if loop of the statement. Particularly, in the....

    phrase.replace(i,1, 'num');

    Right now, I'm getting a warning and 2 errors.
    When I take out the single quotes, I just get the 2 errors...

    Hellp!!!


    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main ()
    {
    	string phrase;
    	int i = 0;
    	int num = 1;
    	char letter = 'A';	
    	
    	cout << "Enter phrase: ";
    	getline(cin, phrase);
    	
    
    	
    	for (i = 0; i < phrase.size(); i++)
    	{
    		if (phrase[i] == letter)
    		{
    			phrase.replace(i,1, 'num');
    		}
    		letter++;
    		num++;
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What are you even trying to do?

    If you want to replace a character with another character, then do so. (For instance, if you wanted to replace with the character '1', then you could use ... the character '1'.)

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Well, the program prompts the user to enter a phrase. And I just want to convert all the letters into numbers starting from 1. So that 'A' would equal 1. 'B' would equal 2, and so on.

    And in the end, I want it to output the same phrase as numbers onto the screen.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main ()
    {
    	string phrase;
    	int i = 0;
    	int num = 1;
    	char letter = 'A';	
    	
    	cout << "Enter phrase: ";
    	getline(cin, phrase);
    	
    
    	
    	for (i = 0; i < phrase.size(); i++)
    	{
    		if (phrase[i] == letter)
    		{
    			phrase.replace(i,1, 'num');
    		}
    		letter++;
    		num++;
    	}
    cout << phrase;
    
    return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    If I replace num with '1', then I would have to type out all 26 numbers. I just wanted a quicker way to replace the whole phrase by incrementing num as it encounters all the letters, since I'm incrementing that too.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then I would venture that you have not a single line correct -- you can't possibly replace in place, since a majority of the letters would require a two character replacement, which means you can't actually replace them. You can build a new string, if you want, or you can go through the string and output each number as you go. If you're allowed to assume ASCII encoding, and if you remember that you can do arithmetic on chars just as well as anything else (since they are, after all, merely small integers), then you're golden.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Can you explain the two character replace? I'm new to c++ programming. I don't understand what you mean by that.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, once you get to J, you would need to "replace" the one character 'J' with the two characters '1' and '0' (since J = 10); same for K, L, ..., Z.

    Now string's replace, which you are using, does allow you to replace different-sized things -- but you will need to be replacing with a string, not a single character. And, if you don't want to build twenty-six different strings, then you're going to have to learn how to convert a number into a string.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    13
    Yeah. I actually tried changing the line

    int num = 1;

    with....

    string num = 1;

    But instead I got an error, which I don't understand. I thought strings could read numbers as well?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course not. Strings are sequence of characters. Those characters can be numerals -- "1" is a string, but 1 is just an integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting _getch() inputted numbers
    By bengreenwood in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2008, 11:46 PM
  2. converting numbers to letters
    By sh4k3 in forum C Programming
    Replies: 6
    Last Post: 06-10-2007, 08:15 PM
  3. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 10:34 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. How do you allocate numbers to letters?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 06:47 AM

Tags for this Thread


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