| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:请帮我看看要怎么改
只看楼主 加入收藏
unknowchiu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-12-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
请帮我看看要怎么改
请帮我看看要怎么改
must write the subprogram to share individual elements of the array -- NOT the whole array. In your loops in main, call the subprogram once for each element of the array.

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

const int size = 3;

struct student
{
  string name;
  string address;
  string city;
  string state;
  int zip;
  char gender;
  int idno;
  float gpa;
}; //student

void getInfo(int i, student* s)
{
// function to get students' information
  cout << "student " << i+1 << "'s Name? ";
  getline(cin, s[i].name);

  cout << "student " << i+1 << "'s address? ";
  getline(cin, s[i].address);

  cout << "student " << i+1 << "'s city? ";
  getline(cin, s[i].city);

  cout << "student " << i+1 << "'s state? ";
  getline(cin, s[i].state);

  cout << "student " << i+1 << "'s zip code? ";
  cin >> s[i].zip;
  cin.ignore(1000, 10);

  cout << "student " << i+1 << "'s gender? [M/F]";
  cin >> s[i].gender;
  cin.ignore(1000, 10);

  cout << "student " << i+1 << "'s ID number? ";
  cin >> s[i].idno;
  cin.ignore(1000, 10);
   
  cout << "student " << i+1 << "'s gpa? ";
  cin >> s[i].gpa;
  cin.ignore(1000, 10);
   
  cout << endl;
}

// function to print students' information
void printInfo(int i, student* s)
{
  cout << "=======student (" << i+1 << ")=======" << endl;
  cout << "Name: " << s[i].name << endl;
  cout << "Address: " << s[i].address << endl;
  cout << "City: " << s[i].city << endl;
  cout << "State: " << s[i].state << endl;
  cout << "Zip code: " << s[i].zip << endl;
  cout << "Gender: " << s[i].gender << endl;
  cout << "ID number: " << s[i].idno << endl;
  cout << "GPA: " << s[i].gpa << endl;
  cout << endl;
}

int main()
{
  PrintStatements();

  student s[2];
  int i;

  for (i = 0; i < size; i++)
  {
    getInfo(i, s);
  } //getInfo

  cout << endl;

  for (i = 0; i < size; i++)
  {
    printInfo(i, s);
  } //printInfo

// End!
  cout << endl;
  cout << endl;
  cout << "Press ENTER to continue..." << endl;
  cin.get();
  return 0;
}
搜索更多相关主题的帖子: individual elements address include whole 
2011-12-11 09:12
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:10 
PrintStatements()这个函数从哪冒出来的?

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2011-12-11 19:01
unknowchiu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-12-11
收藏
得分:0 
it is for showing some statement from me...
2011-12-12 06:09
杨易轩
Rank: 2
来 自:山西
等 级:论坛游民
帖 子:4
专家分:33
注 册:2011-4-14
收藏
得分:10 
const int size = 2;就对了。不应该定义为3;
下面是修改后的:经过运行,无误。

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

const int size = 2;

struct student
{
  string name;
  string address;
  string city;
  string state;
  int zip;
  char gender;
  int idno;
  float gpa;
}; //student

void getInfo(int i, student* s)
{
// function to get students' information
  cout << "student " << i+1 << "'s Name? ";
  getline(cin, s[i].name);

  cout << "student " << i+1 << "'s address? ";
  getline(cin, s[i].address);

  cout << "student " << i+1 << "'s city? ";
  getline(cin, s[i].city);

  cout << "student " << i+1 << "'s state? ";
  getline(cin, s[i].state);

  cout << "student " << i+1 << "'s zip code? ";
  cin >> s[i].zip;
  cin.ignore(1000, 10);

  cout << "student " << i+1 << "'s gender? [M/F]";
  cin >> s[i].gender;
  cin.ignore(1000, 10);

  cout << "student " << i+1 << "'s ID number? ";
  cin >> s[i].idno;
  cin.ignore(1000, 10);
   
  cout << "student " << i+1 << "'s gpa? ";
  cin >> s[i].gpa;
  cin.ignore(1000, 10);
   
  cout << endl;
}

// function to print students' information
void printInfo(int i, student* s)
{
  cout << "=======student (" << i+1 << ")=======" << endl;
  cout << "Name: " << s[i].name << endl;
  cout << "Address: " << s[i].address << endl;
  cout << "City: " << s[i].city << endl;
  cout << "State: " << s[i].state << endl;
  cout << "Zip code: " << s[i].zip << endl;
  cout << "Gender: " << s[i].gender << endl;
  cout << "ID number: " << s[i].idno << endl;
  cout << "GPA: " << s[i].gpa << endl;
  cout << endl;
}

int main()
{
 // PrintStatements();

  student s[2];
  int i;

  for (i = 0; i < size; i++)
  {
    getInfo(i, s);
  } //getInfo

  cout << endl;

  for (i = 0; i < size; i++)
  {
    printInfo(i, s);
  } //printInfo

// End!
  cout << endl;
  cout << endl;
  cout << "Press ENTER to continue..." << endl;
  cin.get();
  return 0;
}
2011-12-12 09:24
快速回复:请帮我看看要怎么改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028367 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved