【问题标题】:How to fix array assignment from ascii value to int如何修复从 ascii 值到 int 的数组赋值
【发布时间】:2019-09-14 00:42:09
【问题描述】:

我正在做一个使用指针遍历数组的家庭作业。当我将 5 位邮政编码分配给数组时,该数组将填充该数字的 ascii 值。我很难理解指针的概念以及何时使用它们。

我在 eclipse 上使用了调试工具来确定数组填充不正确,而不是我的输出语句。

这是用于填充数组的方法,其中r1 是类的对象,zipcode 是大小为 5 的整数数组(for 循环仅递增到 5 用于测试目的):

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

这是 struct RentalAgency 的代码:

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

当给定数字 93619 时,预期输出为 93619,但实际输出为 57 51 54 49 57

【问题讨论】:

  • 请提供minimal reproducible example。看起来一切都是正确的。您正在读取字符并将它们存储为整数。当您将值 57 打印为整数时,将打印数字本身。当您将 57 打印为 char 时,将打印字符 '9'。
  • 请添加r1的定义和它的类或结构定义。 r1.zipcode 是指针、数组还是别的什么?另外,请提供number 的定义。
  • 您可以使用std::atoi 转换它:*(r1.zipcode+i) = std::atoi(number);。请记住包括&lt;cstdlib&gt;。或者,您可以将zipcode 的数据类型从整数更改为字符。
  • 请不要将代码发布为评论。编辑您的问题。
  • 为什么有时使用 std::string 有时使用 cstring?您应该始终使用 std::string。请阅读Why is "using namespace std;" considered bad practice?

标签: c++ pointers int arr


【解决方案1】:

看起来一切都是正确的。您正在读取字符并将它们存储为整数。当您将值 57 打印为整数时,将打印数字本身。当您将 57 打印为 char 时,将打印字符 '9'(ASCII 值)。

你可以用 char 转换成 int

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

C 和 C++ 保证无论使用什么表示,10 个十进制数字的表示都是连续的并且按数字顺序排列,即使不使用 ASCII。

您也可以将邮政编码的数据类型从整数更改为字符:

char zipcode[5];

另外我推荐使用数组样式

r1.zipcode[i] = number;

它的作用相同,但对读者来说更清楚程序员想要做什么。

如果将结构更改为

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);
    infile >> r1.name;
    // or std::getline(infile, r1.name, space);
    for (std::size_t i = 0; i < 5; ++i)
         infile >> r.zipcode[i];
    }
}

您可以致电infile.close(),但您不需要它。当函数体离开时,它也会在析构函数中调用。

这是一个例子

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

【讨论】:

  • std::atoi 不需要单个字符,它需要一个指向空终止字符串的指针。
  • 是的,它不会让我将number 传递给atoi()
  • 不,不固定。 char [5] 没有空间容纳空终止符,因此它不是有效的字符串。任何尝试使用它都会导致疼痛。
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 2011-06-13
  • 2016-10-13
相关资源
最近更新 更多