因此,基本上,我正在制作一个程序,用于存储学生信息以及学生在结构内部的得分。我需要帮助弄清楚如何将值存储到每个学生的矢量成绩中,因为有些学生每个人都有2-3个测试分数,并且所有信息都来自输入文件。
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
// Structures
struct Scores
{
int id;
string course = "";
int credit;
int score;
};
struct Person
{
int id;
string name = "";
string phone = "";
vector<Scores> grades;
};
// Function prototypes
void displayOne(vector<Person>, int);
int main()
{
ifstream inputFile;
ifstream inputFile2;
inputFile.open("StudentInfo.txt");
inputFile2.open("StudentScores.txt");
Scores tempScore;
Person tempStudent;
vector<Person> students;
if (inputFile)
{
int value = 0;
string name = "";
string phone = "";
while (inputFile >> value >> name >> phone)
{
tempStudent.id = value;
tempStudent.name = name;
tempStudent.phone = phone;
students.push_back(tempStudent);
}
}
else cout << "Error Opening StudentInfo.txt.. Try again." << endl;
displayOne(students, 12546);
displayOne(students, 15667);
displayOne(students, 14388);
inputFile.close();
inputFile2.close();
//displayOne(15667);
//displayAll();
return 0;
}
// Function definitions
void displayOne(vector<Person> students, int verifyID)
{
bool foundID = false;
int index = 0;
for (int i = 0; i < students.size(); i++)
{
if (students[i].id == verifyID)
{
foundID = true;
index = i;
}
}
if (students[index].id == verifyID)
{
cout << "Student ID: " << students[index].id << " ";
cout << "Student Name: " << students[index].name << " ";
cout << "Student Phone: " << students[index].phone << " ";
cout << "=================================";
cout << endl;
cout << students[index].grades[].course << endl;
}
}
我需要能够调用一个函数使我能够引用<<学生[0]-学生人数,并显示他们参加的每个班级以及他们取得的分数等等。
答案 0 :(得分:0)
看起来您可能必须使用ec2-user:~/environment/01-hw-saas/myrottenpotatoes $ cucumber features/AdMovie.feature
Using the default profile...
Feature: User can manually add movie
Scenario: Add a movie # features/AdMovie.feature:3
Given I am on the RottenPotatoes home page # features/step_definitions/web_steps.rb:44
Missing template movies/index, application/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/home/ec2-user/environment/01-hw-saas/myrottenpotatoes/app/views"
(ActionView::MissingTemplate)
./features/step_definitions/web_steps.rb:45:in `/^(?:|I )am on (.+)$/'
features/AdMovie.feature:4:in `Given I am on the RottenPotatoes home page'
When I follow "Add new movie" # features/step_definitions/web_steps.rb:56
Then I should be on the Create New Movie page # features/step_definitions/web_steps.rb:230
When I fill in "Title" with "Men In Black" # features/step_definitions/web_steps.rb:60
And I select "PG-13" from "Rating" # features/step_definitions/web_steps.rb:85
And I press "Save Changes" # features/step_definitions/web_steps.rb:52
Then I should be on the RottenPotatoes home page # features/step_definitions/web_steps.rb:230
And I should see "Men In Black" # features/step_definitions/web_steps.rb:105
Failing Scenarios:
cucumber features/AdMovie.feature:3 # Scenario: Add a movie
1 scenario (1 failed)
8 steps (1 failed, 7 skipped)
0m0.063s
ec2-user:~/environment/01-hw-saas/myrottenpotatoes $
来将学生映射到分数,例如SQL联接。
当您遍历 StudentScores.txt 并将每个分数读入临时source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.10'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'cucumber-rails', :require => false
gem 'cucumber-rails-training-wheels' # some pre-fabbed step definitions
gem 'database_cleaner' # to clear Cucumber's test database between runs
gem 'capybara' # lets Cucumber pretend to be a web browser
gem 'launchy' # a useful debugging aid for user stories
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
# Use Haml for templates
gem 'haml'
end
时,您将不得不在id
中找到与{{ 1}}。找到该学生后,您可以Scores
临时评分。
类似
student
Documentation for std::find_if
students
is a Lambda Expression。它提供了一种快速肮脏的功能,Scores::id
会对其进行调用以测试是否找到了它。
您可能会发现std::map
比push_back
更有助于存储Scores tempscore;
while (inputFile2 >> tempscore) // requires a >> overload for Scores, but here I'm
// just using it as shorthand for "read in a Scores."
// Strongly consider the >> overload though.
{
auto & id = tempscore.id; // make this c++11 proof. C++11 doesn't handle
// capturing members
// find student who matches id
auto found = std::find_if(students.begin(),
students.end(),
[id] (const Student& s) { return s.id == id; })
if (found != students.end()) // actually found a match
{
found->grades.push_back(tempscore); // add score
}
else
{
// handle missing student. Log it and discard? Abort? Your call.
}
}
。 [id] (const Student& s) { return s.id == id; }
使查找像
find_if
students
非常适合简单的搜索,但在迭代方面却不如std::vector
更好,而且开销更大,可以超过在短列表中节省的开销。如果学生名单很长,并且程序需要大量搜索std::map<id, Person> students;
,请使用Scores tempscore;
while (inputFile2 >> tempscore)
{
// find student who matches id
try
{
students.at(tempscore.id).grades.push_back(tempscore); // add score
}
catch (const std::out_of_range &)
{
// handle missing student.
}
}
。如果它需要扫描整个列表,那么最好使用std::map
。