I'm trying to read a single letter from the beinging of a string that is generated from user input. Though it sounds childishly easy, I think I may seriously have a heart attack from trying to solve this problem. Example code follows:

#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;

int main()
{
  string user_input;
  string first_letter = user_input[0];

  getline(cin, user_input);
  cout << first_letter;

    return 0;
}

I get an error of "error: invalid conversion from 'char' to 'const char*'" from the compiler. I've tried mainipulating the values to be char array's what feels like 100 times, but I can't assign an array size because I don't know how much text the user is going to put in.

Any help is appreciated.

[EDIT]: I ought to note that whatever variable I assign to be the first letter from the user_input string has to be a string type, I can't use it in other parts of the program if it is a char type.

Recommended Answers

All 8 Replies

You have two problems. First, the first character of a string is a char, not a string, so your variable first_letter should be of type char, not string. Your code is not compiling because the string type's indexer returns a char. Second, your code won't work at runtime, and I'll leave that for you to figure out.

When you see a compile error like that, you can see that it's talking about different types used in the C++ language. The message might not be immediately clear, but what is clear is that you are using the wrong type somewhere, and this is confusing the compiler.

I know why its not compiling, what I can't figure out is how to fix it. The code I posted is just something I wrote really fast to demonstrate the problem, the real program I'm working on is an encryption program for a class, and the decryption side of it has to be able to take the user inputed encrypted message and run it through an antilogarithm. I'm only saying so that forum members have a better understanding of what I'm doing.

Anyway, the first character of the string is absolutely vital, without it the rest of the program might as well not even be there. What I need is away to assign just the first character to a string variable so that I can run it through some functions I have. Because I'm better familiar with python, this would be the python equivalent of what I need:

>>variable = "This is a var"
>>variable[0]
'T'
>>foo = variable[1]
>>foo
'h'

I hope this clears things up a little

I know why its not compiling, what I can't figure out is how to fix it.

I just told you how.

That it's come down to posting my problem on the forums means that I've done a lot of work on my own to solve this issue and that I can't. I know the problem is with assigning the variables the correct type, but I don't know how to pull a char from a string, and then keep or convert that char into a string type

#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;

int main()
{
  string user_input;

  getline(cin,user_input);
  char first = user_input[0];
  cout << "Frist character is  " << first << endl;

    return 0;
}

It works fine and well, but the variable first is of type char. How can I make this type string? If you have the answer, please show me.

Thank you, really. substr didn't just solve this problem, but gave me a far better solution then the one I had lined up for a second problem I was going to tackle. I have to say, I'm a embarrassed I didn't notice substr in all the times I read about the string class :x

Again, thanks for sticking with me through these posts.

commented: Thank you for being patient with my misreading. +1

Should be noted that to append a character to a string you can do this

std::string myString = "Hello, World";
myString.push_back('!');

Chris

Actually, you can't assign a char to a string, but you can add a char to a string. A minor change to you program above:

int main()
{
  string user_input;
  string first = "";
  
  getline(cin,user_input);
  first += user_input[0];
  cout << "Frist character is  " << first << endl;

    return 0;
}

seems to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.