我目前正在学习Bjarne Stroustrup的初学者书,我在第4章练习19,所以请不要使用高于我的水平的代码。我不明白为什么if(name == names[i])
不起作用。
所以这是我目前的代码:
/*
Vladar Akos
Chapter 4 Exercise 19.
2018.03.26
Write a program where you first enter a set of name-and-value pairs, such as Joe 17 and Barbara 22. For each pair, add
the name to a vector called names and the number to a vector called scores (in corresponding positions, so that if
names[7]=="Joe" then scores[7]==17). Terminate input with NoName 0. Check that each name is unique and
terminate with an error message if a name is entered twice. Write out all the (name,score) pairs, one per line.
*/
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int>scores;
std::vector<std::string>names;
std::string name = " ";
int score = 0;
bool first = true;
std::cout << "Please write in a name and a score (terminate input with 'NoName') : \n";
while ((std::cin >> name >> score) && (name != "NoName") && (score != 0))
{
if (first == true)
{
scores.push_back(score);
names.push_back(name);
}
else
{
for (int i = 0; i < names.size(); i++)
{
if (name == names[i]) std::cout << "Same name as before!\n";
else
{
scores.push_back(score);
names.push_back(name);
}
}
first = false;
}
std::cout << "Please write more names and scores: ";
}
for (int i = 0; i < names.size(); i++)
{
std::cout << names[i] << '\t' << scores[i] << "\n";
}
}
如果两次输入名称,我该怎么做才能使程序抛出错误信息? P.S。:这是我的第一篇文章,如果出现问题就很抱歉
编辑:谢谢你的帮助!这对我来说是一个愚蠢的错误。 Swapnil 的答案解决了我的问题,但每个人都帮了很多忙。
答案 0 :(得分:1)
您应该在处理第一个输入后立即first
false
,否则它将永远不会输入其他内容。将first = false
置于else
语句之外。另一个问题是,一旦你发现有重复你应该停止搜索,也就是打破循环。:
while ((std::cin >> name >> score) && (name != "NoName") && (score != 0)) {
if (first == true) {
scores.push_back(score);
names.push_back(name);
}
else {
for (int i = 0; i < names.size(); i++) {
if (name == names[i]) {
std::cout << "Same name as before!\n";
}
else {
scores.push_back(score);
names.push_back(name);
break;
}
}
}
first = false;
std::cout << "Please write more names and scores: ";
}
答案 1 :(得分:1)
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int>scores;
std::vector<std::string>names;
std::string name = " ";
int score = 0;
bool first = true;
std::cout << "Please write in a name and a score (terminate input with 'NoName') : \n";
while ((std::cin >> name >> score) && (name != "NoName") && (score != 0))
{
if (first == true)
{
scores.push_back(score);
names.push_back(name);
}
else
{
bool duplicate = false;
for (int i = 0; i < names.size(); i++)
{
if (name == names[i])
duplicate = true;
}
if(duplicate)
{
std::cout << "Same name as before!\n";
}
else
{
scores.push_back(score);
names.push_back(name);
}
}
first = false;
std::cout << "Please write more names and scores: ";
}
for (int i = 0; i < names.size(); i++)
{
std::cout << names[i] << '\t' << scores[i] << "\n";
}
}
将false放在else之外,然后添加bool标志,我们在插入vector之前检查它们是否重复
连同输出
Please write in a name and a score (terminate input with 'NoName') :
JON
20
Please write more names and scores: SAM
50
Please write more names and scores: PETER
50
Please write more names and scores: PETER
70
Same name as before!
Please write more names and scores: SAM
89
Same name as before!
Please write more names and scores: NoName
0
JON 20
SAM 50
PETER 50
Press <RETURN> to close this window...