user1260698
user1260698

Reputation: 11

Why Doesn't My Program Work Correctly

My program needs to parse a csv file and identify a missing combination of numbers. Order doesn't matter.

The program compiles and runs, but prints out numbers that are already printed in a line in the file.

Input (mega2.csv):

123
134
142

Note 234 isn't in the list.

Expected output: The program is supposed to output 234 since it's the only combination not used. Instead nothing outputs.

Code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int main()
{ 

    ifstream inFile; 
    string value;
    string fileName;
    int count;
    int amount, playCount;
    int a,b,c,d,e,f,g,h,i,j,k,l;
    srand(time(0));
    char ch;


do{

    cout << "Enter number of plays (or -number to quit): ";

    cin >> amount;

    cout << endl;

    playCount = 1;

    while( playCount <= amount ){

        do{

            inFile.open("mega2.csv");

            //create random numbers a,b,c,d,e,f= mega num < 10

            a = rand() % 5;

            if(a == 0){a = 1;}

            do{
            b = rand() % 5;

            if(b == 0){b = 1;}
            }while(b == a);

            do{
            c = rand() % 5;

            if(c == 0){c = 1;}
            }while(c == a || c == b);




            //Load numbers into g,h,i,j,k,l

            do{


            inFile >> g;
            inFile.get(ch);
            inFile >> h;
            inFile.get(ch);
            inFile >> i;
            inFile.get(ch);

        int count = 0;

        cout << g << "," << h << "," << i << endl;



    //A     
    if( a == g || a == h || a == i ){

        count++;
    }

    //B 
    if( b == g || b == h || b == i ){

        count++;
    }

    //C 
    if( c == g || c == h || c == i ){

        count++;
    }



}// close second half do loop

    while(inFile && count < 3);

    inFile.close();
    inFile.clear();


} // close whole do loop

    while(count >= 3);

    cout << endl;
    cout << endl;
    cout << endl;

    cout << a << "," << b << "," << c << endl;

    cout << endl;

    playCount++;

} // End playCount while loop

}// End main do loop

while(amount >= 0); // quit program with negative number

    system("pause");
    return 0;
}

Upvotes: 0

Views: 336

Answers (2)

johnsyweb
johnsyweb

Reputation: 141998

Your algorithm for detecting the missing combination using rand() looks deeply suspicious. But I guess this is some kind of exercise, so I'll leave you to figure this out for yourself.

Some issue that you need to address with your code which will lead to confusing behaviour like you are seeing.

  • Only one of your variables is initialised. They should all be initialised, like this:

    string fileName = "mega2.csv";
    
  • You have two variables called count. You should rename them (and other badly-named variables). What are they counting?

  • You don't check whether the file was opened successful and act appropriately if it is not:

            if (!inFile)
            {
                std::cerr << "Could not open file [" << fileName << "]. Exiting." << std::endl;
                break;
            }
    
  • You don't check whether the variables were read in from file successfully and act appropriately if they are not. Given you're trying to read three comma-separated values from your file but your input file doesn't contain any commas, this is likely to be a problem!

  • You do not validate the user input.

    cout << "Enter number of plays (or -number to quit): ";
    if (!(cin >> amount))
    {
        std::cerr << "Invalid input" << std::endl;
        break;
    }
    
  • You have unused variables. Remove these.

Additionally, your main() is doing much too much. Try breaking your code down into much, much smaller components. This will make it easier for you to test and for others to read.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206646

 int count;

in the main() is never initialized so it contains Indeterminate value.
Initialize it first:

int count = 0;

EDIT:
For those late to the party or for those who downvoted in haste without bothering to actually read the code:

There are two count variables being used here. One in scope of main() and another inside the do-while loop. The count inside the loop is initialized but the count in main() is not and that is the one that gets used in the condition of do-while.

Here is a small snippet which demonstrates what I am talking about if anyone still has troubles understanding this.

Upvotes: 1

Related Questions