难以弄清楚循环

Having trouble figuring out the loop

本文关键字:循环 弄清楚      更新时间:2023-10-16

程序从输入文件中读取文本。我的输出假设为:

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]){
  1. 您正在比较同一个数组
  2. 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;
}