多维数组问题 [英] multidimension array questions

查看:53
本文介绍了多维数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我有2个文本文件,一个有学生姓名,

id#,#of course和course#,第二个文件有课程名称和

课程编号。我想创建一个多维数组,将

学生ID分为一部分,课程id分为另一部分。这是

我的代码:


包含< iostream>

#include< string>

#include< fstream>

使用命名空间std;


int main()

{

int i = 0;

int student_info [500] [25];

string first_name,last_name,course_name;


ifstream courses_file;

courses_file.open(" courses.txt");

if(courses_file.fail())

{

cerr<< 无法打开courses.txt <<结束;

退出(1);

}

ifstream students_file;

students_file.open(" students .txt");

if(students_file.fail())

{

cerr<< 无法打开students.txt <<结束;

退出(1);

}

int index = 0;

while(students_file> first_name> last_name>>

student_info [index] [25])

{

index ++;

while(courses_file> course_name> student_info [500] [i])

{

i ++;

}

}

}


当我打印出我的数组时,我得到了一堆垃圾。其中一个问题是

我正在接受first_name,last_name,数组中的学生ID号

但是之后还有一个学生正在学习的课程ID列表所以

就像一个随机数。我不能超过第一行中的学生ID

的文件。它打印出垃圾。有人可以帮我理解我的

问题是什么以及如何修复它?

I have this problem where I have 2 text files, one with student name,
id#, # of courses and course #, the second file has course name and
course number. I want to make a multidimensional array that takes the
student id into one portion and the course id into the other. This is
my code:

include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
int i = 0;
int student_info[500][25];
string first_name, last_name, course_name;

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
int index = 0;
while(students_file >first_name >last_name >>
student_info[index][25])
{
index++;
while(courses_file >course_name > student_info[500][i])
{
i++;
}
}
}

When I print out my array I get a bunch of garbage. One of problems is
I am taking in first_name, last_name, student id number in the array
but after that theres a list of course id''s the student is taking so
like a random number. I can''t get past the student id in the first line
of the file. It prints out junk. Can someone help me understand what my
problem is here and how to fix it?

推荐答案

On 2006年9月5日21:06:49 -0700在comp.lang.c ++中,foker

< br ************ @ gmail.comwrote,
On 5 Sep 2006 21:06:49 -0700 in comp.lang.c++, "foker"
<br************@gmail.comwrote,

> int student_info [500] [25];
> int student_info[500][25];



为什么幻数500和25?怎么可能总是

有相同数量的学生?

Why are the magic numbers 500 and 25? How can you possibly always
have the same number of students?


> string first_name,last_name,course_name;
> string first_name, last_name, course_name;



如何只存储一个学生姓名和课程名称,而不是全部

他们可能会满意吗?

How can only storing one student name and course name, not all of
them, possibly be satisfactory?


> while(students_file> first_name> last_name>>
student_info [index] [25])
> while(students_file >first_name >last_name >>
student_info[index][25])



你正在写一个结束阵列.. [25]最后一个是24?

为什么你总是想在最后一个位置写?

你忘了检查> 500输入记录。

You are writing one past the end of the array.. [25] the last is 24?
Why are you trying to write always in the last location?
You forgot to check for >500 input records.


> while(courses_file> course_name> student_info [500] [i])
> while(courses_file >course_name > student_info[500][i])



你正在写一个超过数组的末尾.. [500]最后一个是

499?你为什么要总是在最后一个位置写?


这个输入失败后,外部循环将永远不会从失败的流中读取


You are writing one past the end of the array.. [500] the last is
499? Why are you trying to write always in the last location?

After this input fails, the outer loop will never read any more from
the failed stream.


>当我打印出我的数组时,我得到了一堆垃圾。
>When I print out my array I get a bunch of garbage.



毫不奇怪,你从未初步化它。

你想要完成什么?


避免使用裸裸阵列进行应用程序编程。

首选std :: vector。

No surprise there, you never initialized it.
What are you trying to accomplish?

Avoid using bare naked array for application programming.
Prefer std::vector.


" foker"写道:


我真的不能告诉你要做什么;但无论如何我会提供一些随机的

评论。
"foker" writes:

I can''t really tell what you are trying to do; but I will offer some random
comments anyway.

>我有这个问题,我有2个文本文件,一个有学生姓名,

id#,#of course和course#,第二个文件有课程名称和

课程编号。我想创建一个多维数组,将

学生ID分为一部分,课程id分为另一部分。这是

我的代码:


包含< iostream>

#include< string>

#include< fstream>

使用命名空间std;


int main()

