#include<iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
box* next;
};
box* head;
box* create()
{
float heights;
box* p = NULL;
box* q = NULL;
head = NULL;
cout << "please enter box height" << endl;
cin >> heights;
while (heights != 0)
{
p = new box;
p->height = heights;
cout << "please enter box width" << endl;
cin >> p->width;
cout << "please enter box length" << endl;
cin >> p->length;
cout << "please enter box maker" << endl;
cin.getline(p->maker, 40);
if (head == NULL)
{
head = p;
}
else
{
q->next = p;
}
q = p;
cout << endl;
cout << "please enter box height" << endl;
cin >> heights;
}
cout << endl;
if (head != NULL)
{
q->next = NULL;
}
return head;
}
void display(box* head)
{
cout << "box height,width,length,volume and maker." << endl;
while (head != NULL)
{
cout << "height = " << head->height << endl
<< "width = " << head->width << endl
<< "length = " << head->length << endl
<< "volume = " << (head->height) * (head->width) * (head->length) << endl
<< "maker is " << head->maker << endl
<< endl;
head = head->next;
}
}
int main()
{
display(create());
system("pause");
return 0;
}
我的這一步咋不讓我輸入就跳過去了
uj5u.com熱心網友回復:
因為上面輸入有回車了,所以就結束了uj5u.com熱心網友回復:
fflush(stdin);cin.getline(p->maker, 40);
這樣就好
uj5u.com熱心網友回復:
#include<iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
box* next;
};
box* head;
box* create()
{
float heights;
box* p = NULL;
box* q = NULL;
head = NULL;
cout << "please enter box height" << endl;
cin >> heights;
while (heights != 0)
{
p = new box;
p->height = heights;
cout << "please enter box width" << endl;
cin >> p->width;
cout << "please enter box length" << endl;
cin >> p->length;
cout << "please enter box maker" << endl;
fflush(stdin);
cin.getline(p->maker, 40);
if (head == NULL)
{
head = p;
}
else
{
q->next = p;
}
q = p;
cout << endl;
cout << "please enter box height" << endl;
cin >> heights;
}
cout << endl;
if (head != NULL)
{
q->next = NULL;
}
return head;
}
void display(box* head)
{
cout << "box height,width,length,volume and maker." << endl;
while (head != NULL)
{
cout << "height = " << head->height << endl
<< "width = " << head->width << endl
<< "length = " << head->length << endl
<< "volume = " << (head->height) * (head->width) * (head->length) << endl
<< "maker is " << head->maker << endl
<< endl;
head = head->next;
}
}
int main()
{
display(create());
system("pause");
return 0;
}
還是出問題
uj5u.com熱心網友回復:
還是一樣的問題嗎?uj5u.com熱心網友回復:
謝謝,我弄懂了,先加上cin. get()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61614.html
標籤:C++ 語言
上一篇:VS中莫名其妙的錯誤
下一篇:c++ 題 救救孩子吧
