如何获取文件中的字符串以与while循环集成 [英] How do I get the string of words in a file to integrate with a while loop

查看:86
本文介绍了如何获取文件中的字符串以与while循环集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2017,我需要我的程序才能找到

1.打印文件中的所有单词

2.查找所有单词的平均长度单词

3.找到最短和最长的字长

4.文件中有多少个单词。文本文件中有79个单词,每行都是一个单词。文本文件名为Test.txt,我不能因为爱我而得到正确整合循环的字符串。



我尝试了什么:



一切。下面的代码应该可以工作,但它不能正确使用字符串。有人可以帮忙。这是大约5个小时到期,我完全失去了,因为我不知道还有什么可能遗漏



I'm using visual studio 2017 and I need my program to find
1. Print all the words in the file
2. Find the average length of all the words
3. Find the shortest and longest word length
4. How many words are in the file. There's 79 words in the text file and each line is a word. The text file is called Test.txt and I can not for the love of me get the string to integrate with the loop properly.

What I have tried:

Everything. The code below should work but it's not working with the string properly. Could someone just help. It's due in about 5 hours and I'm at a complete loss because i don't know what else could be missing

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

using namespace std;

int main() {
	ifstream file("Test.txt");
	string words;

	float wordCount = 0;
	float totalWordsCharacters = 0;
	while (file >> words) {
		wordCount++;
		totalWordsCharacters += words.length();
	}

	cout << "The average length of letters in each word is " << (totalWordsCharacters / 79) << endl;

	system("pause");
	return 0;
}

推荐答案

Quote:

下面的代码应该可以工作,但它不能正常使用字符串。

The code below should work but it's not working with the string properly.



然后,你的代码出了问题。

使用调试器将帮助你了解代码是如何错误的。



您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

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

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你告诉你发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



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


1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


Then, something is wrong in your code.
Using the debugger will help you to understand how code is wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

Debugger - Wikipedia, the free encyclopedia[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


你的代码错过了一些要求和功能丑陋的 79 幻数。另一方面,它显然工作和运作良好(我用一个简单的5行文本文件测试它。)

如果你仍然相信它不起作用那么,请提供输入文件并说明预期的输出。
Your code misses some of the requirements and features that ugly 79 magic number. On the other hand, it apparently works and works well (I've tested it with a simple 5 lines text file).
If you still believe it doesn't work then, please, provide the input file and state the expected output.


上面的两个解决方案是真实的,但我补充说你没有一些代码用于最小值和最大值:

Take the two solution above for real, but I add that you havent some code for the minimum and maximum value like that:
int min = 10000;//some foolish start
// in the loop
if( length < min ) {
  min = length;
}


这篇关于如何获取文件中的字符串以与while循环集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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