C++ [警告] 从 'char' 转换为 'double' ...但我不是...?

标签 c++ compiler-errors warnings

我定义了自己的数据结构:

struct Data
{
    int partNumber;
    char nameOfTool[ 30 ];
    int numInStock;
    double pricePer;
};

我的这个函数给我带来了问题:

void inputOrUpdate(fstream &toolsFile)
{
 int accountToModify;
 char nameOfTool[ 30 ];
 int numInStock;
 double pricePer;

 printf( "Enter record number ( 1 to 100, 0 to return to main menu )" );
 scanf( "%d", &accountToModify );

 if ( accountToModify > 0 && accountToModify <= 100 )
 {
      printf( "Enter tool name, quantity, cost" );
      scanf( "%29s %d %d", nameOfTool, numInStock, pricePer );

      Data toolToWrite = { accountToModify, nameOfTool[ 30 ], numInStock, pricePer };

      toolsFile.seekg((accountToModify - 1) * sizeof(toolsFile));
      toolsFile.write((char *)&toolToWrite, sizeof(toolsFile));
 }
 else
 {
      printf( "Invalid record number provided." );
 }

 printf( "\n\n" );
}

具体来说,这一行:

Data toolToWrite = { accountToModify, nameOfTool[ 30 ], numInStock, pricePer };

出现此错误:

[Warning] converting to `char' from `double' 

但这对我来说没有任何意义; “数据”应该是 int,一串字符,int,double,并且 scanf 行应该捕获一串字符,一个 int 和一个 double,我组装 toolToWrite 的那一行应该是预期的整数,一个字符串字符,整数和 double ...如果我在 scanf 行中指定 %29s ,我不明白它认为我在哪里将 double 转换为字符;只有 %s 也失败了。

我觉得我错过了一些非常基本的东西,这让我抓狂。有什么想法吗?

最佳答案

如何改变这一行:

Data toolToWrite = { accountToModify, nameOfTool[ 30 ], numInStock, pricePer };

Data toolToWrite = { accountToModify, *nameOfTool, numInStock, pricePer };

关于C++ [警告] 从 'char' 转换为 'double' ...但我不是...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908655/

相关文章:

c++ - 引用为成员变量

android - 为什么Eclipse无法编译Google示例?

C 警告 "format ' %d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'int"

c - 函数 strlen 中的警告 - 在 C 中

c++ - 文件未在正确的目录 C++ 中创建

c++ - C - 打印 float - 转换为 int 时精度损失

c++ - 获取另一个指针类成员封装的指针类成员列表的索引值

java - 如何创建一个按单个字符读取整数的循环

java - "Constructor cannot be applied to given types"错误

python - 让 Python 的 `warnings.warn()` 不提及自己