gpt4 book ai didi

c++ - 为什么我的程序不能正常工作

转载 作者:行者123 更新时间:2023-11-28 08:08:40 24 4
gpt4 key购买 nike

我的程序需要解析一个 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 未初始化,这是在 同时执行

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

关于c++ - 为什么我的程序不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9644701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com