难以弄清楚循环
Having trouble figuring out the loop
程序从输入文件中读取文本。我的输出假设为:
Level Score Stars
----------------------------------
1 3840 **
2 5940 **
3 11560 **
4 18140 **
5 18780 **
它显示级别和分数,但问题是星星的显示。程序的其余部分显示星号而不是输出。
我似乎无法弄清楚为什么星星一直在循环而不是输出?
#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<cmath>
#include <fstream>
using namespace std;
int buildArrays(int A[],int B[],int C[]) {
int a, i = 0; // (I think this is where the looping problem begins)
ifstream inFile;
inFile.open( "candycrush.txt" );
if ( inFile.fail() ) {
cout << "The candycrush.txt input file did not open";
exit(-1);
}
inFile >> a;
while(inFile) {
A[i] = a;
inFile >> a;
B[i] = a;
inFile >> a;
C[i] = a;
inFile >> a;
i++;
}
inFile.close();
return i;
}
void printArrays( string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels ) {
cout << reportTitle << endl;
cout << "LevelstScorestStars" << endl;
for(int i = 0; i < numberOfLevels; i++) {
cout << levelsArray[i] << "t" << scoresArray[i] << "t";
for(int j = 0; j < starsArray[i]; j++) {
cout << "*";
}
cout << endl;
}
}
void sortArrays( int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels ) {
for(int i = 0; i < numberOfLevels; i++) {
for(int j = 0; j < numberOfLevels; j++) {
if(levelsArray[i] < levelsArray[j]) {
int temp1 = levelsArray[i];
int temp2 = scoresArray[i];
int temp3 = starsArray[i];
levelsArray[i] = levelsArray[j];
scoresArray[i] = scoresArray[j];
starsArray[i] = starsArray[j];
levelsArray[j] = temp1;
scoresArray[j] = temp2;
starsArray[j] = temp3;
}
}
}
}
int main() {
const int MAX=400;
int levelsArray[MAX];
int scoresArray[MAX];
int starsArray[MAX];
int numberOfLevels = buildArrays(levelsArray, scoresArray, starsArray);
printArrays( "Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
sortArrays( levelsArray, scoresArray, starsArray, numberOfLevels);
printArrays( "Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
system("pause");
}
for(int i=0;i<numberOfLevels;i++){
for(int j=0;j<numberOfLevels;j++){
if(levelsArray[i]<levelsArray[j]){
- 您正在比较同一个数组
- i 和 j 的初始值均为 0
因此,程序永远不会进入 if 语句的范围。
我想你的意思是:
for(int i=0;i<numberOfLevels;i++){
if(levelsArray[i]<levelsArray[i+1]){
您的问题可能从这里开始:
inFile>>a;
C[i]=a;
将一些字符"*"推入 int 变量/数组的地方。
你期望在这一点上出现什么? 数字 od 星或数字重复(例如 ASCII) 的起始字符
之后,您将星星与存储到此变量/数组中的数字进行比较
for(int j=0;j<starsArray[i];j++)
{
cout<<"*";
}
正如你所说:"程序的其余部分显示星号而不是输出"可能意味着你输出了一些(int)"**"星
星所以我认为你的意思是:
std::string t;
inFile>>t;
C[i]=t.length();
请参阅 std::字符串引用
将starsArray[i]
更改为starsArray[j]
。
发现此行代码上的错误:for(int j=0;j<starsArray[i];j++)
编辑:这是完整的代码:
#include <iostream> // access to cin, cout
#include <cstring>
#include <cstdlib>
#include<cmath>
#include <fstream>
using namespace std;
int buildArrays(int A[],int B[],int C[])
{
int a,i=0; // (I think this is where the looping problem begins)
ifstream inFile;
inFile.open( "candycrush.txt" );
if ( inFile.fail() )
{
cout << "The candycrush.txt input file did not opennn";
exit(-1);
}
inFile >> a;
while(inFile)
{
A[i]=a; cout<<a<<" n";
inFile>>a;
B[i]=a; cout<<a<<" n";
inFile>>a;
C[i]=a; cout<<a<<" n";
inFile>>a; cout<<a<<" n";
i++;
}
inFile.close();
cout<<" nnnnnn";
return i;
}
void printArrays( string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
{
//cout<<reportTitle<<endl;
cout<<"CAMDYCRUSH SAGA SCORES SORTER ----------------------------N";
cout<<"LevelstScorestStars"<<endl;
for(int i=0;i<numberOfLevels;i++)
{
cout<<levelsArray[i]<<"t"<<scoresArray[i]<<"t";
for(int j=0;j<starsArray[j];j++)
{
cout<<"*";
}
cout<<endl;
}
}
void sortArrays( int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
{
for(int i=0;i<numberOfLevels;i++)
{
for(int j=0;j<numberOfLevels;j++)
{
if(levelsArray[i]<levelsArray[j])
{
int temp1=levelsArray[i];
int temp2=scoresArray[i];
int temp3=starsArray[i];
levelsArray[i]=levelsArray[j];
scoresArray[i]=scoresArray[j];
starsArray[i]=starsArray[j];
levelsArray[j]=temp1;
scoresArray[j]=temp2;
starsArray[j]=temp3;
}
}
}
}
int main()
{
const int MAX=400;
int levelsArray[MAX];
int scoresArray[MAX];
int starsArray[MAX];
int numberOfLevels=buildArrays(levelsArray,scoresArray,starsArray);
printArrays( "Candy Crush UNSORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
sortArrays( levelsArray, scoresArray, starsArray, numberOfLevels);
printArrays( "Candy Crush SORTED Report", levelsArray, scoresArray, starsArray, numberOfLevels );
system("pause");
return 0;
}
相关文章:
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 弄清楚如何在C++中将整数读入 ASCII
- 写一个分数类,我无法弄清楚这些错误
- 分段错误(核心转储)但无法弄清楚
- N-queen问题:无法弄清楚为什么我的解决方案不起作用
- 为什么我的编译器无法弄清楚这种转换,它何时存在?
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 循环不继续无法弄清楚为什么
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 无法弄清楚为什么没有调用重载运算符 []
- 谁能弄清楚为什么我的循环会失败?
- C++需要弄清楚为什么这个 for 循环不起作用
- 我在while循环中遇到问题,我只是无法弄清楚为什么它无法按预期工作
- 我似乎无法弄清楚这种嵌套循环模式C++
- 难以弄清楚循环
- 有人可以帮助我弄清楚为什么我的 Do/While 循环不起作用吗?(C++)
- 我的 for 循环出错,无法弄清楚原因,我有一个"expected declaration"错误
- C++教授无法弄清楚的循环错误
- 简单的循环我无法弄清楚
- 无法弄清楚为什么这个输出格式化循环是无限的