control skipping over switch statement, and following code within function.

This is baffling me.

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
inline void upper(char & ch)
{
    cout << "ok in upper ch = " << ch << endl;
    //exit(43);
    switch(ch)
        {cout << "!!!!!!!!!!!!!!!!!!";
            exit(44);
            cout << "wtf\n";
        exit(99);
            case a_accent:cout << "here " << endl;
                return A_accent;
            case e_accent:
                return E_accent;
            case i_accent:
                return I_accent;
            case o_accent:
                return O_accent;
            case u_accent:
                return U_accent;
            case sset:
                return sset;
            case a_umlaut:
                return A_UMLAUT;
            case o_umlaut:
                return O_UMLAUT;
            case u_umlaut:
                return U_UMLAUT;
            default:
                return toupper(ch);
                break;
        }
    exit(45);
    cout << "at end ch = " << ch << endl; exit(42);
}


I get "Ok in upper" printed on my screen, but the program never exits returning 44 or 45.

It is being called from this function:
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
inline void upperize(char * ch)
{
    int i = 0;
    int size = sizeof ch;
    int cz = 0;
    cout << "size = " << size << endl;
    while (cz <= size)
    //while (ch[++i] != NULL)
    //while (ch[++i] != " ")
        {
            //cout << "cz = " << cz << endl;
            //cout << "upper(ch) = " << upper(ch) << endl;
            //ch[i] = upper(ch);  //i think this is correct.
            cout << "this ch = " << ch[i] << endl;
            cout << "doing stuff to it: \n";
            upper(ch[i]);
            cout << "after did stuff to it ch = " << ch[i] << endl;
            ++cz; ++i;
        }

    //return ch[0];
    cout << "at end of upperize() " << ch << endl;
    //return ch;
    return;
}


edit: never mind for now... i forgot i changed the return value to void.
Last edited on
You have unreachable code in the upper function
well I took it out, but it still doesn't not seem to want to capitalize my chars.
It won't.
toupper does not change the parameter passed to it. It returns the uppercase as the return value.

So in your uppersize function you have upper(ch[i]); which will have no effect on ch[i] as the code stands.

if you want to you can leave that line as it is and change the upper function to
ch = toupper(ch); //you can forget about the return and make it a void function now
because ch was passed by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
inline void upper(char & ch)
{
    switch(ch)
    {

    //all those we don't want touched in any way
    case a_accent:
    case e_accent:
    case i_accent:
    case o_accent:
    case u_accent:
    case sset:
    case a_umlaut:
    case o_umlaut:
    case u_umlaut:
    break;

    //all others - change case if needed
    default:
    ch = toupper(ch);
    break;
    }
}
Last edited on
I was thinking it had something to do with the parameters being passed to upperize. because when i try to change its value i get a problem.

Why won't this work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
inline void upperize(char ch[])
{
    char temp;
    int i = 0;
    int size = sizeof ch;
    int cz = 0;
    cout << "size = " << size << endl;
    while (ch[++i] != NULL)
        {
            temp = ch[i];
            cout << "before temp = " << temp << endl;
            temp = toupper(ch[i]);
            cout << "after its = " << temp << endl;
            ch[i] = temp;

        }

    //return ch[0];
    cout << "at end of upperize() " << ch << endl;
    //return ch;
    return;
}


Temp is being assigned correctly, but now when i try to change ch it cashes.
can you pass pointers by reference? because if so I think thats what I want to do.
Are you trying to modify a string literal???????
because upperize("hello") will certainly crash because literals are read only.

Also this bit won't work the way you think (you will miss out ch[0]).
1
2
3
    while (ch[++i] != NULL)
        {
            temp = ch[i];
no i'm not, and I went ahead and changed ++i to i++

I just want to know why I can't
1
2
char * test = "fuck";
upperize(test);


the upperize function can't modify its parameters
no - read only
for that matter I can't seem to manually modify anything.

this code is causing me to crash:

1
2
3
4
5
6
7
8
9
const char yada[33] = { "yadayadayada"};
int main()
{
    char * copy;
    copy = new (nothrow) char[33]; 
    copy = yada; 
    upperize(copy);  //I can't do this unless upperize doesn't do nothing to copy.
    //I can't even do the following:
    copy[0] = "A"; 


what gives?
Last edited on
yada is a constant char array.

try instead :
1
2
3
4
5
6
7
8
 
char yada[33] = { "yadayadayada"};

int main()
{
    
    upperize(yada);
 



ok i think i figured it out...

any reason why I can't declare char yada[33] as constant?
If it is constant, then you can't modify it.
copy = yada;

This doesn't copy what's in yada to the memory pointed to by copy. It makes copy point to yada (causing a memory leak, too). That's why you need functions such as strcpy().
Last edited on
Topic archived. No new replies allowed.