-3

I can only use string objects and string functions for this exercise. When I tried to put " over " << loser. It didn't output the loser team's name first. Also, I want the scores to be arranged as winnerscore << " to " << loserscore;

Basically,

cout << winner << " over " << loser << " " << winnerscore << " " << loserscore << endl;

Code:

#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;
}

Output I Got:

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

Output I Want:

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • ... don't overuse code snippet if it's not Javascript/HTML/CSS. – user202729 Feb 10 '18 at 09:36
  • 1
    [why `while (!fin.eof())` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Barmar Feb 10 '18 at 09:38
  • And what's there in your file? Verifiable example please. – user202729 Feb 10 '18 at 09:39
  • The problem is obviously with the code that's using `hey.find()` to parse the lines of the file. Show an example of the lines, we might be able to figure out what you're doing wrong. – Barmar Feb 10 '18 at 09:41
  • The `cout` line is fine, you've just got the wrong values in the variables. – Barmar Feb 10 '18 at 09:41
  • It would probably be a lot easier if you used a `std::stringstream` instead of `find()`. – Barmar Feb 10 '18 at 09:43

1 Answers1

0

I see in the second if in the loop the same condition as in the first if in the loop. Did you forget something before the second if, maybe hey = hey.substr(pos); pos = hey.find(' ');

In the body of the same second if the calculated blank is not used. Maybe there must be loser =, not loser +=.

@barmar gave you a useful advice.

273K
  • 29,503
  • 10
  • 41
  • 64