2011-10-21 13 views
0

複数の文字列またはユーザー入力をアレイに追加するにはどうすればよいですか?私は10名までの連絡先を追加する必要がある連絡先帳を作成しようとしています。私はそれらの配列またはtxtファイルを格納しようとしているし、後で私はこの入力を使用できるようにしたい。アレイに複数のユーザー入力を追加するにはどうすればよいですか?

ここに私のコードです。私が言うことが明確でない場合は、コードを実行すると役立ちます。

#include <cstdlib> 
#include <iostream> 
#include <fstream> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    // declare two variables; 
    char name[20]; 
    int age; 
string ans; 
do {  
    // get user to input these; 
    cout << "What is your name: "; 
    cin >> name; 
    cout << "What is your age : "; 
    cin >> age; 
    cout<<"continue ";cin>>ans; 
    }while((ans == "y" || ans=="yes")); 
    // create output stream object for new file and call it fout 
    // use this to output data to the file "test.txt" 
    char filename[] = "test.txt"; 
    ofstream fout(filename); 
    fout << name << "," << age << "\n"; // name, age to file 
    fout.close(); // close file 

    // output name and age : as originally entered 
    cout << "\n--------------------------------------------------------" 
     << "\n name and age data as entered"; 
    cout << "\n Your name is: " << name; 
    cout << "\n and your age is: " << age;  

    // output name and age : as taken from file   

    // first display the header 
    cout << "\n--------------------------------------------------------" 
     << "\n name and age data from file" 
     << "\n--------------------------------------------------------"; 

     ifstream fin(filename); 

     char line[50];    
    fin.getline(line, 50);  


    char fname[20];  
    int count = 0;  
    do 
    { 
     fname[count] = line[count];  
     count++; 
    } 
    while (line[count] != ',');   
    fname[count] = '\0';    


    count++; 
    char fage_ch[10];  
    int fage_count = 0; 
    do 
    { 
     fage_ch[fage_count] = line[count]; 
     fage_count++; count++;    
    } 
    while (line[count] != '\0');    
    fage_ch[fage_count] = '\0'; 


    int fage_int = 0;   
    int total = 0;   
    char temp;    

    for (int i = 0; i < (fage_count); i++) 
    { 
     temp = fage_ch[i]; 
     total = 10*total + atoi(&temp); 
    } 

    fage_int = total; 

    // display data 
    cout << "\n\n Your name is: " << fname; 
    cout << "\n and your age is: " << fage_int; 
    cout << "\n\n--------------------------------------------------------";  

     cout <<endl; 

    return EXIT_SUCCESS; 
} 
+0

ファイルを使用する理由はありますか?それともあなたが試した方法ですか?私はあなたがしたいことを理解しようとします。 –

答えて

2

おそらく、各エントリの名前&年齢を格納する代わりに、2つの別々の配列の構造体の配列を使用したほうが良いでしょう。次に、strcpyを使ってループして、名前から構造体の名前に入力文字列をコピーすることができます。構造体に慣れていない場合は、2次元配列を2つ使用することもできます。

私はコードを投稿するつもりはないが、基本的なアルゴリズムのためにあなたが始めるために(そして、できればあなたが持っているもの簡素化する)ので、これは宿題のようになります。

#define MAX_CONTACTS 10 
#define MAX_NAME_LENGTH 20 

// 2D array to store up to 10 names of max 20 character length 
char nameVar[MAX_CONTACTS][MAX_NAME_LENGTH] 
int ageVar[MAX_CONTACTS] 

do until end of user input 
    read name into nameVar[index] 
    read age into ageVar[index] 
    index += 1 
end loop 

while contactCounter < index 
    ouput nameVar[contactCounter] 
    output age[contactCounter] 
    // you could also write to file in this loop if thats what you're trying to do 
    // using the fprintf function to write to an opened file 
    contactCounter += 1 
end loop 

また、私はあなたはその電話で何をしようとしているのか分からないが、必要ではないように思える。 atoiの働きは、渡された最初の文字を見て、配列内の数字以外の文字が出現するまですべての数字を変換します。だから、もしあなたがchar配列を持っていれば、atoiは123を返します。atoi "1h2"を渡すと1を返します。

また、fprintfを使ってchar配列とintの両方を出力できます。ファイル。だから、

あなたは私とのchar S [10] = "こんにちは" とファイルストリームあなたのようなストリーミングするために印刷できませんでしたint型持っている場合:

のfprintf(ストリーム、「ディスプレイへの私のテキスト:%sの%のI "、s、i)

私はそれが役に立ちそうです。

+0

ありがとう、これは非常に有用です。 – user836910

関連する問題