该程序无法正确地将文件中的信息存储到数组中? [英] The program does not properly store information from a file into arrays?

查看:155
本文介绍了该程序无法正确地将文件中的信息存储到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序正确编译并格式化了输出,但是输出信息错误.没有名称,数字不正确.
这是我的程序.

My program compiles and formats the output correctly, but the output information is wrong. There are no names and the numbers are incorrect.
Here is my program.

/*  Program:	prog6.cpp
	By:		Mackenzie Ritter
	Last Modified:	Dec 3, 2017
	Purpose:	To provide a rating for each elf based on their toy production and output all information.
	Notes:
 */
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std ;

string* elfRating (int, int[], string[]) ;

int main ()
{
    int SIZE = 50 ;
    string* names = new string[SIZE] ;		// Elf''s name
    int numToys[SIZE] ;		// Number of toys made by each elf
    string* rating = new string[SIZE] ;		// Rating for elves based on number of toys
    string line = " " ;
    ifstream ins ;
    ins.open ("elves.dat") ;
    int i = 0 ;
    istringstream iss (line) ;
    while (getline(ins, line))
    {
        istringstream iss (line) ;
        iss >> names [i] >> numToys [i] ;
        i++ ;
    }
    int total = 0 ;
    for (int num = 0; num < SIZE; num++)
    {
        total += numToys[num] ;
    }
    int cnt = 0 ;
    int max = numToys [0] ;
    while (cnt < SIZE)
    {
        if (numToys[cnt] > max)
        {
            max = numToys [cnt] ;
            cnt++ ;
        }
        else
        {
            cnt++ ;
        }
    }
    int count = 0 ;
    int min = numToys [0] ;
    while (count < SIZE)
    {
        if (numToys[count] < min)
        {
            min = numToys [count] ;
            count++ ;
        }
        else
        {
            count++ ;
        }
    }
    int ii = 0 ;
    while (ii < 50)			//printout of arrays
    {
        rating = elfRating(SIZE, numToys, rating) ; 
        cout << setw(20) << left << "Elves name:" << names[ii]
        << setw(20) << left << "Number of Toys Produced:" << numToys[ii]
        << setw(10) << left << "Rating:" << rating[ii] << endl ;
        ii++ ;
    }
    cout << "Total number of toys created by all elves: " << total <<endl;
    cout << "The greatest number of toys created is: " << max << endl ;
    cout << "The lowest number of toys created is: " << min << endl ;
    
    cin.ignore () ;
}

/*  Program:	prog6.cpp
	By:		Mackenzie Ritter
	Last Modified:	Dec 3, 2017
	Purpose:	To provide a rating for each elf based on their toy production.
	Notes:
 */

string* elfRating (int SIZE, int numToys[], string rating[])
{
    int t = 0 ;
    for (t=0;t<SIZE;t++)
    {
        if (numToys [t] < 200)
        {
            rating[t] = "-" ;
        }
        else if (numToys [t] < 300)
        {
            rating[t] = "*" ;
        }
        else if (numToys [t] < 500)
        {
            rating[t] = "***" ;
        }
        else if (numToys [t] >= 500)
        {
            rating[t] = "*****" ;
        }
        else
        {
            cout << "Error" << endl ;
        }
    }
    
    return rating;
}



任何帮助将不胜感激.

我尝试过的事情:

我已经将这个代码修复了很多次,因为它不断产生错误.现在,它可以正确编译,该信息是不正确的.应该获取文件并将信息存储到两个数组中:名称和numToys.出于某种原因,没有输出名称,并且数字非常错误.



Any help is greatly appreciated.

What I have tried:

I have fixed this code so many times because it kept producing errors. Now that it compiles correctly, the information is not correct. It is supposed to take the file and store the information into two arrays: names and numToys. For some reason, there are no names outputted and the numbers are extremely wrong.

推荐答案

您已经多次发布此问题,并在收到了一些有用的建议帮助理解错误吗? [
You have already posted this question a number of times, and received some useful suggestions at Help with understanding errors?[^]. You would make much better progress by not using the old C-style arrays, but move to some of the STL classes. Also you could create a class for your Elf type, which would contain all the information necessary for the program to manage. You elf class could be something like:
class Elf
{
    string name;
    int numberOfToys;
    int rating;
// constructors helpers etc.
};


然后,您可以使用std::vector<T>来保存所有的elf对象.


You could then use a std::vector<T> to hold all your elf objects.


这是一个有效的示例(尽管需要整理cout格式.请仔细看一下并尝试理解其逻辑,但不要将此作为自己的工作提交,因为它与您编写的内容有很大不同.
This is a working sample (although the cout formatting need to be tidied up. Take a good look at this and try to understand the logic, but do not submit this as your own work, as it is considerably different from what you wrote.
/*  Program:	prog6.cpp
By:		Richard MacCutchan
Last Modified:	Dec 3, 2017
Purpose:	To provide a rating for each elf based on their toy production and output all information.
Notes:
*/
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

string elfRating(int);
#define SIZE 50

int main()
{
	string names[SIZE];		// Elf's name
	int numToys[SIZE];		// Number of toys made by each elf
	string rating[SIZE];		// Rating for elves based on number of toys
	string line = " ";
	ifstream ins;
	ins.open("elves.dat");
	int total = 0;
	int max = 0;
	int min = INT32_MAX;
	istringstream iss(line);
	int count = 0;
        // collect all records and calculate the different values
	while (getline(ins, line))
	{
		istringstream iss(line);
		iss >> names[count] >> numToys[count];
		total += numToys[count];
		if (max < numToys[count])
			max = numToys[count];
		if (min > numToys[count])
			min = numToys[count];
		rating[count] = elfRating(numToys[count]);
		count++;
	}
        // count now contains the number of records that were read in
	int i = 0;
	while (i < count)			//printout of arrays
	{
		cout << setw(20) << left << "Elves name:" << names[i]
			<< setw(20) << left << "Number of Toys Produced:" << numToys[i]
			<< setw(10) << left << "Rating:" << rating[i] << endl;
		i++;
	}
	cout << "Total number of toys created by all elves: " << total << endl;
	cout << "The greatest number of toys created is: " << max << endl;
	cout << "The lowest number of toys created is: " << min << endl;

	cin.ignore();
}

/*  Program:	prog6.cpp
By:		Richard MacCutchan
Last Modified:	Dec 3, 2017
Purpose:	To provide a rating for each elf based on their toy production.
Notes:
*/

string elfRating(int numberOfToys)
{
	string rating = "None";
	if (numberOfToys < 200)
	{
		rating = "-";
	}
	else if (numberOfToys < 300)
	{
		rating = "*";
	}
	else if (numberOfToys < 500)
	{
		rating = "***";
	}
	else if (numberOfToys >= 500)
	{
		rating = "*****";
	}
	else
	{
		cout << "Error" << endl;
	}

	return rating;
}


这篇关于该程序无法正确地将文件中的信息存储到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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