繁体   English   中英

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

[英]Why Doesn't My Program Work Correctly

我的程序需要解析一个csv文件,并确定缺少的数字组合。 顺序无关紧要。

该程序会编译并运行,但会打印出文件中已打印在一行中的数字。

输入(mega2.csv):

123
134
142

注意234不在列表中。

预期输出:该程序应该输出234因为它是唯一未使用的组合。 相反,没有任何输出。

码:

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

main()中的从未被初始化,因此它包含不确定的值。
首先初始化它:

int count = 0;

编辑:
对于那些晚到聚会的人或那些匆忙投票但又不想真正阅读代码的人:

这里有两个count变量。 一个在main()范围内,另一个在do-while循环内。 循环内的count已初始化,但main()count未初始化,并且这是在do-while条件下使用的count

这是一个小片段 ,它演示了如果有人仍然难以理解这一点,我在说什么。

您使用rand()检测缺失组合的算法看起来非常可疑。 但是我想这是某种练习,所以我会让您自己解决这个问题。

您需要使用代码来解决一些问题,这将导致您所看到的令人困惑的行为。

  • 您的变量中只有一个被初始化。 他们应该被初始化,如下所示:

     string fileName = "mega2.csv"; 
  • 您有两个名为count变量。 您应该重命名它们(以及其他命名错误的变量)。 他们在计数什么?

  • 您无需检查文件是否已成功打开,否则不能采取适当的措施:

      if (!inFile) { std::cerr << "Could not open file [" << fileName << "]. Exiting." << std::endl; break; } 
  • 您不会检查变量是否已成功从文件中读取,如果没有,则应采取适当的措施。 鉴于您正尝试从文件中读取三个逗号分隔的值,但您的输入文件不包含任何逗号,因此这可能是一个问题!

  • 您不验证用户输入。

     cout << "Enter number of plays (or -number to quit): "; if (!(cin >> amount)) { std::cerr << "Invalid input" << std::endl; break; } 
  • 您有未使用的变量。 删除这些。

另外,您的main()做得太多。 尝试将您的代码分解成很多非常小的组件。 这将使您更易于测试和他人阅读。

暂无
暂无

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

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