{

int i = 0;

int student_info [500] [25];
>I have this problem where I have 2 text files, one with student name,
id#, # of courses and course #, the second file has course name and
course number. I want to make a multidimensional array that takes the
student id into one portion and the course id into the other. This is
my code:

include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
int i = 0;
int student_info[500][25];



这是一个包含整数的数组。我怀疑你是否想要那个,我认为你的b / b $ b $变量更可能是字符串。甚至可能是课程编号。

课程编号没有合理的计算,是吗?它是否有意义将两个课程数相乘?

That''s an array that holds integers. I doubt if you want that, I think your
variables are more likely to be strings. Perhaps even the course number.
There is no reasonable calculation on a course number, is there? Does it
make sense to multiply two course numbers?


string first_name,last_name,course_name;


ifstream courses_file;

courses_file.open(" courses.txt");

if(courses_file.fail())

{

cerr<< 无法打开courses.txt <<结束;

退出(1);

}

ifstream students_file;

students_file.open(" students .txt");

if(students_file.fail())

{

cerr<< 无法打开students.txt <<结束;

退出(1);

}

int index = 0;

while(students_file> first_name> last_name>>

student_info [index] [25])
string first_name, last_name, course_name;

ifstream courses_file;
courses_file.open("courses.txt");
if (courses_file.fail())
{
cerr << "Could not open courses.txt" << endl;
exit(1);
}
ifstream students_file;
students_file.open("students.txt");
if (students_file.fail())
{
cerr << "Could not open students.txt" << endl;
exit(1);
}
int index = 0;
while(students_file >first_name >last_name >>
student_info[index][25])



你不想放*所有东西*进入元素[x] [25]你呢?更糟糕的是,

数组元素编号为0..24

You don''t want to put *everything* into element [x][25] do you? Even worse,
the array elements are numbered 0..24


{

index ++;

while(courses_file> course_name> student_info [500] [i])
{
index++;
while(courses_file >course_name > student_info[500][i])



你的意思是为每个学生阅读整个文件吗?如果你这样做,你应该

倒带。开始新学生之前的文件。过度使用

常数。你真的想制作一个混搭的信息吗?

信息和学生信息?

Do you mean to read the entire file for each student? If you do, you should
"rewind" the file before beginning a new student. Excessive use of
constants again. Do you really want to make a mish-mash of course
information and student information?


{

i ++;

}

}

}


当我打印出我的阵列时,我得到了一个一堆垃圾。其中一个问题是

我正在接受first_name,last_name,数组中的学生ID号

但是之后还有一个学生正在学习的课程ID列表所以

就像一个随机数。我不能超过第一行中的学生ID

的文件。它打印出垃圾。有人可以帮我理解我的

问题是什么以及如何解决它?
{
i++;
}
}
}

When I print out my array I get a bunch of garbage. One of problems is
I am taking in first_name, last_name, student id number in the array
but after that theres a list of course id''s the student is taking so
like a random number. I can''t get past the student id in the first line
of the file. It prints out junk. Can someone help me understand what my
problem is here and how to fix it?



我认为你应该首先解决(如果我的猜测是对的)你的逻辑问题。

也许先编写伪代码。使用铅笔和纸张,并为主要数据结构制作

的草图。我想*你正在尝试制作

两个外部文件的数组副本,然后你打算用这两个数组做一些事情 -

代码中没有已经写好了。常见问题解答包含有关

二维数组的信息,可能会有所帮助。我怀疑你真的想要

结构数组。结构是平民认为的记录,

本质上绑定在一起的信息。脱离背景你的第一个

名字和你的姓名实际上没有任何关系*你*他们是

只是随机人名的一部分。 。


例如:


结构学生

{

string first_name;

string last_name;

string course_name;

};


学生为[500]; //学生阵列。


这个常数被许多人所厌恶,数组的使用被许多人赞不绝口。让他们皱眉。

I think you should fix (if my guesses are right) your logic problems first.
Perhaps write pseudocode first. Use pencil and paper and make sketches of
your main data structures. I *think* you are trying to make array copies of
two external files, then you plan to do something with those two arrays - in
code that hasn''t been written yet. The FAQ has information on
two-dimensional arrays that may be helpful. I suspect you actually want
arrays of structures. Structures are what a civilian thinks of as records,
information that is intrinsically bound together. Out of context your first
name and your last name have virtually no relationship to *you* They are
simply parts of names of random people. .

For example:

struct Student
{
string first_name;
string last_name;
string course_name;
};

Student as[500]; // array of students.

The constant is frowned upon by many, the use of arrays are frowned upon by
many. Let them frown.


好吧,显然我做了一个非常糟糕的工作,解释我想要做什么

。我有2个文本文件,其中有500名学生。它有他们的学生姓名,学生编号,他们正在学习的课程数量,

然后是课程编号。第二个文本文件的名称为

课程和课程编号。所以我想要的是在

整个文本文件中为学生阅读。我想填补数组

student_info [500] [25]。我希望有500个学生ID和25个课程

id''。问题是我不能读过第一行,因为我的代码

是错的,我不知道该怎么做。这些文件的排列方式如下:

这个:Students_fil:[学生姓名] [学生证] [他们学习的课程数量
正在服用] [课程ID'他们在] Courses_file:[课程名称] [课程

ID]。

Ok apparently I did a really bad job explaining what I''m trying to do
here. I have 2 text files one that will have 500 students in it. It has
their student name, student number, number of courses they are taking,
then the course numbers. The second text file has the name of the
course and the course numbers. So what I want to do is read in the
whole text files for the students. I want to fill the array
student_info[500][25]. I want to have 500 student id''s and 25 course
id''s. The problem is I cant read in past the first line because my code
is wrong and I''m not sure how to do it. The files are arranged like
this: Students_fil: [Student Name][Student ID][Number of courses they
are taking][Course Id''s They are in] Courses_file: [Course Name][Course
ID].


这篇关于多维数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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