我想要一些关于字符串函数的帮助

时间:2018-02-10 09:33:48

标签: c++ variable-assignment

我只能在本练习中使用字符串对象和字符串函数。当我试图把"结束" <<失败者。它没有首先输出失败者队伍的名字。此外,我希望将得分排列为winnerscore<< "到" << loserscore;

基本上,

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

1 个答案:

答案 0 :(得分:0)

我在循环中的第二个if中看到与循环中第一个if相同的条件。你在第二个if之前忘记了什么,也许是hey = hey.substr(pos); pos = hey.find(' ');

在同一秒if的正文中,未使用计算的blank。 也许必须有loser =,而不是loser +=

@barmar给了你一个有用的建议。