string.get(char)

Pages: 12
I am trying to take a string and grab each character from it so i can observe it. Doesn't seem to be working. Is there another way to accomplish what i am trying to do?

 
string.get(char)


Use the [] operator or .at()
I haven't learned those operators yet. Could you give an example of how they are used? Thanks.
Also, you might use a string::iterator. Here's a long winded example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string::iterator it;		// The string iterator.
string wordString;

for (it= wordString.begin(); it != wordString.end(); it++)
{		

  if ((*it) == '\n')							
	it = wordString.erase (it);	

  if ((*it) == '!')
	it = wordString.erase (it);

  if ((*it) == '?')
	it = wordString.erase (it);

  if ((*it) == ',')
	it = wordString.erase (it);
}

// Or . . .
for (int n = 0; n != wordString.end(); n++)
{
    cout << wordString[n] << endl;
}
Last edited on
1
2
3
4
5
string MyString("123456");
MyString.at(2); //gets '2'
MyString[2]; //also gets '2'
MyString.at(99); //gives an error
MyString[99]; //doesn't 
Thanks I think that should work. I might have to create a counter to make it grab each one 1by1 where i thought if i put the string.get(char) in a loop i would be able to grab it in each iteration.
Okay so i'm confused now. Heres what I am trying to do. I need to take roman numerals and convert them into a decimal number.

I was thinking about using the MyString.at() function... But I need to assign it to a character, so i dont think that will work. Unless I can say char = 'MyString.at()' or something like that.
Last edited on
i used the .at function and now its crashing with a runtime error. Can someone explain why this is happening/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void convertNumeral(/*in*/ string numeral,
		            /*out*/ int& decimal)

{

	char singleNumeral;
	int iterateCount;

	singleNumeral = numeral.at(iterateCount);
	
    while(singleNumeral != ' ')
    {
                        
		if(singleNumeral == 'M')
			decimal = decimal + 1000;
		if(singleNumeral == 'D')
			decimal = decimal + 500;
		if(singleNumeral == 'C')
			decimal = decimal + 100;
		if(singleNumeral == 'L')
			decimal = decimal + 50;
		if(singleNumeral == 'X')
			decimal = decimal + 10;
		if(singleNumeral == 'V')
			decimal = decimal + 5;
		if(singleNumeral == 'I')
			decimal = decimal + 1;
		
        iterateCount++;	
		singleNumeral = numeral.at(iterateCount);

	}
	
}
    
You never initialize iterateCount, so it is some random unknown value that is probably not what you want.
I will try that. Thanks. The .at function worked perfect.
Still getting the runtime error. I just assumed the itearateCount was working because when i ran it with a test cout, it output the correct character.
Maybe it will work better like this; its just a guess . . .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string numeral;
int iterateCount = 0;
string::iterator it;	

for (it= numeral.begin(); it != numeral.end(); it++)
{	
  if ((*it) == ' ')	
     it = numeral.end();

  if ((*it) == 'M')							
	decimal = decimal + 1000;

  singleNumeral = numeral [iterateCount]; 
  iterateCount++;	
}


I know I use the for loop probably too often but maybe this will help. I am a beginner so there's a better way, but I want you to find your solution or at least something that works.
Good luck.
Last edited on
Thanks for the possible solution angelGithara23, but i dont understand the 'for' loop or string iterator at the moment. i've grasped the while loop and would like to use it and not jump to functions that i don't understand or haven't gotten to. If you can come up with a solution with a while() loop, i would love to see it.
Last edited on
Make sure you are not going out of bounds on the vector...If you have the new code I could check that too.

@Angel: That would work fine, and you wouldn't need singleNumber or interateCount in that case actually.
Last edited on
@ vince: well, If you are uneducated on the language, with all respect, vince, I would hope that you would learn more about control structures before jumping into a more complicated thing like functions, because control structures are a logical constituent of functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	int decimal = 0;
	string numeral = "IV", stringTemp = ""; 
	int iterateCount = 0;
	string::iterator it;	

	for (it= numeral.begin(); it != numeral.end(); it++)
	{	
	  iterateCount++;

	  if ((*it) == ' ')	
		  it = numeral.end();
	  else  
		  stringTemp = stringTemp + *it;	
	  
	  if (iterateCount == 2)
	  {
	     if(stringTemp == "IV")
		    decimal = 4;
	  }
	  else if(stringTemp == "V")
			decimal = decimal + 5;
	  else if(stringTemp == "I")
			decimal = decimal + 1;
	 	
	}

	cout << decimal;

	cin.get();
	return 0;
}

Here this should work once you have studied for loops a little, and look at the easy to use STL reference at this excellent site to learn more about the useful string::iterator. And if you have difficulties figuring out the rest just ask more questions (or PM me) and I'll try to help figure some of that out with you.

