程序只循环一次 [英] Program only loop through once

查看:174
本文介绍了程序只循环一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The program reads text from an input file. My output is suppose to read:

Level         Score          Stars
----------------------------------
1              3840           **

2              5940           **

3             11560           **

4             18140           **

5             18780           **

星号假定与级别匹配。

程序只显示第一行,但程序被写入,循环可以显示总输出。
由于某种原因,程序循环只运行第一行代码。

The program only displays the first line but the program is written so the loop can display the total output. For some reason the program loop only runs through the first line of code.

我不知道为什么程序不循环多次。任何建议都会有所帮助。

I cant figure out why the program is not looping multiple times. Any advise would be helpful.

#include <iostream>
#include <cstring>
#include <cstdlib>
#include<fstream>

using namespace std;

int buildArrays(int A[],int B[],int C[])
{
    int i=0,num;
    ifstream inFile;
    inFile.open("candycrush.txt");

    if(inFile.fail())
    {
        cout<<"The candycrush.txt input file did not open"<<endl;
        exit(-1);
    }
    while(inFile)
    {
        inFile>>num;
        A[i]=num;

        inFile>>num;
        B[i]=num;

        inFile>>num;
        C[i]=num;

        i++;
    }
    inFile.close();

    return i;
}

void printArrays( string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int numberOfLevels )
{
    cout<<endl;
    cout<<reportTitle<<endl;
    cout<<"Levels\tScores\tStars"<<endl;
    cout<<"---------------------"<<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");
}


推荐答案

您的candycrush.txt文件在第三列中包含一个或多个 * 字符,但您尝试以整数形式读取该字符。这当然是一个错误,因此输入流的错误状态被设置。在此状态下,从此流中读取的数据将会失败。

Your "candycrush.txt" file contains one or more * characters in the 3rd column but you are trying to read that in as an integer. This is of course an error and so the input stream's error state is set. Subsequent reads from this stream will fail while it's in this state.

修复这个操作非常简单 - 只需读取最后一列作为一个完整的字符串。这也会简化你的 printArrays ,因为你不必再计数 *

Fixing this is pretty straightforward -- just read the last column in as a full string. This will also simplify your printArrays since you don't have to count the * anymore; just print it out as is.

这是你的 buildArrays 修正了一个实际大小,所以它' t overrun:

Here's your buildArrays fixed up a bit with an actual size so it doesn't overrun:

template <size_t maxsize>
int buildArrays(istream &is, int (&A)[maxsize], 
                             int (&B)[maxsize], 
                             std::string (&C)[maxsize])
{
  assert(is);

  size_t i;
  for(i = 0; i < maxsize && is; ++i)
  {
    is >> A[i] >> B[i] >> C[i];
  }

  return i;
}



您的主要打开输入文件,只是传递 ifstream into buildArrays

int main()
{
  const int MAX = 400;
  int levelsArray[MAX];
  int scoresArray[MAX];
  string starsArray[MAX];

  ifstream infile("candycrush.txt");
  int numberOfLevels = buildArrays(infile, levelsArray, scoresArray, starsArray);
  // and other stuff
  // ...
}

这篇关于程序只循环一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