converting strings to char

This is a discussion on converting strings to char within the C++ Programming forums, part of the General Programming Boards category; Is there a way that you can convert a roman numeral to a decimal number? for example is there a ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    converting strings to char

    Is there a way that you can convert a roman numeral to a decimal number? for example is there a way to break a string into 1 letter at a time so I can verify 1 letter at a time so I can put it in a casestatement

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Sure, you can do that.
    Code:
    char *lpString = "Hello World";
    
    char firstChar = *lpString;
    char secondChar = *(lpString + 1);
    
    switch( firstChar )
    {
      case 'X':
      case 'x':
    
        // blah..
        break;
    }
    That code does nothing but you should get the idea how to walk through a string. You have a pointer to a series of characters. When you dereference the pointer you get a single character. So to get the second character I add one to the pointer address and then dereference. Hope that helped.

    EDIT:
    You'll want this in a loop and you'll want to check for the '\0' character because strings are null-terminated.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    If you are using type string, you can use index operator [].
    Code:
    string str = "Hello Word!";
    
    char first = str[0].
    gg

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Originally posted by Codeplug
    If you are using type string, you can use index operator [].
    Code:
    string str = "Hello Word!";
    
    char first = str[0].
    gg
    I was not aware you could use the index operator on a string type. I am glad I read this thread

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    do a board search. Roman numeral conversions crop up quite often.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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