toUpper issues

Hello! I am a beginner at C++ and have to write a program that takes a Roman Numeral input and then converts it regular numbers. Currently I am trying to use toUpper (which I have no experience with) to make all of the input convert to uppercase so that it is easier to parse, validate, etc...For example, if the user inputs "iii" it should convert to "III".

The problem I am getting is that it is only storing the first character in the string...any suggestions? Here is a snipet of my code

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

#include <iostream>
#include <string>
#include <locale>

using namespace std;

class RomanNumeral
{
    
private:
    
    string m_inputString;
public:
    
    // Constructor
    RomanNumeral(string input)
    {
        m_inputString = input;
    }
   
    // Member function to convert m_inputString to all upper case letters
    void convertUp ()
    {
        locale loc;
        for (int i=0; i < m_inputString.length(); i++)
        {
           m_inputString = toupper(m_inputString[i], loc);
        }
        
        // Statement to test that m_inputString is now uppercase
        cout << "string is: " << m_inputString;
    }

int main()
{
    string inputString;
    
    cout << "Please input your roman numeral string to conver: " << endl;
    getline(cin, inputString);
    
    // Constructor call
    RomanNumeral romanNumeral(inputString);
    
    romanNumeral.convertUp();
http://stackoverflow.com/a/313990/1959975
You can do the same but with ::toupper ;)
@LB - Brilliant! Thanks so much. That worked out well.

For the sake of knowledge however - if I wanted to convert that using toupper without using #include <algorithm>...what would be the proper way to do it?
The proper way is to use <algorithm>, but I'm assuming you meant 'how do I reinvent the wheel'. Basically you would just loop over each character in the string and apply toupper to it. This means you assign a new value to the character, which is the return value of calling toupper with the character as a parameter.
@LB thanks again! That makes sense. I see that it is way more effective to just use <algorithm>. Now I have a question about parsing a string for my validation in my program but I will open another post for that since it's not related to toupper.
closed account (Dy7SLyTq)
bonus challenge: write your own toupper function. hint: it requires converting chars to their ascii values
@DTScode: your bonus challenge is impossible, not to mention the hint is misleading.
closed account (Dy7SLyTq)
no its not. not the c function
hint: it requires converting chars to their ascii values

No it doesn't. It requires two comparisons and an addition (well, a subtraction).
DTSCode wrote:
no its not. not the c function
I mean it's not possible to make it portable. You can write non-portable code that works on *most* systems, but I don't consider that a solution ;)
closed account (Dy7SLyTq)
well your just nitpicking but i guess i do that too often to compplain
Topic archived. No new replies allowed.