2016-06-27 11 views
-4

atoiとatof変換を使用して文字列をint型とfloat型に変換するレコードを作成しています。しかし、コンパイラは私にこのエラーを与えています: "[エラー]は、 'char * gets(char *)'に引数 '1'の 'int *'を 'char *'に変換できません。 コードは次のとおりです。atoiとatofからのデータ変換

int main() 
{ 
    int ch_id[25]; 
    float ch_gp[25]; 
    struct Data 

    { 
     char name[25]; 
     char Fname[25]; 
     int idno; 
     float Gpa; 

    }; 
    Data emp; 

    cout<<"Enter name:"; 
    gets(emp.name); 
    cout<<"Enter fathers's name:"; 
    gets(emp.Fname); 
    cout<<"Enter Id number:"; 
    gets(ch_id); 
    emp.idno=atoi(ch_id); 
    cout<<"Enter GPA:"; 
    gets(ch_gp); 
    emp.Gpa=atof(ch_gp); 

} 

私はそれを解決しようとしたが、私はmistake.Helpを把握できませんでした!

+2

atoi()とatof()の両方が引数としてcharポインタを使用しますが、代わりにintとfloatの配列を渡しています。私はあなたが 'char ch_id [25];としてch_idを宣言したいと思うch_gpを 'char ch_gp [25];とします。代わりに。 –

+0

@JeremyFriesnerしたがって、ch_idとch_gpのデータ型をcharに変更する必要がありますか? –

+1

[C++で文字列をintに解析する方法は?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – VolAnd

答えて

0

アレイを使用する必要はありません。

int main() 
{ 
    int ch_id; 
    float ch_gp; 
    struct Data 

    { 
     char name[25]; 
     char Fname[25]; 
     int idno; 
     float Gpa; 

    }; 
    Data emp; 

    cout<<"Enter name:"; 
    cin >> emp.name; 
    cout<<"Enter fathers's name:"; 
    cin >> emp.Fname; 
    cout<<"Enter Id number:"; 
    cin >> ch_id; 
    emp.idno=atoi(ch_id); 
    cout<<"Enter GPA:"; 
    cin >> ch_gp; 
    emp.Gpa=atof(ch_gp); 

} 

はまた、あなたのコードはC++よりもはるかにCのように見えます。 charの代わりにstd::stringを使用できます。

+0

しかし、私は配列を使用しない場合は、それ以上の文字を格納しません。そして私は大きな人物を保存する必要があります。 @イシャン。また、アレイの取り外しの問題が同じままである場合。 –

+0

'gets(ch_id);' int ch_id; 'が定義されている間にあなたは何を期待しますか? – VolAnd

+0

@HumaJamilは 'gets(ch_id)'の代わりに 'cin >> ch_id'を使用していますか? –

1
int main() 
{ 
    string ch_id; 
    string ch_gp; 
    struct Data 
    { 
     string name; 
     string Fname; 
     int idno; 
     float Gpa; 
    }; 
    Data emp; 

    cout<<"Enter name:"; 
    getline(cin, emp.name); 
    cout<<"Enter fathers's name:"; 
    getline(cin, emp.Fname); 
    cout<<"Enter Id number:"; 
    cin >> ch_id; 
    emp.idno= stoi(ch_id); 
    cout<<"Enter GPA:"; 
    cin >> ch_gp; 
    emp.Gpa=stof(ch_gp); 

} 
関連する問題