如何将程序的结果输出到记事本? [英] How do I output my program's result to notepad?

查看:177
本文介绍了如何将程序的结果输出到记事本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个程序,要求20个名字和等级。

然后程序自动按字母顺序排列。

现在我试图输出结果a。 txt文件。但.txt文件没有在记事本中排列名称

我想知道我做错了什么以及如何修复程序以按字母顺序输出20个结果



我尝试过:



I made a program that ask for 20 names and grade.
Then the program automatically arrange it alphabetically.
Now I tried to output the result the a ".txt" file. But the .txt file didn't arrange the names in the notepad
I want to know what I did wrong and how can I fix my program to output the 20 results alphabetically

What I have tried:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
 ofstream outputFile;
 outputFile.open("earlfoz.txt")
 string name[20];
 int grade[20];
 int index[20];
 int i, j;

 for(i=0;i<20;i++)
 {
  cout << "Please enter name: ";
  cin >> name[i];
  outputFile << name[i] << endl;
  cout << "Please enter grade: ";
  cin >> grade[i];
  outputFile << grade[i] << endl;
  
 }

 for(i=0;i<20;i++)
 {
  index[i]=i;
 }

 for(i=0;i<20;i++)
 {
  for(j=i+1;j<20;j++)
  {
   int temp;
   
   if(name[index[i]] > name[index[j]])
   {
    temp = index[i];
    index[i] = index[j];
    index[j] = temp;
   }
  }
 }
 cout << endl;

 for(i=0;i<20;i++)
 { 
  cout << name[index[i]] << "        "
   << grade[index[i]] << endl;
 }

 cin.ignore();
 cin.get();

 outputFile.close();
 cout << "Done!\n";
 
}

推荐答案

首先,为此添加一个分号:
First, add a semi-colon to this:
outputFile.open("earlfoz.txt");



接下来,注释掉


Next, comment out the outputFile in

for(i=0;i<20;i++)
{
 cout << "Please enter name: ";
 cin >> name[i];
 //outputFile << name[i] << endl;
 cout << "Please enter grade: ";
 cin >> grade[i];
 //outputFile << grade[i] << endl;
}

然后将排序结果逐行输出到文件

then output the sorted result line by line to file

for(i=0;i<20;i++)
{
  outputFile << name[index[i]] << "        "
  << grade[index[i]] << endl;
}


Quote:

I想知道我做错了什么以及如何修复我的程序以按字母顺序输出20个结果

I want to know what I did wrong and how can I fix my program to output the 20 results alphabetically



让我们猜测,你在屏幕上得到排序列表,但文件包含列表,因为它输入。

重读你的代码并注意你用 outputFile 做什么,铃声应该开始响铃。



当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器,看看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Lets guess, you get the sorted list on screen but the file contain the list as it was input.
Reread your code and pay attention to what you do with outputFile, a bell should start to ring.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何将程序的结果输出到记事本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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