![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 13
| converting letters into numbers. 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++;
}
}
|
| xforevertink is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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'.) |
| tabstop is offline | |
| | #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;
}
|
| xforevertink is offline | |
| | #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. |
| xforevertink is offline | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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. |
| tabstop is offline | |
| | #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. |
| xforevertink is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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. |
| tabstop is offline | |
| | #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? |
| xforevertink is offline | |
| | #9 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Of course not. Strings are sequence of characters. Those characters can be numerals -- "1" is a string, but 1 is just an integer. |
| tabstop is offline | |
![]() |
| Tags |
| c++, converting, letters, numbers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Converting _getch() inputted numbers | bengreenwood | C++ Programming | 9 | 07-16-2008 10:46 PM |
| converting numbers to letters | sh4k3 | C Programming | 6 | 06-10-2007 07:15 PM |
| converting numbers to words | dionys | C Programming | 2 | 05-08-2004 09:34 AM |
| the definition of a mathematical "average" or "mean" | DavidP | A Brief History of Cprogramming.com | 7 | 12-03-2002 11:15 AM |
| How do you allocate numbers to letters? | face_master | C++ Programming | 9 | 11-14-2001 06:47 AM |