需要有关数组和文件I / O的帮助 [英] Need help with arrays and file I/O

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

问题描述

我试图从文本文件中计算4个整数的平均值,然后找出最大值和最小值。这是我到达的地方...我有问题读取整数并计算平均值..

I am trying to calculate the average of 4 integers from a text file and then find the maximum and minimum. This is where i have reached...I am having problems to read the integers and calculate the average..

展开 | 选择 | Wrap | 行号

推荐答案

我理解你的问题

首先把streamin的if语句放在你说streamin.open的行之后,

并且不要使用if streamout语句,直到你定义哪个文件streamout将被使用。


你的另一个问题是getline

你输入数字100作为其中一个参数,意思是来自文件的100个字符。并且您只需要读取4位数字而不是100.

通过它很好看
i understand your problem
first put the if statement of streamin after the line where you say streamin.open,
and don''t use the if statement of streamout until u define which file streamout is going to use.

your other problem is with getline
you are entering the number 100 as one of the parameters, which means 100 chars from the file. and you only need to read 4 digits not 100.
look through it well


我做了一些改变..平均部分不工作..可以你告诉我,如果我做对了..谢谢
I have made some changes..The average part is not working..can you tell me if im doing it right..thanks
展开 | 选择 | Wrap | 行号


您正在阅读输入内容

你正在把它读成char值,然后你将它添加到一个不起作用的数字。

你必须在一个数字上添加一个数字来加号到一个数字。

尝试将char转换为数字

如果你有char c =''1'';

int i = c - ''0'';

然后我== 1;

对于任何数字都是一样的,这只适用于char中的单个数字(建议在char中为多个数字执行);

你可以解决它
you are reading the input worng
you are reading it into char value and after that you are adding that to a number that will not work.
you have to add a number to a number to char to a number.
try to convert the char into a number
if you have char c=''1'';
int i =c-''0'';
then i==1;
the same thing for any number , this works only for single digit in a char (advicing to to do it for more than one digit in a char);
you can solve it


这篇关于需要有关数组和文件I / O的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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