2012-03-10 105 views
0

我的程序需要解析一个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; 
} 
+0

请修改您的问题以获得更具描述性的标题并删除无关的代码部分。发布之前,您应该运行您发布的确切代码,并确保问题发生在其中。 – 2012-03-10 07:28:11

+2

你的标题看起来太含糊。 – 2012-03-10 07:29:03

+3

“代码中有一些不必要的东西,它们不影响任何东西,只是忽略它们。”你如何删除不相关的部分,并提供给我们一个[简短,自包含,正确(可编译),例子](http://sscce.org/)?这会让StackOverflow社区更容易帮助你。另请参见http://tinyurl.com/so-hints – Johnsyweb 2012-03-10 07:37:14

回答

1
int count; 

main()从未初始化,使其包含不确定的值。
第一初始化:

int count = 0; 

编辑:
对于那些迟到了或为那些谁别忙downvoted没有打扰到实际读取的代码:

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

这是一个small snippet这说明我在说什么,如果有人仍然有麻烦理解这一点。

+0

count也是他的局部变量....他已经初始化它... – 2012-03-10 07:33:35

+2

@ShashankKadne:除非我的眼睛背叛了我,'do-while'中使用的'count'是在范围内声明的'count' 'main()'并且永远不会被初始化。 – 2012-03-10 07:35:07

+0

我可以在他的内部看到int count = 0,而 – 2012-03-10 07:37:28

0

使用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()做得太多了。尝试将代码分解为更多更小的组件。这将使您更容易测试并让其他人阅读。