Hi everyone,

I am writing a program that converts numbers to words and vice-versa, like typing on a older cellphone. The user enters as many messages as he wants in numbers and/or text. The first character of the messages indicates what the remaining string is: numbers or letters. I can't seem to find a way to get that first character without errors spawning everywhere. I have tried using cin and getline. If someone could explain what I am doing wrong, that would be great.
Here is what I have:

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    //Declare variables
    string indicator;
    string input;   

    //Get input
    cout << "Enter your message, starting with '#' for text, and '@' for numbers." << endl;
    5   while(input != '*')
16  {
17      cin >> input;
18  //This is where I would place the code to get the indicator
19  //if(#), then textToNumber function. 
20  //if(@), then numberToText function.
21  }
22   
23  //Display results
24  cin.get();
25  cin.get();
26   
27  //Exit program
28  return 0;

Recommended Answers

All 3 Replies

Your "indicator" is a single character, so why declare it as a string?

Is it your intent that a user's message is just one line (no newlines)? If so, get an input then examine the first character of that to determine the processing needed. Something like

if( input[0] == '#' )
  //do processing

To end the loop, do you expect the user to enter just a single * ? In that case, you need to compare to the string containing asterisk, not the char. while( input != "*" )

I would find it a bit confusing to use # (sometimes called the number sign) to start text and a different character to start a number.

The message can be as long as the user wants, so I guess it could very well have multiple lines. I actually figured out how to get the first character, but now my I can't find a good source for text to number and number to text

stringstream is probably the easiest way to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.