C++ help

Im trying to create a code program but am being stopped because Xcode will not let me use the line length=word.length.The error message is Implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'int'. The line of code is


string Encode(string word) {

int length = word.length();

string theSum = "";

for (int i = 0; i < length; i++) {

if (theSum[i] == 'a') {

word += 1-1;

}

There is more, but it is really long. The same error is occuring with all my lines of code with the words 'length=word.length. Please advise! All help is welcome.

If you are basing all of that logic on string length, then why not make the type the same? use "string::size_type" instead of "int". Or perhaps look up string iterators and algorithms.

I tried that, but it was an error as well. how would I fix this line of code:


string Encode(string word) {

int length = word.length();


string theSum = "";



for (int i = 0; i < length; i++) {

if (theSum[i] == 'a') {

word += 1-1;

}

so it encorperates string::size_type? Please let me know. you are a huge help

Instead of using "int length = work.length()", use "typename string::size_type length = word.length()". Or, if you think the length of the string will fit with the size of a SIGNED integer, you can use "int length = int(work.length())" or "int length = (int)work.length()". The size of a std::string is a std::size_t which is an "unsigned long" on MacOSX (64-bit integer). An int is a 32-bit SIGNED integer in the new C++/C conventions (a "long" or "unsigned long" is defined to be size of an integer type that can hold a pointer, which is 64-bits on most machines today. Unsigned long long is defined to be an integer type whose length is at least 64-bits, could be greater). You might want to consider using an "unsigned int" if you are concerned about size of the variable since sizes are always >= 0.


Actually, I would use the "auto" keyword, as in "auto length = word.length()", and "for (auto i=0; i<length; ++i)". Let the compiler figure it out (C++11 or greater, though).


Jonathan

Thank you Jonathan! Unfortunatly, i have incountered an error when running the program. The error is:


Do you want to encode or decode a message?

a. Encode

b. Decode

a

You have chosen to Encrypt. What do you want to encode:

h

Encrypted Message: h

sh: pause: command not found

Program ended with exit code: 0

I'm fairly new to C++, so could you try to use basic coding language so I may understand it better? if you want, I will send you the entire code so you can help.

change "int length" to "long length".

>> I'm fairly new to C++


Well, apparently you are new to programming generally, since you don't seem to have any familiarity with the type system. Without that, you will not be able to get very far (as you've already discovered). I suggest you go back to some basic C++ tutorials, ones that deal with the numeric types, until things click into place and you can proceed on your own.

That did not work. Anything Else? I know I am new to programming, but I did create that entire program, which just leaves that one error message to deal with. The lines of code i was wondering about are:


string Encode(string word) {

long length = word.length();

string theSum = "";

for (auto i = 0; i < length; i++) {

if (theSum[i] == 'a') {

word += 1-1;

}

else if (theSum[i] == 'b')

word += 1-2;

else if (theSum[i] == 'c')

word += 1-3;

I was wondering if I would get a different result if I changed it from word += to word.append?

All yof your help has been so helpful. I am sorry that I am so ignorent about programming.

Oh my goodness!!!! Thank you so much guys!!! I got it to work! Thank you to PBK, jonprescott, QuinceyMorris, and John Daniel for all your help. Thank you once again!!!!!

i think "auto" will cause some compilers to complain unless you set the correct flags.

C&#43;&#43; help
 
 
Q