【发布时间】:2018-07-21 00:25:00
【问题描述】:
我只能在这个练习中使用字符串对象和字符串函数。当我试图把“超过”
基本上,
cout << winner << " over " << loser << " " << winnerscore << " " << loserscore << endl;
代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main(){
ifstream fin;
string winner, winnerscore, loser, loserscore, hey, file;
size_t pos, blank, blank2;
fin.open("C:\\Users\\leewi\\Desktop\\Computer Programs & Projects\\C++\\BentleyCIS22B\\Ex5\\Ex5.txt");
if (!fin)
{
cout << "Can't Open File." << endl;
exit(0);
}
while(!fin.eof()){
getline(fin, hey);
pos = hey.find(' ');
winner = hey.substr(0, pos);
if(isalpha(hey[pos+1])){
blank = hey.find(' ');
winner += hey.substr(pos, blank);
}
else if(isdigit(hey[pos+1]))
{
blank = hey.find(',');
winnerscore = hey.substr(pos, blank);
}
if(isalpha(hey[pos+1])){
blank = hey.find(' ');
loser += hey.substr(pos, hey.length());
}
else if(isdigit(hey[pos+1]))
{
loserscore = hey.substr(pos, hey.length());
}
cout << winner << " over " << loser << " " << winnerscore << " to " << loserscore << endl;
}
fin.close();
return 0;
}
我得到的输出:
Cincinnati over 27, Buffalo to 27, Buffalo 24
Detroit over 31, Cleve to 31, Cleveland 17
Kansas City over City 24, Oakland 7 31, Cleve to 31, Cleveland 17
Carolina over City 24, Oakland 7 35, Minnes to 35, Minnesota 10
Pittsburgh over City 24, Oakland 7 19, NY Jets to 19, NY Jets 6
Philadelphia over City 24, Oakland 7 31, Tampa Bay to 31, Tampa Bay 20
Green Bay over City 24, Oakland 7 Bay 19, Baltimore 17 31, Tampa Bay to 31, Tampa Bay 20
St. Lo over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 31, Tampa Bay to 31, Tampa Bay 20
Denver over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 35, Jack to 35, Jacksonville 19
Seattle over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 20, Tenne to 20, Tennessee 13
New En over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 England 30, New Orleans 27 20, Tenne to 20, Tennessee 13
San Fr over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 England 30, New Orleans 27 Francisco 32, Arizona 20 20, Tenne to 20, Tennessee 13
Dallas over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 England 30, New Orleans 27 Francisco 32, Arizona 20 31, Wash to 31, Washington 16
over City 24, Oakland 7 Bay 19, Baltimore 17 Louis 38, Houston 13 England 30, New Orleans 27 Francisco 32, Arizona 20 31, Wash to 31, Washington 16
我想要的输出:
Cincinnati over Buffalo 27 to 24
Detroit over Cleveland 31 to 17
Kansas City over Oakland 24 to 7
Carolina over Minnesota 35 to 10
Pittsburgh over NY Jets 19 to 6
Philadelphia over Tampa Bay 31 to 20
Green Bay over Baltimore 19 to 17
St. Louis over Houston 38 to 13
Denver over Jacksonville 35 to 19
Seattle over Tennessee 20 to 13
New England over New Orleans 30 to 27
San Francisco over Arizona 32 to 20
Dallas over Washington 31 to 16
【问题讨论】:
-
... 如果不是 Javascript/HTML/CSS,请不要过度使用代码 sn-p。
-
你的文件里有什么?请提供可验证的示例。
-
问题显然出在使用
hey.find()解析文件行的代码上。显示线条的示例,我们也许能够找出您做错了什么。 -
cout行很好,只是变量中的值有误。