how do i convert string to char

I am trying to take a word and only use the first letter, and then after the letter is converted i have another input inquire, but the compiler skips it. my educated guess is that might be because the letters that are after the first letter i took from string are still there. Also is there a better way to convert string to char?

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
cout << "\nProject #3: Basic Operations: Sum, Rest, Multiplication, Division\n";
    
    int inp = 0, Amount;
string Operation;
char choice;
    
    cout << "Which Operation you want to use ?\n";
    cin >> Operation;
    char *y = new char[Operation.length() + 1]; 
    
    strcpy(y, Operation.c_str());
    
    choice = toupper(choice); // Convert to uppercase (needs #include <cctype>)
    while (choice != 'S' && choice != 'R' && choice != 'M' && choice != 'D')
    {
        cout << "That's not one of the options, try again: ";
        cin >> choice;
        choice = toupper(choice);
    }
    
    if (choice == 'R')
    {
        cout << "How many values you gonna use to Operate ?\n[Ex: 12, 4, 69 (4 Values will be used to Operate)]\n";
        cin >> Amount;
        cout << "Now enter the values: ";
    }

    delete[] y;
Last edited on
Line 4: You haven't shown the declaration of choice. Is it a string or a char?

Line 5: Operation is an empty string. So operation.length() is 0. The new operation will allocate only 1 character.

Line 8: Not clear what the purpose of the strcpy is since you delete y at line 9.

Line 21: Your example is inconsistent. You list 3 values, but say 4 will be used.
My bad i didn't realize that since it is a long code i only copied and pasted it. I just want to find a way where the user inputs a word and i either convert the string to char or use the first letter but with out causing anything to the code. for example i was testing "rest" but it just skips line 24 which is collecting a int
Line 4: choice is an uninitialized variable (garbage).

Line 14, 21: You test the uninitialized variable. Line lines 14 and 21 are pretty much guaranteed to be false since choice contains garbage.


There is a very easy way to do this but in order to do it there is a library you should have. By any chance, are you using Visual Studio 2015? Knowing this information would be very helpful.
abstractionAnon what should i do instead? use only char since the beginning? I do not want to prompt the user telling them to enter only the first letter, i want them to enter the full word and me take the first letter and ignore or erase the rest.

Sylvagon nope I'm using Xcode, but which library is it? maybe i can find alternatives.
Why don't you just take the first char of the string ?

1
2
3
4
5
6
7
string Operation;
char choice;
    
cout << "Which Operation you want to use ?\n";
cin >> Operation;
if (Operation.size() > 0)
   choice = toupper(Operation[0]);
I am unsure of the certain library, however I do know that for me, I use Visual Studio Community 2015, and a library comes pre-installed. The library seems to have been made by microsoft, but I will see if I can find it or alternatives. It could be possible that you do not need to install anything extra, but instead you need to use the namespace: "System". (Yes, the "S" is capital). For me, if I wanted to convert string to char or vise versa, I would use a command using Console::Converto. However that command is in the system namespace. My question is, why do you even have to make it a char? Can you not just keep it as a string?

For example:

1
2
3
4
5
6
//This string will be the string of the first letter of the word
string letter;

if (letter == "R") {
     //In here just type what you need
}

Thomas1965's idea was good so i used that one but yea i kinda figure i can do that. But you mean...

1
2
3
4
5
string letter;

if (letter[0] == 'R') {
     //In here just type what you need
} 


Thanks man thats a great way to use it too.
Yeah, sorry I didn't pay attention to the thing that Thomas1965 said, but yes you would be correct in that.
Topic archived. No new replies allowed.