New Reply

Issue with string::operator[]

 
Share Thread Thread Tools
Jun26-12, 02:30 AM   #1
 
Blog Entries: 1

Issue with string::operator[]


#include <iostream>
#include <string>

using namespace std;

void str2hex(string str) {

int strlen = str.length(); // Assign the length of input/str to strlen
string hex[strlen]; // Initialize the Array hex with the length of the string


for (int x = 0; x < strlen; x++){ // Create a for loop assigning the values of the string to hex as hexadecimal values.


if (str[x] == " "){

hex[x] = 20;

}

}

}

int main()
{

string input; // Declaring the string which will be converted into HEX
getline(cin,input); // Gets the user's input for the string
str2hex(input); // Inserts the user's input into the str2hex function

return 0;
}




In this program the issue is occuring on the line with if(str[x] == " ") {

the error i'm getting is: comparison with string literal results in unspecified behaviour
I can't figure out what is wrong. I am new to C++ so please don't scold me. Thank - you.
 
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Front-row seats to climate change
>> Attacking MRSA with metals from antibacterial clays
>> New formula invented for microscope viewing, substitutes for federally controlled drug
Jun26-12, 02:51 AM   #2
 
Admin
Isn't str[x] a character? If so, you should compare it to a character ' ', not a string " ".
 
Jun26-12, 02:54 AM   #3
 
Blog Entries: 1
Thank you very much! Works perfectly! I'm such a novice -.-
 
New Reply
Thread Tools


Similar Threads for: Issue with string::operator[]
Thread Forum Replies
Commutator of density operator with kinetic energy operator Advanced Physics Homework 0
commutator of square angular momentum operator and position operator Advanced Physics Homework 5
Show operator can be an eigenfunction of another operator given commutation relation Advanced Physics Homework 9
Eigenfunctions of translation operator and transposed operator property proof Calculus & Beyond Homework 1
Divergent sum in zero mode Virasoro Operator of bosonic String Theory Beyond the Standard Model 0