@ firedraco: Are you saying I can get a int count from a string::iterator? How? That would actually help me to know that. Oh, I get what you're saying now, duh.
Last edited on
You can't, it's just you don't need to have those other variables. (*it) will give you whatever it is pointing at without needing to have an int index;.
I cant figure it out. please take a look and try my existing code and help debug it i would appreciate it. I think it has to do with the (singleNumeral != ' ') loop with the string. it seems to get 2 good iterations with a 3 character roman numeral and crashes...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//Roman Numeral Calculator
// vincent 

#include <iostream>
#include <cstdlib>

using namespace std;

void convertNumeral( string, int&);
void convertDecimalPrint(int);
void calculateSum(int, char, int, int&);

int main()
{
    string numeral, numeral1, numeral2;
    int value1, value2, decimalValue, decimalSum;
    char arithmetic;
    
    cout << "This program will calculate 2 roman numeral numbers" << endl
         << "and print out the decimal values and sum in roman numerals." << endl << endl;
    cout << "Input first roman numeral number: ";
    cin >> numeral;
    cout << endl << numeral;
    
    numeral1 = numeral;
    
    convertNumeral(numeral, decimalValue);
    
    cout << "test";
    
    value1 = decimalValue; 
    
    cout << endl << "The first number is " << value1 << endl;
    
    cout << "Input second roman numeral number: ";
    cin >> numeral;
    cout << endl << numeral;
    
    numeral2 = numeral;
    
    convertNumeral(numeral, decimalValue);
    
    value2 = decimalValue;
    
    cout << endl << "The second number is " << value2 << endl;
    
    cout << "Enter the desired arithmetic operation : ";
    cin >> arithmetic;
    cout << endl << arithmetic;
    
    calculateSum(value1, arithmetic, value2, decimalSum);
    
    cout << numeral1 << " " << arithmetic << " " << numeral2 << " = ";
    
    convertDecimalPrint(decimalSum);
    
    cout << " (" << decimalSum << ")";
    
    system("pause");
    return 0;
    
}
    
    
    
//**************************************************************************
// convert numeral to decimal number


void convertNumeral(/*in*/ string numeral,
		            /*out*/ int& decimal)

{

	char singleNumeral;
	int iterateCount;
	
	iterateCount = 0; //  integer to make .at function go to next character
	decimal = 0;

	singleNumeral = numeral.at(iterateCount);
	
    while(singleNumeral != ' ')
    {
        cout << decimal;
        cout << singleNumeral;               
		if(singleNumeral == 'M')
			decimal = decimal + 1000;
		if(singleNumeral == 'D')
			decimal = decimal + 500;
		if(singleNumeral == 'C')
			decimal = decimal + 100;
		if(singleNumeral == 'L')
			decimal = decimal + 50;
		if(singleNumeral == 'X')
			decimal = decimal + 10;
		if(singleNumeral == 'V')
			decimal = decimal + 5;
		if(singleNumeral == 'I')
			decimal = decimal + 1;
		
        iterateCount++;	
		singleNumeral = numeral.at(iterateCount);
        cout << decimal;
	}
	
}
    


//***************************************************************************
// convert decimal number to numeral (prints out)

void convertDecimalPrint(/*in*/ int decimalSum)
{
	while(decimalSum >= 1000)
		{
		cout << "M";
		decimalSum = decimalSum - 1000;
		}
	while(decimalSum >= 500)
		{
		cout << "D";
		decimalSum = decimalSum - 500;
		}
	while(decimalSum >= 100)
		{
		cout << "C";
		decimalSum = decimalSum - 100;
		}
	while(decimalSum >= 50)
		{
		cout << "L";
		decimalSum = decimalSum - 50;
		}
	while(decimalSum >= 10)
		{
		cout << "X";
		decimalSum = decimalSum - 10;
		}
	while(decimalSum >= 5)
		{
		cout << "V";
		decimalSum = decimalSum - 5;
		}
	while(decimalSum >= 1)
		{
		cout << "I";
		decimalSum = decimalSum - 1;
		}
		
}
		
//*****************************************************************************
// calculate the numbers

void calculateSum(int value1,
                  char arithmetic,
                  int value2,
                  int& decimalSum)
                          
{
                          if(arithmetic == '+')
                          decimalSum = value1 + value2;
                          if(arithmetic == '-')
                          decimalSum = value1 - value2;
                          if(arithmetic == '*')
                          decimalSum = value1 * value2;
                          if(arithmetic == '/')
                          decimalSum = value1 / value2;
}
Last edited on
I see the problem. If you simply type in something like "XI" then hit the Enter key, there is no space. In this case I would suggest using getline() the get the data, then iterating over the string with respect to it's size as opposed to whether you find a space.

For getline():
http://www.cplusplus.com/forum/articles/6046/
I dont want to sound naive or doubtful about getline(), but assuming that a user doesn't know that they have to input a following space with getline() and they just input a standard roman numeral without a following space. wouldn't it still do the same thing as cin? maybe concatenate a space onto the user input?
It worked! concatenating the numeral variable with numeral = numeral + " "; worked. Thanks for the help guys!
Pages: 12