0

I am doing a homework assignment using pointers to traverse arrays. When I go to assign a 5 digit zip code to an array the array is populated with the ascii value of the number. I am having a hard time grasping the concept of pointers and when to use them.

I have used the debugging tool on eclipse to determine that the array is being populated incorrectly and not my output statement.

Here is the method used to populate the array where r1 is an object of a class and zipcode is an integer array of size 5 (for loop only increments to five for testing purposes):

void getDataFromFile(RentalAgency &r1){
ifstream infile;
string input;
char number;

cout << "Enter the file name: ";

infile.open("Agencies.txt");
    infile.get((r1.name),MAX_SIZE,space);
        for(int i = 0;i < 5;i++){
            infile >> number;
            *(r1.zipcode+i) = number;
        }
    infile.close();
}

And here is the code for struct RentalAgency:

struct RentalAgency{
  char name[MAX_SIZE];
  int zipcode[5];
  RentalCar inventory();
};

When given the number 93619 the expected output is 93619, however the actual output is 57 51 54 49 57

  • Please provide a [mcve]. It looks like everything is correct. You are reading chars and storing them as integers. When you print the value 57 as integer the number itself is printed. When you print 57 as char the character '9' is printed. – Thomas Sablik Sep 14 '19 at 00:51
  • Please add the definition of `r1` and it's class or structure definition. Is `r1.zipcode` a pointer, an array, something else? Also, provide the definition of `number`. – Thomas Matthews Sep 14 '19 at 00:55
  • You could use [std::atoi](https://en.cppreference.com/w/cpp/string/byte/atoi) to convert it: `*(r1.zipcode+i) = std::atoi(number);`. Remember to include ``. Alternatively you can change the data type of `zipcode` from integer to char. – Thomas Sablik Sep 14 '19 at 00:58
  • Please don't post code as comment. Edit your question. – Thomas Sablik Sep 14 '19 at 01:02
  • Why so you sometimes use std::string and Sometimes cstring? You should always use std::string. And please read [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Thomas Sablik Sep 14 '19 at 10:24

1 Answers1

1

It looks like everything is correct. You are reading chars and storing them as integers. When you print the value 57 as integer the number itself is printed. When you print 57 as char the character '9' (ASCII value) is printed.

You can convert char to int with

*(r1.zipcode+i) = number - '0';

C and C++ guarantees that whatever representation is in use, the representations of the 10 decimal digits are contiguous and in numeric order, even if not ASCII is used.

Alternatively you can change the data type of zipcode from integer to char:

char zipcode[5];

In addition I recommend to use the array style

r1.zipcode[i] = number;

It does the same but it's a little bit clearer for the reader what the programmer wanted to do.

If you change the struct to

struct RentalAgency{
  std::string name;
  char zipcode[5];
  RentalCar inventory();
};

you can simplify the function to

void getDataFromFile(RentalAgency &r1){
    std::ifstream infile;
    std::string input;

    std::cout << "Enter the file name: ";
    std::cin >> input;

    infile.open(input);
    infile >> r1.name;
    // or std::getline(infile, r1.name, space);
    for (std::size_t i = 0; i < 5; ++i)
         infile >> r.zipcode[i];
    }
}

You can call infile.close() but you don't need it. It's also called in the destructor when the function body is left.

Here is an example

#include <iostream>
#include <sstream>
#include <string>

struct RentalCar {};

struct RentalAgency{
  std::string name;
  char zipcode[5];
  RentalCar inventory();
};

void getDataFromFile(RentalAgency &r1){
    // std::ifstream infile;
    // std::string input;

    // std::cout << "Enter the file name: ";
    // std::cin >> input;

    // infile.open(input);
    std::stringstream infile{"Name 12345 Name2 12346"};
    infile >> r1.name;
    for (std::size_t i = 0; i < 5; ++i)
         infile >> r.zipcode[i];
    }
}

int main() {
    RentalAgency r;
    getDataFromFile(r);
    std::cout << r.name << ' ';
    for (std::size_t i = 0; i < 5; ++i)
         std::cout << r.zipcode[i];
    }
    std::cout << '\n';
    // Output: Name 12345

    return 0;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62