letter counter

in a string how do you put counters on specific letter for example i input VXXI
and the computer counts v as 1, x as 2 and I as 1 .
Last edited on
sorry about the repost
what exactly are you trying to do?
I'm trying to create a program that follows the one of the rules of roman numeral

There can never be more than 4 M’s, 1 D, 4 C’s, 1 L, 4 X’s, 1 V, or 4 I’s because then the representation is not minimal (e.g. 5 X’s can be written as L).


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


int main()
{
	string myInput;
	int value;
	cout << "Please enter the Roman Numeral to convert : ";
	cin >> myInput;
	
	
	
	cout << "Roman Number " << myInput << " equals " << value <<endl;


return 0;	
}


note: this code is still incomplete and i'm still working on it
basically I'm trying to do this

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
#include<iostream>
using namespace std;

int main()
{
	int sum, counter, max_num;
	
	cout << "Enter an integer between 1 and 200: ";
	cin >> max_num;
	
	cout << endl << endl;
	sum = 0;
	counter = 1;
	
	while ( counter <= max_num)
	{
		cout << "This is step: " << counter << endl << endl;
		sum = sum + counter;
		counter = counter+1; //increment the counter
	}
	
	cout << "The sum is " << sum << endl;
	
	return 0;
}


but with letters
You want to count the number of times a letter appears in a string. The program you show above is completely unrelated to this task.

Look up std::map - I would recommend using a std::map<char, int> to map each character to the number of times it appears. All you need to do is increment the count for the character as you loop through the string.
http://www.cplusplus.com/reference/map/map/
http://en.cppreference.com/w/cpp/container/map
I got it to count the letters however i cant's the maximum of each letter to 4

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

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string roman, Q;
	bool not_done;
	not_done = true;

    int length = roman.length(); //length of string
    int i = 0;
    int I_count = 0; 
    int X_count = 0; 
    int L_count = 0; 
    int C_count = 0;
    int V_count = 0;
    int M_count = 0;
      
    

	cout << "Enter a roman: ";
	cin >> roman;

	while (not_done) 
	{
	    while(i<length)
	    {
		    if (roman.at(i) == 'I' )
            {
                I_count = I_count + 1;
                i = i + 1;
            }
            if (roman.at(i) == 'X' )
            {
                X_count = X_count + 1;
                i = i + 1;
            }
            if (roman.at(i) == 'V' )
            {
                V_count = V_count + 1;
                i = i + 1;
            }
                if (roman.at(i) == 'C' )
            {
                C_count = C_count + 1;
                i = i + 1;
            }
                if (roman.at(i) == 'M' )
            {
                M_count = M_count + 1;
                i = i + 1;
            }
             if (roman.at(i) == 'L' )
            {
                L_count = L_count + 1;
                i = i + 1;
            }
            
	    }
	  
	  cout << roman <<" is equal to " <<" (enterQ to quit) " <<endl;
	  cin >> roman;
	  
	  if (roman == 999)
	  {
	      not_done=false;
	  }
	
    }
	return 0;
}




note: still incomplete code and it won't leave loop
Last edited on
Why are you using a while-loop instead of a for-loop?
letter counter by <map>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//letter counter
#include <iostream>
#include <map>
using namespace std;

int main() {	

  string s;
  map<char, int> letters;   

  cout << "enter a character string: ";
  getline(cin, s);    
  
  for(auto &x: s){
  	x= tolower(x);
  	++letters[x]; // map operator[]
  }
  
  for(auto x: letters){
  	cout << x.first << " " << x.second << "\n";
  }
  
return 0;	
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main(){
	std::string s = "abcdbhbhab";	
	for(int i=0; i<s.size(); i++ ){
		int n=0;
		for(int j=0; j<s.size(); j++){
			if(s[i]==s[j]) {
				if(i>j)	break;
				n++;
			}					
		}
		if(n) std::cout << s[i] << " " << n << " times\n";	
	}		
return 0;		
}
how do i set specific character such as I,X,L,V,C,M to 4 so the user can't input any of these character more than four times
Topic archived. No new replies allowed.