pointer variable to integer?

This is a discussion on pointer variable to integer? within the C++ Programming forums, part of the General Programming Boards category; Hi, My problem is that I have an array stored in a file which contains a variable "type". An example ...

  1. #1
    Unregistered
    Guest

    Angry pointer variable to integer?

    Hi,

    My problem is that I have an array stored in a file which contains a variable "type".

    An example of the file is

    1
    Mr Blank
    2 Blank Road
    Blanksville
    r

    the final character 'r' stands for remove and is can be identified tbp->entry[j].type

    what I am trying to do is read each record from the file and depending on the 'type' perform certain actions.

    My code looks something like this

    for(j=o; j<(tbp->n); j++)
    if(tbp->entry[j].type == 'r')
    statements;
    else statements;

    the problem seems to be that tbp->entry[j].type is not recognised as an integer and as such cannot be compared with 'r'

    hope someone can help?

    Andy

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    I assume that yourstruct.type is defined as a char *, but without you posting the definition of your structure this is conjecture.

    A couple of solutions

    o if the operation field in your file is only ever 1 character, then you don't need a pointer to a string for that field... a plain old char would do.

    o use strcmp() instead of an == comparison (which you should do for all strings, unless you really do want to compare two pointers rather than the strings those pointers point to).

    Ian Woods

  3. #3
    Unregistered
    Guest
    How does tbp-> fit into the picture? If you have a struct like this:

    struct Entry
    {
    int orderNumber;
    char name[30];
    char address[30];
    char type;
    }

    Then you could have an array (or some other container) of type Entry like this to store the Entry's in the file.

    const int SIZE;
    Entry dataBase[SIZE];

    Then you can read the entries into the array using one of several techiques (here's just one way);

    while(!fin.eof())
    {
    for(int i = 0; i < SIZE; i++)
    {
    fin >> dataBase[i].orderNumber;
    .
    .
    .
    fin >> dataBase[i].type;
    }
    }

    Then you could display the contents of the contaner:
    for(int i = 0; i < SIZE; i++)
    {
    if(dataBase[i].type != 'r')
    {
    cout << dataBase[i].name << endl << dataBase[i].address << endl << endl;
    }
    }

    or whatever.

  4. #4
    Unregistered
    Guest

    Angry pointer to string?

    Hi,

    Thanks for all input.

    Okay here's some of my coding to help explain the problem a little further.


    #define TYPE_SIZE 2
    /*Struct definitions...*/
    struct transaction_book_element


    {
    char mem_num[MEM_NUM_SIZE];
    char name[NAME_SIZE];
    char addressone[ADDRESS_SIZE];
    char addresstwo[ADDRESS_SIZE];
    char type[TYPE_SIZE];
    };
    typedef struct transaction_book_element Transaction_Book_Element;
    struct transaction_book


    {
    Transaction_Book_Element entry[TRANSACTION_BOOK_SIZE];
    int n;
    };


    /* Here's some more of my definitions */

    FILE *fp =fopen("tbfile.txt","r");
    Transaction_Book *tbp,tb;
    tbp = &tb;

    I have tried using a string compare function resulting in the Error "comparrison of incompatible types "

    I wondered if it would be possible to declare a pointer variable and assign a type action eg "r" to it then simply compare the two pointers but it dose'nt seem to work?

    Please advise this troubled beginner?

    PS I am working with a gcc compiler on a UNIX terminal so no C++ replies please. (I know C & C++ are similar but I'm only a beginner!).

    Thanks again!

  5. #5
    Unregistered
    Guest
    given those definitions, tbp->entry[i].type should be an array of char with TYPE_SIZE number of elements. It is not possible to use the == operator on arrays. You have to compare each individual element of the array, or one of the strcmp() family of functions if the array is really a string. Basically type is not an int, but a group of ints (ANSI char values). If type is a string then you shoulc be able to do this:

    int result = strcmp(tbp->entry[i].type, "r");
    if(result == 0)
    ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i know the size of variable pointer
    By unikgila in forum C Programming
    Replies: 10
    Last Post: 10-28-2008, 10:08 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. How to set pointer for environment variable
    By spiky1 in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2006, 05:19 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21