#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));
}
答案 0 :(得分:1)
#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;
}
}
原文备注:
我在快速阅读时注意到了几个错误:
void initialize ( char name[],int *age) {name=""; age=0; }
应该是
void initialize ( char name[],int *age) {name[0]='\0'; *age=0; }
和
return(age1==age2 &&strcmp(name1,name2));
应该是
return(*age1==*age2 && !strcmp(name1,name2));
此外,这没有任何意义:
print(&name1,&age1);
print(&name2,&age2);
答案 1 :(得分:0)
一些建议:
1.使用std::string
代替'char *`
这可以减轻许多令人头疼的问题,例如内存分配和调整大小。 (输入和输出也更简单。)
如果修改函数的参数,则按引用传递(不要使用指针) C ++提供了引用,它允许修改变量而不会出现乱七八糟的指针。不需要检查引用是否为NULL,并且它们很少指向非法或丢失的内容 例如:
void intialize(std :: string&amp; name,int&amp; age) { name.clear(); 年龄= 0; 返回; }
开始使用类,结构和对象。让对象处理输入,输出和比较:
结构人
{
std :: string name; //每个人都有一个名字。
unsigned int age; //“年龄”不能为负数,因此声明为无符号。
Person()//默认构造函数 :age(0)//使用初始化列表进行初始化。 {; } // String将自己初始化,所以这里没有列出。
void Print(void)//使用std :: cout打印成员。 { cout&lt;&lt; “姓名:\”“&lt;&lt; name&lt;&lt;”\“\ n”; cout&lt;&lt; “年龄:”&lt;&lt;年龄&lt;&lt; “\ n” 个; 返回; };
打印一个人:
Person me;
me.name = "Albert Einstein";
me.age = 53;
me.Print();
在Print
中封装struct
功能有助于简化程序。不需要所有这些C风格的功能。