Codelab: Loops with strings

/*
Take as your starting point the program you wrote for UNIT-00110.

MODIFY that program so that, for each state, it looks at all the polls in the state
and computes the average percentage for Obama and the average percentage for Romney for that state.
Each state is printed, and instead of the number of polls (which was the output of (110)) it is followed
by its two averages (first Obama's then Romney's), the name of the candidate who is "winning" (in the polls)
followed by the difference in the poll averages on a line by itself.

So if the contents of polls.txt were:

      AR 35 56 00 Sep 17 Sep 17 Hendrix Coll.
      AR 39 59 00 Jan 01 Jan 01 Election 2008
      CA 53 38 00 Oct 11 Oct 15 Princeton Survey
      CA 53 39 00 Oct 07 Oct 09 SurveyUSA
      CA 54 33 00 Oct 07 Oct 10 Pepperdine U.

the output would be:

      AR 37 57 Romney 20
      CA 53 36 Obama 17

Note that you are to use integer arithmetic and that means that numbers will be rounded down: (35+36)/2 == 35.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {
     ifstream pollFile("polls.txt");
     string state, notNeeded,prevState;
      int count, obama, romney, obamaSum, romneySum;
      bool firstState=true;

 pollFile >> state >> obama >> romney;
     while (!pollFile.fail()) {
          getline(pollFile,notNeeded);
        if (firstState){
            count=1;
			firstState=false;
            obamaSum=obama;
			romneySum=romney;
           
        }else if (state!=prevState){
            count=1;   
            obamaSum=obama;
            romneySum=romney;
           
        }else{
            count++;
            obamaSum+=obama;
            romneySum+=romney;
           
        }
        prevState=state;
       
        if((obamaSum/count)>(romneySum/count)){
            cout << state << " " << (obamaSum)/count << " " <<(romneySum)/count<< " " << " Obama"<< " " << obamaSum/count-romneySum/count<<endl;

           

        }else if((romneySum/count)>(obamaSum/count)){
            cout << state << " " << obamaSum/count << " " << romneySum/count << " " << "Romney" << " " << romneySum/count-obamaSum/count << endl;
           

   
        }
       
        pollFile >> state >> obama >> romney;   
       
    }
     return 0;
}





This program print out the state with its information more than once. I do not need duplicates but the last line of the state only. I am getting an output such as;

AZ 42 46 Romney 4
AZ 42 49 Romney 7
AZ 43 48 Romney 5
CO 46 50 Romney 4
CO 47 48 Romney 1
CO 47 48 Romney 1
NC 46 45 Obama 1


------------------------------------------------------------------------
this is my standard input.

AZ 42 46 00 Sep 25 Sep 26 Moore Consulting
AZ 42 52 00 Sep 25 Sep 25 Rasmussen
AZ 45 48 00 Sep 15 Sep 19 Purple Strategies
CO 46 50 00 Oct 21 Oct 21 Rasmussen
CO 50 47 00 Oct 16 Oct 18 PPP
CO 47 48 00 Oct 09 Oct 10 SurveyUSA
CO 49 48 00 Oct 07 Oct 07 Rasmussen
CO 46 50 00 Oct 05 Oct 08 ARG
NC 46 45 00 Sep 29 Oct 10 High Point University
Topic archived. No new replies allowed.