7 Replies - 11589 Views - Last Post: 16 October 2006 - 07:12 PM Rate Topic: -----

#1 Umbrella  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 13-October 06

How to read each character in a string

Posted 14 October 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
Is This A Good Question/Topic? 0
  • +

Replies To: How to read each character in a string

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: How to read each character in a string

Posted 14 October 2006 - 09:13 PM

View PostUmbrella, on 14 Oct, 2006 - 08:16 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#3 Umbrella  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 13-October 06

Re: How to read each character in a string

Posted 15 October 2006 - 10:20 AM

I dont know if any of those really help solve my problem. Im still having trouble.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to read each character in a string

Posted 15 October 2006 - 10:36 AM

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.
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: How to read each character in a string

Posted 15 October 2006 - 10:42 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 Umbrella  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 13-October 06

Re: How to read each character in a string

Posted 15 October 2006 - 10:58 AM

//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
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: How to read each character in a string

Posted 15 October 2006 - 12:50 PM

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?
Was This Post Helpful? 0
  • +
  • -

#8 violent_crimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 06

Re: How to read each character in a string

Posted 16 October 2006 - 07:12 PM

char szTest [512];

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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1