-3

I am trying to read a file named elect12.csv elect12.csv sample in Excel and get the values in the last column (state) and compare it with a user inputted variable (stateCode). However, every time I run my program I am unable to prompt the user to reinput the value. How can I get this working? Thanks.

//Read popular votes file ifstream pVotesFile ("elect12.csv");

for (int i = 0; i < statecount; i++)
{

    if(pVotesFile.is_open())
    {   
        cout << "Enter state code: ";
        cin >> stateCode;
        while (pVotesFile >> obama >> romney >> other >> total >> state)
        {   
            if (stateCode == state)
            {
                statePoints = ((double)(obama - romney) / total) * 100;
                stateTotal += statePoints;
            }
            else
            {
                cout << "Enter state code: ";
                cin >> stateCode;
                stateTotal = 0;
            }
    }
}

If the picture happens to go down sometime in the future, here is what the first few lines of the file look like:

"795696 1255925 22717 2074338 AL"

"122640 164676 13179 300495 AK"

"1025232 1233654 40368 2299254 AZ"

"394409 647744 27315 1069468 AR"

"7854285 4839958 344304 13038547 CA"

  • Also - Can somebody please verify if I am reading the file correctly with ifstream? (Sorry for being such a noob haha) –  Oct 01 '16 at 00:34
  • 1
    No clue if you're reading it right or not because I don't know what the file looks like. If it truly is csv, you aren't parsing the commas. – user4581301 Oct 01 '16 at 00:41
  • Edit your question and add a sample of that file. Hosting it off site is a bad idea. First, All information relevant to the question should be contained in the question. As soon as that link goes stale, the question becomes useless. Second, This is the Internet. Downloading some random file off some random site is beyond stupid. – user4581301 Oct 01 '16 at 00:50
  • I added a picture of the file. Hopefully that works. –  Oct 01 '16 at 00:58
  • Picture doesn't help much. It can also be invalidated when imgur shuffles it's links around or garbage collects. Why not just copy and paste a few lines of the file into the question? – user4581301 Oct 01 '16 at 01:00
  • Reading the file over and is brutally time consuming. Instead create a structure that represents one line of data. Create a `std::map` of that structure. [Read lines one by one](http://stackoverflow.com/questions/7868936/read-file-line-by-line) into the structure and store the structure in the map with the state code as the key. Then you can have a simple loop that reads user input and prints the state data from the map. – user4581301 Oct 01 '16 at 01:02
  • How about reprompting the user for a correct input? –  Oct 01 '16 at 01:10
  • When posting a question on SO you can help yourself and help us to help you by trimming your code down to a minimal example of your problem, rather than dumping a glut of code and data that is not relevant to the question. There's no table of values here, no attempt to ask a question and validate it, but there is a whole bunch of other irrelevant gunk and a link to download some random file off the internet. – kfsone Oct 01 '16 at 03:07
  • I got the solution. The if statement was the one thing cause errors, not the while loop. –  Oct 01 '16 at 03:43

1 Answers1

1
"795696

You have extra quoatation marks in the file. As a result, cin >> integer_value; will fail, because it encounters text. You should change the data like so

795696 1255925 22717 2074338 AL
122640 164676 13179 300495 AK
1025232 1233654 40368 2299254 AZ
394409 647744 27315 1069468 AR
7854285 4839958 344304 13038547 CA

Use cout to make sure you are actually reading something:

if (pVotesFile.is_open())
{
    cout << "Enter state code: ";
    cin >> stateCode;
    while (pVotesFile >> obama >> romney >> other >> total >> state)
    {
        cout << obama << ", " << romney << ", " << other << ", " << total << ", "
                    << state << "\n";
        if (stateCode == state)
        {
            cout << "state found\n";
        }
    }
}

Alternatively, keep the data as is, then skip the first " quotation mark.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77