2011-12-13 10 views
0

上記の問題は、2D char配列に10人の従業員の名前と年齢を入力として受け取り、年齢の昇順で名前を表示するプログラムを作成することです。名前を年代順に表示しますか?

これまでの記事の下に貼り付けましたが、年齢順に名前を表示する方法がわかりません。私は何が欠けていますか?

#include<iostream.h> 
#include<string.h> 
void sort(int[],int); 
void main(void) 
{ 
    char a[10][5],x; 
    int b[5],i,j; 
    for(j=0;j<=5;j++) 
    { 
     cout<<"Enter name of Employee:";   
     for(i=0;i<10;i++) 
     { 

      cin>>x; 
      x=a[j][i]; 
     } 
     cout<<"Enter Employee's age:"; 
     cin>>b[j]; 
     cout<<endl; 
    } 
    sort(b,j); 
    cout<<"Name of Employee \t\t Age \n"; 
    for(j=0;j<5;j++) 
    { 
     for(i=0;i<10;i++) 
     { 
      x=a[j][i]; 
      cout<<"\t"<<x; 
     } 
     cout<<"\t\t"<<b[j]<<"\n"; 
    } 
} 
void sort(int x[],int size) 
{ 
    int temp,i,j; 
    for(j=0;j<size;j++) 
    { 
     for(i=0;i<size;i++) 
     { 
      if(x[i]>x[i+1]) 
      { 
       temp=x[i+1]; 
       x[i+1]=x[i]; 
       x[i]=temp; 
      } 
     } 
    } 
} 
+2

ここで宿題を投稿するのは嫌です。それはサイトを劣化させます。 – David

+0

@Dave「宿題」というタグが付いているので、「宿題」タグを無視することができます – Dov

答えて

1

void mainが有効なC++ではありませんので、あなたは、int mainvoid mainを変更することで開始する必要があります。そして、あなたは本当のCであることを#include Sを変更する++ヘッダは、あなたが持っているものではないファイル:

#include <iostream> 
#include <string> 

次に、あなたがあなたの変数に意味のある名前を与える必要があります。 abの海のコードに従うことは不可能です。

従業員を年齢順に並べ替える必要があります。すでに並べ替えアルゴリズムが書かれているので、年齢だけを比較しながら、年齢を交換する代わりにのすべての従業員データを交換する必要があります。

struct employee { 
    std::string name; 
    int age; 
}; 

て一度にソート、あなたができるだけのループ:代わりに、従業員データ用の2つの別々の配列で、あなたの代わりに、従業員の名前と年齢の両方を保持する構造体の単一のアレイを保った場合、これは容易になりますすべての従業員が名前を印刷します。

私はコードにいくつか問題があると思いますが、with a good referenceとデバッグ時間を簡単に修正する必要があります。問題がある場合は、あなたが今までに得たものを使って新しい質問を投稿し、何があなたを悩ませているのかを説明することができます。

