第五版C++primer plus10章最后一道习题的初阶版,参照10-5改了试了下,想改个字符串板的。。弄了一晚上完全不知道问题所在==
<header.h>
struct MStruct
{
char vendor[50];
};
class List
{
private:
MStruct list[5];
int count;
public:
List();
void push(char pu[]);
char* pop();
//void ifempty();
//void iffull();
};
<ex10-8.cpp>
#include<iostream>
#include"header.h"
#include<cstring>
List::List()
{
for (int i = 0;i < 5;++i)
strcpy_s(list[i].vendor, "a");
count =0;
}
void List::push(char pu[])
{
if (count >= 5)
{
std::cout << "the list is full";
}
else
{
strcpy_s(list[count].vendor, pu);
++count;
}
}
char* List::pop() //返回字符串(首地址)
{
if (count=0)
{
std::cout << "the list is empty";
}
else
{
return (list[count-1].vendor);
--count;
}
}
<main.cpp>
#include<iostream>
#include"header.h"
#include<cstring>
#include<cctype>
int main()
{
using namespace std;
List ex1;
char ch;
char arr[50];
cout <<"please enter A to add, P to pop, Q to quit";
while (cin>>ch&&toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case 'A':
case'a': cout << "enter arr";
cin.get(arr, 50);
while (cin.get() != '\n')
continue;
ex1.push(arr);
break;
case 'P':
case'p': strcpy_s(arr,ex1.pop());
cout << "pop ED: " << arr << endl;
break;
}
cout << "please enter A to add, P to pop, Q to quit";
}
cout << "bye~“;
}
第一次踏上论坛,若是错误低级自己没发现,大神勿喷