自己寫了一個類,但是輸出有問題
這是調試界面,下面是代碼,求助啊,為什么輸出有燙口
#include"stdafx.h"
#include <string.h>
#include <iostream>
using namespace std;
class Dogs
{
public:
char name[5];
int age;
void dogspeak()
{
cout << "wang wang !!" << endl;
}
void dogrun()
{
cout << "I am running !!" << endl;
}
};
int main(void)
{
Dogs white_dog;
Dogs *p = new Dogs();
if (NULL == p)
{
//falsed
return 0;
}
cout << "please input your dog's name:" << endl;
for (int i = 0; i < 5; i++)
{
cin >> white_dog.name[i];
}
cout << "The dog is ______ years old." << endl;
cin >> white_dog.age;
cout << "The white dog is " << white_dog.age << " years old." << endl;
cout << "name is " << white_dog.name << endl;
cout << "please input your another dog's name:" << endl;
for (int i = 0; i < 5; i++)
{
cin >> p->name[i];
}
cout << "The another dog is ______ years old." << endl;
cin >> p->age;
cout << "The " << p->name <<" dog is " << p->age << " years old." << endl;
cout << "name is " << p->name << endl;
delete p;
p = NULL;
system("pause");
return 0;
}
2018-08-13
出現燙之類的亂碼,很可能是訪問的地方沒有數據。當然這個地方可以改寫為如下方法,讓用戶輸入的數據一次性讀入到輸入緩沖區,而避免一次次輸入到緩沖區而帶來的不確定的錯誤。經過測試,將for循環的代碼改成下列代碼,可以成功運行: