Not understanding conversions...

There's two programs I'm stuck on for my intro to C++ class for the same reason. Every time I go to compile, I get an error about not being able to convert a variable to a string. In the first program, I need to be able to convert a word into numbers, or hex. The second one, I'm converting a time to a string, and I'm doing it like the book is suggesting to do it.

1
2
3
4
5
6
7
8
9
10
11
12
string AskTimeOut()
{
	int TimeOutHR, TimeOutMIN;
	char TimeOutCHAR;
	cout<<"Please enter the time out (HR:MIN): ";
	cin>>TimeOutHR>>TimeOutCHAR>>TimeOutMIN;
	cin.ignore();
	cout<<"\n";

	string TimeOut="TimeOutHR TimeOutCHAR TimeOutMIN";
	return TimeOut;
}


The whole pointer thing is confusing me as well. Although I kinda get it, I've gotten to the point where I'm guessing what I need to input in my code to make it work.

Any help is appreciated, and I realize that I'm being rather broad, but I don't want someone to do both assignments for me. If I need to be more specific, please say so.
For your first program, are you talking about trying to convert a string into hex?

For the second one, you cannot assign variables to a string like that, if you enclose them in " ", you are saying it is a constant string. You would have to set it equal to the first variable (without quotes) and then use .append() in order to append the other variables to it.
I tend to use stringstreams quite a lot. If you are using iostreams, it doesn't really add any significant overhead to use stringstreams too.
1
2
3
4
5
6
7
8
9
10
#include <sstream>
...
string AskTimeOut()
{
        ...

        ostringstream TimeOut;
        TimeOut << TimeOutHR << ':' << TimeOutMIN;
        return TimeOut.str();
}


Hope this helps.
I think I understand what I need to do in the second one for now, but I want to focus on the first one.

For the first one, I need to create a simple encoder. The the user inputs a string of all caps and numbers, then the number he wants to shift by, and then the code pops out. For example, if he puts in BOB24, shifts by 2, the outcome would be DQD46.

My problem is that I don't know how to convert the string inputted by the user to a decimal value so that I can add the user's shift to it. I've read over the chapter this is in, and I'm still confused by it.
You could put it into a string and then modify each character individually if you wanted to.
I don't have the slightest clue as to how I'd go about that. Could you explain it to me?
Last edited on
Or maybe someone could point out where I could learn how to do this? I just want to know how to change the characters values like this.
Well, if you are getting the data from them, you can just use cin (unless you want spaces, but I don't think you do). Then you can just create a temporary string to put the new data into, and iterate through the first string, checking to see what it is (using isalpha() or isdigit()), and then performing the desired operation. Once you get the new char, just use .push_back() to put the character into the new string.
I've never heard of isalpha() or isdigit(). I need this explained to me in more detail.
isalpha(char) returns true if the char given is in ['a', 'z'] or ['A', 'Z'] otherwise it returns false
isdigit(char) does the same bot checking if the char is in ['0', '1']

so:
1
2
3
4
5
6
7
8
9
isalpha('A') == true
isalpha('b') == true
isalpha('2') == false
isalpha('#') == false

isdigit('a') == false
isdigit('2') == true
isdigit('#') == false
isdigit( 2 ) == false // 2 != '2' 
Topic archived. No new replies allowed.