Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,267 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,486 people online right now. Registration is fast and FREE... Join Now!




How to read each character in a string

 
Closed TopicStart new topic

How to read each character in a string

Umbrella
14 Oct, 2006 - 07:16 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2006
Posts: 49


My Contributions
Im making a hexadecimal program and trying to figure out how to read each character in the string seperatly sto assign the int equivialent. for instance if the hexadecimal number was "aabc" how would i read a first then the next char then the next until there were no more characters
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: How To Read Each Character In A String
14 Oct, 2006 - 08:13 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(Umbrella @ 14 Oct, 2006 - 08:16 PM) *

Im making a hexadecimal program and trying to figure out how to read each character in the string seperatly sto assign the int equivialent. for instance if the hexadecimal number was "aabc" how would i read a first then the next char then the next until there were no more characters

First thing to understand:
Strings are an array of characters, see for more infornation.
User is offlineProfile CardPM
+Quote Post

Umbrella
RE: How To Read Each Character In A String
15 Oct, 2006 - 09:20 AM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2006
Posts: 49


My Contributions
I dont know if any of those really help solve my problem. Im still having trouble.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Read Each Character In A String
15 Oct, 2006 - 09:36 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Can you post the code you have completed so far?

It would make it much easier to help if we can see what you are doing.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Read Each Character In A String
15 Oct, 2006 - 09:42 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
The information provided by GregoryH is exactly what is required to understand how strings of characters work. The links he provided show how to traverse a character string character by character...once you do that, you can process each character in turn. You can traverse the string, and store the corresponding int in another array, or evn a string version of the int in the same array.
User is offlineProfile CardPM
+Quote Post

Umbrella
RE: How To Read Each Character In A String
15 Oct, 2006 - 09:58 AM
Post #6

New D.I.C Head
*

Joined: 13 Oct, 2006
Posts: 49


My Contributions
CODE
//Author:
//File:  HexCalculator.cpp

/*    
--Purpose--
×Create a hexidecimal calculator that can:
1.  Perform the mathematical operation addition
2.  Perform the mathematical operation subtraction
3.  Perform the mathematical operation multiplication
4.  Perform the mathematical operation division
5.  Convert a hexadecimal number to it's decimal equivalent
6.  Convert a decimal number to it's hexadecimal equivalent
*/

//enum for operations
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

enum MenuType {ADD, SUBTRACT, MULTIPLY, DIVIDE, HEX_TO_DECIMAL, DECIMAL_TO_HEX, QUIT};

//function prototype
MenuType GetMenuPick();
string GetHex();
void Add();
void Subtract();
void Multiply();
void Divide();
int ConvertFromDecimal(string);
string ConvertFromHex(int);
void InvalidHex();
void OperationSelector(MenuType);


int main()
{
    MenuType choice;
    choice = GetMenuPick();

    OperationSelector(choice);

}


MenuType GetMenuPick()
{
    char choice;

    cout << "HEXIDECIMAL CALCULATOR\n\n\n\n";

    cout << "Please Select one of the following options\n"
        << "A for addition\n"
        << "S for subtraction\n"
        << "M for multiplication\n"
        << "D for division\n"
        << "C to convert a hexidecimal number to its decimal equivalent\n"
        << "H to convert a decimal number to its hexidecimal equivalent\n"
        << "Q to exit the program\n";

    cin >> choice;
    choice = toupper(choice);

    switch(choice)
    {
    case 'A':  
        {
            system("cls");
            return ADD;
            break;
        }
    case 'S':
        {
            system("cls");
            return SUBTRACT;
            break;
        }
    case 'M':
        {
            system("cls");
            return MULTIPLY;
            break;
        }
    case 'D':
        {
            system("cls");
            return DIVIDE;
            break;
        }
    case 'C':
        {
            system("cls");
            return HEX_TO_DECIMAL;
            break;
        }
    case 'H':
        {
            system("cls");
            return DECIMAL_TO_HEX;
            break;
        }
    case 'Q':
        {
            return QUIT;
            break;
        }
    default:
        {
            cout << "Im sorry, the option you selected is invalid.\n"
                << "Please try again.\n";
            system("pause");
            system("cls");
            GetMenuPick();
            break;
        }

    }
}

void OperationSelector(/*in*/ MenuType choice)
{
    while(choice != 'Q')
    {
        switch(choice)
        {
        case ADD:
            {
                cout << "adddition" << endl;
                system("pause");
                system("cls");
                choice = GetMenuPick();
                break;
            }
        case SUBTRACT:
            {
                cout << "subtract" << endl;
                system("pause");
                system("cls");
                choice = GetMenuPick();
                break;
            }
        case MULTIPLY:
            {
                cout << "subtract" << endl;
                system("pause");
                system("cls");
                choice = GetMenuPick();
                break;
            }
        case DIVIDE:
            {
                cout << "subtract" << endl;
                system("pause");
                system("cls");
                choice = GetMenuPick();
                break;
            }
        case HEX_TO_DECIMAL:
            {
                cout << "subtract" << endl;
                system("pause");
                system("cls");
                choice = GetMenuPick();
                break;
            }
        case DECIMAL_TO_HEX:
            {
                cout << "subtract" << endl;
                system("pause");
                system("cls");
                choice = GetMenuPick();
                break;
            }



        }
    }
}
void Add()
{
    int decimalhex1;
    int decimalhex2;
    int intsum;
    string stringhex1;
    string stringhex2;
    string stringsum;

    stringhex1 = GetHex();
    stringhex2 = GetHex();
    decimalhex1 = ConvertFromHex(stringhex1);
    decimalhex2 = ConvertFromHex(stringhex2);
    intsum = decimalhex1 + decimalhex2;
    stringsum = ConvertFromDecimal(intsum);

    cout << stringhex1 << " + " << stringhex2 << " =   " << stringsum << endl;
}
string GetHex()
{
    string hexnum;

    cout << "Enter a hexadecimal number:   " << endl;
    cin >> hexnum;
    return hexnum;
}
int ConvertFromHex(/*in*/ string hex1)
{
}
string ConvertFromDecimal(/*in*/ int num)
{

}





void Subtract();
void Multiply();
void Divide();
void ToDecimal();
void ToHex();


edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Read Each Character In A String
15 Oct, 2006 - 11:50 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Sorry to ask, but was this posted because you require help with the code? If so, can you please specify which portion of the code is not working as planned, and what the problem seems to be?
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: How To Read Each Character In A String
16 Oct, 2006 - 06:12 PM
Post #8

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
CODE

char szTest [512];

for (int x = 0; x < 512; x++)
{
cout << szTest [x]; //something like that..
}

User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/4/08 07:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month