これは、すべてのあなただけのC++標準ライブラリを使用するが、残念ながら一部の教師ではなく、適切なCの学生に不自由++を教えることにした場合より良いだろう++ :(

#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 

// treat an employee as a single unit 
struct employee { 
    std::string name; 
    int age; 
}; 

// a comparison function to compare two employees by their age 
// should return true if the first one is younger than the second one 
// this will be used for sorting later 
bool is_younger(employee const& l, employee const& r) { 
    return l.age < r.age; 
} 

int main() 
{ 
    // a vector with 5 employees 
    std::vector<employee> employees(5); 
    for(j=0;j<=5;++j) 
    { 
     std::cout << "Enter name of Employee: " 
     // read the entire name at once 
     getline(std::cin, employees[j].name); 

     std::cout << "Enter Employee's age:"; 
     // read the age 
     std::cin >> employees[j].age; 
    } 

    // sort all the employees using the comparison function written above 
    std::sort(employees.begin(), employees.end(), is_younger); 

    std::cout<<"Name of Employee \t\t Age \n"; 
    for(j=0;j<5;++j) 
    { 
     std::cout << employees[j].name; 
     std::cout << "\t\t" << employees[j].age << "\n"; 
    } 
} 
+0

これはVisual C++でこのプログラムをやっていますが、これは違いがありますか?主な問題はソートではなく、彼らの年齢、どのように私はそれを行うのですか? –

2

私は右であなたを指します方向。

あなたは比較関数を作る、その後、名前と年齢のペアを保持する構造体を定義する必要があります。

その後、あなたは、単にあなたの構造体のベクトルを移入する必要があり、その後standaが提供するソート機能を使用してそれらを並べ替えますrdライブラリ、ベクトルを繰り返してコンテンツを出力すると、単純化するために作成した構造体のストリーム演算子を定義できます。

http://en.cppreference.com/w/cpp/container/vector

http://en.cppreference.com/w/cpp/algorithm/sort

http://www.cplusplus.com/doc/tutorial/structures/

http://www.java2s.com/Code/Cpp/Overload/Overloadstreamoperator.htm

EDIT:すべてのことが良いためと、名前を保持するためのstd ::文字列を使用してください。あなたのようなchar配列を使用して

http://www.cplusplus.com/reference/string/string/

ほとんど廃止されています。

私はむしろC++があなたの講義があなたに与えているものではないことを知りたいので、これを他の解決策の前に書き始めました。

#include <string> 
#include <vector> 
#include <iostream> 
#include <iterator> 
#include <algorithm> 
#include <fstream> 

struct employee 
{ 
    employee(const std::string& name_, unsigned age_) 
    : name(name_), age(age_) {} 

    std::string name; 
    unsigned age; 
}; 

std::ostream& operator<<(std::ostream& os, const employee& e) 
{ 
    os << e.name << " " << e.age; 
    return os; 
} 

bool comp_age(const employee& e1, const employee& e2) 
{ 
    return e1.age<e2.age; 
} 


int main() 
{ 
    std::vector<employee> employees; 
    employees.reserve(5); 

    for(unsigned i=0; i!=5; ++i) 
    { 
     std::string name; 
     unsigned age; 

     std::cout << "Name:" << std::flush; 
     std::cin >> name; 
     std::cout << "Age:" << std::flush; 
     if(!std::cin >> age) 
     { 
      std::cerr << "not an number" << std::endl; 
      --i; 
      continue; 
     } 
     //note you should catch any failure to parse to int here 

     employees.push_back(employee(name, age)); 
    } 

    std::sort(employees.begin(), employees.end(), comp_age); 

    std::copy( employees.begin(), employees.end(), 
       std::ostream_iterator<employee>(std::cout, "\n")); 

    return 0; 
} 

これは代替ですが、この例を構成する上記の概念を覚えておいてください。

+0

私は構造体を使うことができますが、これには2D配列のみを使用しなければなりません。それは条件です。 –

+0

サニー:ベクトルは基本的に2次元配列でカプセル化されており、明確に定義されたインタフェースです。そうでない場合は、std :: sort(my_array、my_array + array_size、compare)でソート(配列の配列で)を使用することができます。しかし、TBHベクトルは正しい方法です。 (あなたの講義をSOに紹介していない場合) – 111111

+0

'std :: flush'は必要ありません。標準入力から読み込むと、標準出力はフラッシュされることが保証されています。 –

0

std::multimap(ニーズに合ったキー/バリューストア)を使用します。これはデフォルトで弱くソートされています。 keyは年齢、valueは名前です。値を印刷するにはイテレータを使用します。同じ年齢の従業員が潜在的に存在する可能性があるため、std::multimapが選択されます。

-2
#include <iostream.h> 

int main() { 
    int a; 
    int b; 
    int sum; 

    std::cout << "Enter two numbers" << std::endl; 

    std::cout << "Enter the value of a" << std::endl; 
    std::cin >> a; 

    std:: cout << "Enter the value of b" << std::endl; 
    std::cin >> b; 

    sum = a + b; 
    std::cout << sum << std::endl; 

    return 0; 
} 
関連する問題