开发者

how to solve the errors of this program

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstring>

void initialize(char[],int*);
void input(const char[] ,int&);
void print ( const char*,const  int);
void growOlder (const char [], int* );

bool comparePeople(const char* ,const int*,const char*,const int*);

int main(){

     char name1[25];
     char name2[25];
     int age1;  
     int age2;


    initialize (name1,&age1);
    initialize (name2,&age2);

    print(name1,*age1);
    print(name2,*age2);

    input(name1,age1);
    input(name2,age2);

    print(&name1,&age1);
    print(&name2,&age2);

    growOlder(name2,age2);

    if(comparePeople(name1,&age1,name2,&age2))
    cout<<"Both People have the same  name and age "<<endl;
    return 0;
}

void input(const char name[],int &age)
{
    cout<<"Enter a name :";
    cin>>name ;

    cout<<"Enter an age:";
    cin>>age;
    cout<<endl;
}

void initialize (  char name[],int *age)
{  
name="";
age=0;
}
void print ( const char name[],const int age )
{
    cout<<"The Value stored in variable name is :"
         <<name<<endl
        <<"The Value stored in variable  age is :"
         <<age<<开发者_运维百科endl<<endl;
}

void growOlder(const char name[],int *age)
{
    cout<< name <<" has grown one year older\n\n";
    *age++;
}
bool comparePeople (const char *name1,const int *age1,
                    const char *name2,const int *age2)
{

    return(age1==age2 &&strcmp(name1,name2));

}


Oh dear. The more I look at this code, the harder it is to find a line without some bug or another. My original comments (containing specific bugs I found) are still at the bottom of this post, but this code is crying out for drastic refactoring:

#include <iostream>
#include <string>

using namespace std;

// Since you say you're using visual studio, presumably you're coding in C++.
// People is a perfect candidate for a class:

class Person
{
  private:
    // Instead of char arrays, you should use std::string for string data
    string name;
    int age;

  public:
    // The initialize() method becomes the class constructor
    Person() : name(""), age(0) {}

    void growOlder()
    {
      cout << name << " has grown one year older\n\n";
      age++;
    }

    // Instead of comparePeople, you can overload operator==
    bool operator==(const Person &other) const
    {
      return age == other.age && name == other.name;
    }

    void print() const
    {
      cout << "The value stored in variable name is: " << name << endl;
    }

    // A factory method can construct a Person from imput
    static Person input()
    {
      Person p;
      cout << "Enter a name: " << endl;
      cin >> p.name;
      cout << "Enter an age: " << endl;
      cin >> p.age;
      return p;
    }
};

int main()
{
  Person p1 = Person::input();
  Person p2 = Person::input();

  p1.print();
  p2.print();

  p2.growOlder();

  if(p1 == p2)
  {
    cout << "Both people have the same name and age" << endl;
    return 0;
  }
}

Original remarks:

A couple of bugs I noticed on a quick reading:

void initialize ( char name[],int *age) {name=""; age=0; }

should be

void initialize ( char name[],int *age) {name[0]='\0'; *age=0; }

and

return(age1==age2 &&strcmp(name1,name2));

should be

return(*age1==*age2 && !strcmp(name1,name2));

Also, this doesn't make any sense:

print(&name1,&age1);
print(&name2,&age2);


Some suggestions:
1. Use std::string instead of 'char *`.
This relieves you of many headaches such as memory allocation and resizing. (Input and output is also simpler.)

  1. If the argument to a function will be modified, pass by reference (don't use pointers).
    C++ provides references which allow a variable to be modified without the mess of pointers. References don't need to be checked for NULL and very seldom do they point to something illegal or missing.
    Example:

    void intialize(std::string& name, int& age) { name.clear(); age = 0; return; }

  2. Start using classes, structures and objects. Let the objects handle input, output and comparison:
    struct Person { std::string name; // Every person has a name. unsigned int age; // An "age" cannot be negative, so it is declared as unsigned.

    Person() // Default constructor : age(0) // Use initialization list for initializations. { ; } // String will initialize itself, so not listed here.

    void Print(void) // Prints the members using std::cout. { cout << "Name: \"" << name << "\"\n"; cout << "Age: " << age << "\n"; return; };

To print a person:

Person me;
me.name = "Albert Einstein";
me.age = 53;
me.Print();

Encapsulating the Print functionality inside the struct helps to simplify the program. No need for all those C-style functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