【问题标题】:File input separation文件输入分离
【发布时间】:2012-11-01 17:46:34
【问题描述】:

目前,我正在为一个类编写一个程序,我们必须使用文件输入和输出。她给了我们一个文件,里面有 8 个不同的集合,每组 7 个数字(都是整数)。我们必须让用户输入他们想要阅读的那一行,然后输出最高数字、最低数字、数字集合的总和以及集合的平均值。我在让 XCode 只读取一行数字时遇到问题。我没有太多练习 getline() 函数,如果这甚至是一个解决方案的话。

有什么建议吗?

【问题讨论】:

  • 建议:从你能做的简单的事情开始。尝试。失败。重做。思考。成功。然后尝试更努力的事情。最终你将能够做到这一点。
  • '我在让 xcode 只读取一行数字时遇到问题'您能否详细说明一下,您能否展示一些您尝试过的代码。
  • 提示:使用std::ostringstream检查你可以用一行做什么
  • linput.open("Lab5input.rtf"); linput>>num1>>num2>>num3>>num4>>num5>>num6>>num7; cout
  • @RaymondAaron 你能把那个代码放在你的问题中吗,很难在评论中阅读......

标签: c++ file input output


【解决方案1】:

当然,这是一个家庭作业,这会牺牲效率来表明你知道某个主题,但代码应该给你一个模糊的想法。

注意**这仅在您的用户只能输入一行并且程序退出时才有效

cout << "enter line...";
cin >> line;
//error check line to make sure it is in bounds

///loops through each set of 7 numbers LINE times

//loop through line(FOR LOOP)

    //read in the 7 vars

//end loop

//loop through array(FOR LOOP)

    //add array[i] onto sum
    //is this number higher than the var HIGH? if yes then set high equal to array element
    //is this number lower than the low var? if yes then set low equal to array element

//end loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多