程序主要是一个查询功能
了解cin cin.get cin.getlin cin.get(name,size)
cin 进行读取一段字符通过接受回车结束
cin.get 能接受包括回车的字符
cin.getline 函数的作用是从输入流中读取一行字符,其用法与带3个参数的get函数类似。即
cin.getline(字符数组(或字符指针), 字符个数n, 终止标志字符)//可以没有终止标志字符
程序并没有对这个函数进行测试;
cin.get(name,size)作用是在输入流中读取size的字数进行赋值给name测试不适合本程序因为程序的f接受的只是一个字符大材小用;
#include "stdafx.h"
#include<iostream>
#define size 20
using namespace std;
struct bop {
char name[size];
char title[size];
char popname[size];
int preference;
}pop[4] = { { "wimp","haha","fix",0 },{ "raki","youke","red",1 },{ "celia","lunduan","teacher",2 } ,{ "hopp","yahu","tianwen",3 } };;
int fun(char);
int main()
{
cout << "输入a显示所有姓名
输入b显示所有title
输入c显示别称
输入d目前不理解
输入q退出" ;
char f;
cin>>f;
while(f != 'a' && f != 'b' && f != 'c' && f != 'd ' && f != 'q')
{
f = cin.get();
cout << "choice again
" << f << endl;
}
while (fun(f))
{
cout << "next choice" << endl;
cin >> f;
}
return 0;
}
int fun(char f)
{
int i;
switch (f)
{
case 'a':cout << "show name" << endl;
for (i = 0; i<4; i++)
cout << pop[i].name << endl; break;
case 'b':cout << "show title" << endl;
for (i = 0; i<4; i++)
cout << pop[i].title << endl; break;
case 'c':cout << "show bopname" << endl;
for (i = 0; i<4; i++)
cout << pop[i].popname << endl;
case 'd':cout << "show prefence" << endl;
break;
case 'q':cout << "bye" << endl;
return 0; break;
}
return 1;
}
进行多次更改,刚开始无法解决输出流重复输出cout的文件经过分析发现cin.get不仅接受了字符还接受了回车才导致进行了两次循环