Welcome to Dream.In.Code
Become a C++ Expert!

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!




Issues checking character values from array

 
Reply to this topicStart new topic

Issues checking character values from array, "ISO C++ Forbids comparing integer to pointer"

lwnexgen
22 Nov, 2007 - 05:08 PM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
Hey guys-

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;
                }

                imagStream[afterStart] = input[after];

                afterStart++;
            }
        }
    }

    double newReal = atof(realStream);
    double newImag = atof(imagStream);

    if (thisChar == '-')
    {
        newImag = (-1)*(newImag);
    }
    
    cout << newReal << " newReal " << newImag << " newImag" << endl;     
    
    ComplexNumber newComplex(newReal,newImag);

    n = newComplex;
        
    return in;
}


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
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Issues Checking Character Values From Array
22 Nov, 2007 - 06:59 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
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'
User is online!Profile CardPM
+Quote Post

lwnexgen
RE: Issues Checking Character Values From Array
22 Nov, 2007 - 07:19 PM
Post #3

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
QUOTE(NickDMax @ 22 Nov, 2007 - 07:59 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'


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.

Thanks!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Issues Checking Character Values From Array
22 Nov, 2007 - 08:22 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
QUOTE
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;
                }

                imagStream[afterStart] = input[after]; //this initilizes imagStream[afterStart], but...

                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....
User is online!Profile CardPM
+Quote Post

lwnexgen
RE: Issues Checking Character Values From Array
22 Nov, 2007 - 08:50 PM
Post #5

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
QUOTE(NickDMax @ 22 Nov, 2007 - 09:22 PM) *

QUOTE
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;
                }

                imagStream[afterStart] = input[after]; //this initilizes imagStream[afterStart], but...

                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 "+=")
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Issues Checking Character Values From Array
22 Nov, 2007 - 09:20 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
well here is my example of reading a complex value...
CODE
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

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);
        }
    }


    cout << "complex number: " <<inBuffer << endl;
    cout << "Real: " << real << endl;
    cout << "Imaginary: " << imaginary << endl;



    return 0;
}

//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
User is online!Profile CardPM
+Quote Post

jjhaag
RE: Issues Checking Character Values From Array
22 Nov, 2007 - 09:29 PM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(lwnexgen @ 22 Nov, 2007 - 09:50 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
CODE
const ComplexNumber & ComplexNumber::operator+= (const ComplexNumber & rhs) {
    this->real+=rhs.real;
    this->imag+=rhs.imag;
    return *this;
}


Because if the definition header looks like this (missing the ComplexNumber:: before operator):
CODE
const ComplexNumber & operator+= (const ComplexNumber & rhs) {...

You won't be able to access the private members - you need to specify the scope in which you are referring to the private variables real and imag.

hth,

-jjh

This post has been edited by jjhaag: 22 Nov, 2007 - 09:31 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:18PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month