Join 149,540 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,511 people online right now. Registration is fast and FREE... Join Now!
I'm having trouble checking the value of a character that I get out of an overloaded cin function:
here's my function:
CODE
//Overload >> for input istream & operator>>(istream & in, ComplexNumber & n) { int afterStart = 0;
char realStream[30]; char imagStream[30];
char thisChar = 'X';
//Hold the input stream char input[64];
//fill the input stream character array with the input in.getline(input, 64);
for (int i = 0; i < 64; i++) { thisChar = input[i];
if ((thisChar == '+')||(thisChar == '-')) { for (int before = 0; before < (i-1); before++) { realStream[before] = input[before]; } for (int after = (i+2); after < 64; after++) { char imagChar = imagStream[afterStart];
if(imagChar == 'i') <=============== This line is causing issues. { break; }
The indicated line is where I'm having problems. Right now, as I have it (if imagChar == 'i') the program will compile, but won't actually break in from the for loop (or execute any code within the if statement). If I change the statement to (if imageChar == "i") it throws an "error: ISO C++ forbids comparison between pointer and integer" error.
Can anyone explain to me why this is happening, or how I could get the desired effect? (I think it's pretty obvious, I want the for loop to stop copying characters into the imagStream array whenever it encounters an 'i' character.)
Thanks!
This post has been edited by lwnexgen: 22 Nov, 2007 - 05:09 PM
Th line is technically correct as if (imagChar == 'i') { break; } if imagChar == 'i' it will break out of the current for loop. Problem is, I don't think that you initialize imagStream before you use it... Basically if that break if never happening, that means that you are never getting imagChar == 'i'
Th line is technically correct as if (imagChar == 'i') { break; } if imagChar == 'i' it will break out of the current for loop. Problem is, I don't think that you initialize imagStream before you use it... Basically if that break if never happening, that means that you are never getting imagChar == 'i'
Well, that's true for the first run through. However, if i put output statements to output the values of the elements in imagChar within the loop, it definitely outputs an i.
Also, if you look at the end of the program, i have an if statement to check whether the sign character is a '-', and my syntax doesn't work there either.
Well, that's true for the first run through. However, if i put output statements to output the values of the elements in imagChar within the loop, it definitely outputs an i.
Lets think about this... when the loop start imagStream is an uninitialized array, and afterStart == 0...
CODE
for (int after = (i+2); after < 64; after++) { char imagChar = imagStream[afterStart];
if(imagChar == 'i') <=============== This line is causing issues. { break; }
afterStart++; //we increase afterStart, so imagStream[afterStart] is again uninitialized... }
BUT this loop is inside a loop, SO, on the NEXT iteration of the outer loop... well, afterStart is not reset, so the problem continues...
So the question is, what is your goal... I think it is to take a string like "123.456 + i567.89" and break it into two strings "123.456" and "567.89" and the operation in the middle '+'? So let me look into that a bit....
Well, that's true for the first run through. However, if i put output statements to output the values of the elements in imagChar within the loop, it definitely outputs an i.
Lets think about this... when the loop start imagStream is an uninitialized array, and afterStart == 0...
CODE
for (int after = (i+2); after < 64; after++) { char imagChar = imagStream[afterStart];
if(imagChar == 'i') <=============== This line is causing issues. { break; }
afterStart++; //we increase afterStart, so imagStream[afterStart] is again uninitialized... }
BUT this loop is inside a loop, SO, on the NEXT iteration of the outer loop... well, afterStart is not reset, so the problem continues...
So the question is, what is your goal... I think it is to take a string like "123.456 + i567.89" and break it into two strings "123.456" and "567.89" and the operation in the middle '+'? So let me look into that a bit....
Hmm, well I fixed the issue. It turns out it doesn't really matter whether i kill the new string at the i or not...atof auto-parses it to a double as-is, and it turns out my "this char" was getting reassigned, so i just have my program kick the value of the sign out to a second variable and parse off of that, so I'm all good. It'd be nice to know why exactly this isn't working, but I'll worry about that later, as the program is due tomorrow.
Now, any ideas on why the hell my private variables "real" and "imag" (as set by the header file, which i can't change) aren't showing up as accessible by any of my other operators? (ie "+=")
int getRealNumber(char *in, char *out); int getImaginaryNumber(char *in, char *out);
int main() { char inBuffer[256]; char realPart[256]; char imaginaryPart[256];
int realLen = 0; double real = 0; double imaginary = 0;
cout << "Input a complex number ###.### +/- i###.###: "; cin.getline(inBuffer, 255); //remeber to leave room for 0x00 char at end of string if (realLen = getRealNumber(inBuffer, realPart)) { real = atof(realPart); if (getImaginaryNumber(inBuffer + realLen, imaginaryPart)) { imaginary = atof(imaginaryPart); } }
//returns the number of characters moved over to out. int getRealNumber(char *in, char *out) { int retVal = 0; char *ptr = in; int hasDecimal = false; while (*ptr == 0x20 || *ptr == 0x09) { ptr++; } //ignore initial white space //do we even have a valid initilal char [+-0123456789.] if (isdigit(*ptr) || *ptr == '.' || *ptr=='-' || *ptr == '+') { //The will add a sign into our output if one was present if (*ptr=='+' || *ptr=='-') { *out = *ptr; //copy the sign to the output out++; retVal++; //we added a char to the output... ptr++; //advance past the sign; while (*ptr == 0x20 || *ptr == 0x09) { ptr++; } //ignore any white space }
while (isdigit(*ptr) || *ptr == '.') { if (*ptr=='.') { if (!hasDecimal) { *out = *ptr; out++; retVal++; hasDecimal=true; } else { //Error number as an extra decimal point in it?? } } if (isdigit(*ptr)) { *out = *ptr; out++; retVal++; } ptr++; } } *out=0x00; //zero terminate the string. return retVal; }
//returns the number of characters moved over to out. int getImaginaryNumber(char *in, char *out) { int retVal = 0; char *ptr = in; int hasDecimal = false; while (*ptr == 0x20 || *ptr == 0x09) { ptr++; } //ignore initial white space
//do we even have a valid initilal char [+-0123456789.iI] if (isdigit(*ptr) || *ptr == '.' || *ptr=='-' || *ptr == '+' || *ptr=='i' || *ptr=='I') { //The will add a sign into our output if one was present if (*ptr=='+' || *ptr=='-') { *out = *ptr; //copy the sign to the output out++; retVal++; //we added a char to the output... ptr++; //advance past the sign; while (*ptr == 0x20 || *ptr == 0x09) { ptr++; } //ignore any white space }
if (*ptr=='i' || *ptr == 'I') { ptr++; while (*ptr == 0x20 || *ptr == 0x09) { ptr++; } //ignore any white space }
while (isdigit(*ptr) || *ptr == '.') { if (*ptr=='.') { if (!hasDecimal) { *out = *ptr; out++; retVal++; hasDecimal=true; } else { //Error number as an extra decimal point in it?? } } if (isdigit(*ptr)) { *out = *ptr; out++; retVal++; } ptr++; } } *out=0x00; //zero terminate the string. return retVal; }
I added a little more polished version of this code as a snippet.
This post has been edited by NickDMax: 22 Nov, 2007 - 11:09 PM
Now, any ideas on why the hell my private variables "real" and "imag" (as set by the header file, which i can't change) aren't showing up as accessible by any of my other operators? (ie "+=")
Are you using the proper scope for the operator definition? As in