【问题标题】:invalid conversion from 'int' to 'char *'从 'int' 到 'char *' 的无效转换
【发布时间】:2014-05-18 09:10:39
【问题描述】:

我想编写一个程序,该程序将从文本文件中读取并使用结构存储文本文件中的内容,然后重新组合并打印出文本文件中的信息。但是我遇到了getline 的问题。我试过这样写getline

getline(infile, info.name)

但它不起作用。我还包括<string><cstring>,但我仍然遇到像

这样的错误

错误 C2664:'std::basic_istream<_elem> &std::basic_istream<_elem>::getline(_Elem *,std::streamsize)' : 无法将参数 1 从 'int' 转换为 'char *'

错误 C2664: 'std::basic_istream<_elem> &std::basic_istream<_elem>::getline(_Elem *,std::streamsize,_Elem)' : 无法将参数 1 从 'char [10][80]' 转换为 'char *'

我想打印出来的文本文件是下面的文本

Isabella Chan
新加坡共和国
Libra 23 - 10 - 1993
7
我希望精通 C++
我希望 克里斯蒂娜·格里米(christina grimmie)将赢得声音
我希望......

抱歉这个菜鸟问题,提前致谢!

   #include <iomanip>
   #include <iostream>
   #include <cstdlib>
   #include <ctime>
   #include <cctype>
   #include <fstream>
   #include <string>

   using namespace std;

   const int MAX = 80;
   const int MAXNO = 10;
   enum Zodiac 
   {
            Aquarius, Pisces, Aries, Taurus,
            Gemini, Cancer, Leo, Virgo,
            Libra, Scorpio, Sagittarius, Capricorn
   };
   struct Date
   {
       Zodiac sign;
       int day;
       int month;
       int year;
   };
   struct Student
   {
           char name [MAX];
       char nationality [MAX];
       Date birthDate;
       int no; // no of other messages
       char wishMessage [MAXNO][MAX];
       // Feel free to add in more features
   };

   void myInfo (fstream&, char [], Student&);
   // The above function reads information from the text file
   // and store the information in a structure reference parameter

   void printOut(Student);

   int main()
   {
       fstream infile;
       char fName[MAX];
       Student info;
       cout << "Enter your info file name: "
       cin  >> fName; 
       cout << endl;

       myInfo(infile, fName, info);
       printOut(info);

   }

   void myInfo (fstream& infile, char fName[], Student& info)
   {
          infile.open(fName, ios::in);

      if(!infile)
      {
           cout << "Error file not found!" << endl;
           exit(0);
      }
       infile.getline(info.name, MAX);
       infile.getline(info.nationality,MAX);
       infile  << info.birthDate.sign
           << info.birthDate.day
           << info.birthDate.month
           << info.birthDate.year;
       infile.getline(info.no, MAX);
       infile.getline(info.wishMessage, MAXNO, MAX);

       infile.close();
       cout << "Successfully readed!" << endl;

   }

   void printOut(Student info)
   {
       cout << "My name is " << info.name << endl;
       cout << "My nationality is " << info.nationality << endl;
       cout << "My date of birth is " << info.birthDate.day 
            << " " << info.birthDate.month << " " 
        << info.birthDate.year << endl;
       cout << "I am " << info.birthDate.sign << endl;
       cout << "\n I have " << info.no << " wishes:" << endl;

   }

【问题讨论】:

  • getline 读取字符串,而不是整数。

标签: c++ parameters multidimensional-array fstream getline


【解决方案1】:

您似乎正在尝试使用 getline 读取非字符串,而它正在按照 documentation 读取字符串。

从流中提取字符作为未格式化的输入,并将它们作为 c 字符串存储到 s 中,直到提取的字符是定界字符,或者 n 个字符已写入 s(包括终止的空字符)。

这里有两条违规的行:

infile.getline(info.no, MAX);

infile.getline(info.wishMessage, MAXNO, MAX);

前者读入int,后者读入字符串数组。

你需要先读入其中的字符串,然后根据需要进行相应的转换操作。

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多