简体   繁体   English

为什么我的程序无法正常工作

[英]Why Doesn't My Program Work Correctly

My program needs to parse a csv file and identify a missing combination of numbers. 我的程序需要解析一个csv文件,并确定缺少的数字组合。 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): 输入(mega2.csv):

123
134
142

Note 234 isn't in the list. 注意234不在列表中。

Expected output: The program is supposed to output 234 since it's the only combination not used. 预期输出:该程序应该输出234因为它是唯一未使用的组合。 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;
}
 int count;

in the main() is never initialized so it contains Indeterminate value. main()中的从未被初始化,因此它包含不确定的值。
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. 这里有两个count变量。 One in scope of main() and another inside the do-while loop. 一个在main()范围内,另一个在do-while循环内。 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 . 循环内的count已初始化,但main()count未初始化,并且这是在do-while条件下使用的count

Here is a small snippet which demonstrates what I am talking about if anyone still has troubles understanding this. 这是一个小片段 ,它演示了如果有人仍然难以理解这一点,我在说什么。

Your algorithm for detecting the missing combination using rand() looks deeply suspicious. 您使用rand()检测缺失组合的算法看起来非常可疑。 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 . 您有两个名为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. 另外,您的main()做得太多。 Try breaking your code down into much, much smaller components. 尝试将您的代码分解成很多非常小的组件。 This will make it easier for you to test and for others to read. 这将使您更易于测试和他人阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM