Extracting Characters from String

This is a discussion on Extracting Characters from String within the C++ Programming forums, part of the General Programming Boards category; Hi All, Iím in the process of building a C++ program that prompts the user for two Roman numerals, then ...

  1. #1
    Unregistered
    Guest

    Extracting Characters from String

    Hi All,

    Iím in the process of building a C++ program that prompts the user for two Roman numerals, then prompts the user for an arithmetic operator, then calculates the two Roman numerals.

    -----------------------------------------------------

    For example:

    -----------------

    Enter first Roman numeral:

    // user enters XV, which is 15.

    Enter second Roman numeral:

    // user enters XX, which is 20.

    Enter arithmetic operator:

    // user enter +

    Your answer is: XXXV // which is 35.

    ----------------------

    For simplicity purposes Iím listing Roman numerals by highest value to lowest value. Therefore the number four will be represented in the program as IIII, not IV.

    Now, to the problem. After the user enters a Roman numeral I need the program to convert the Roman numeral to a float value in order to perform the calculation; this is what I cannot do. The following is what Iím trying to do in a void function:

    --------------------------------------------------------------------------

    void GetRoman()
    {
    string firstRoman;
    //string secondRoman;
    //char math;
    //int RomanI = 1;
    //int RomanV = 5;
    //int RomanX = 10;
    //int RomanL = 50;
    //int RomanC = 100;
    //int RomanD = 500;
    //int RomanM = 1000;
    int loopCount;
    int loop = 0;
    char ch;

    cout << " Enter first Roman numeral: ";
    cin >> firstRoman;

    loopCount = firstRoman.length();

    while (loop < loopCount)
    {
    loop = loop++;

    getline(cin, firstRoman);
    cin.get();

    --------------------------------------------------------------------------

    What I would like to do is start a loop, get the first character of the string ďfirstRomanĒ, read it, then convert it to a float value as listed by the above variables, some of which are commented out.

    Any help would be appreciated.

    Thanks,
    bob2509

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    void GetRoman()
    {
    string firstRoman;
    int loopCount = 0;
    int first = 0;

    cout << " Enter first Roman numeral: ";
    cin >> firstRoman;

    //change Roman numeral to arabic equivalent.
    while (loopCount < firstRoman.length())
    {
    switch(firstRoman[loopCount++])
    {
    case 'I':
    first += 1;
    break;
    case 'V':
    first += 5;
    break;
    //etc.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    My, this question looks familiar. You didn't happen to post the same thing on cpp-home.com did you? If so then I posted a full translation routine for you to play with, all you have to do is perform the arithmetic operation on the results of two calls to the routine.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Thumbs up Thanks Much!!!!

    switch was the trick. I appreciate it.

    bob2509

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  2. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

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