我正在提供我的C ++代码。 [英] Hereby I am providing my C++ code .

查看:75
本文介绍了我正在提供我的C ++代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>       
#include <fstream>
#include <cstdio>
#include <sstream>
using namespace std;
int main()
{
char w[20],c;
clrscr();
cout<<"Enter the word to be searched in the dictionary :- ";
cin>>w;
if(w[0]>=97 && w[0]<=122) 	//converting to upper case
w[0]=w[0]-32;
c=w[0];
string directory = "D:/";
string filename ="A";
string fileType = ".txt";
stringstream ss;
ifstream opener;
ss<<directory<<filename<<fileType;
opener.open(ss.str());
opener.close();
ss.str("");
string word = w;
ifstream readFromFile("A.txt");
string data;
while(!readFromFile.eof()){
getline(readFromFile,data);
if (data.find(word)!=string::npos) cout << word << endl;}
return 0;
}





我的尝试:



到目前为止,我尝试了许多方法,例如打开文件唱变量名并在文本文件中使用变量名称搜索。请检查此c ++文件中的错误和警告。

我想在一个文本文件中搜索用户输入的单词,该文本文件的名称由输入的单词的第一个字母分配。



What I have tried:

I have tried so far many ways such as opening a file sing a variable name and using the variable name search in a text file .Please check for the errors and warnings in this c++ file.
I want to search for a word entered by a user in a text file whose name is getting assigned by the first alphabet of the word which is entered.

推荐答案

Quote:

请检查此c ++文件中的错误和警告。

Please check for the errors and warnings in this c++ file.

否。我们不是编译器。

你有一个编译器或IDE - 用它来编译你的代码,它会给你一个错误和/或警告的列表。



并帮自己一个忙:缩进你的代码。挑选和缩进的风格(不介意哪个,K& R,Whitesmiths,甚至是可憎的1TB)并坚持下去。平放在左边,并在各处混合?不好主意。



然后当你干净利地编译它时,使用调试器来完成它的工作。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!

No. we are not a compiler.
You have a compiler or an IDE - use it to compile your code and it will give you a list of any errors and / or warnings.

And do yourself a favour: indent your code. Pick and indentation style (don't mind which, K&R, Whitesmiths, even the execrable 1TB) and stick with it. Flat to the left and mixed all over the place? Bad idea.

Then when you have it compiling cleanly, use the debugger to follow what it does. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


试试:

Try:
#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;


int main()
{
  string word;
  cout<<"Enter the word to be searched in the dictionary :- ";
  cin >> word;
  word[0] = toupper(word[0]);

  string path = string("D:/") + word[0] + ".txt";

  ifstream ifs(path);

  int count = 0;
  while ( ifs )
  {
    string line;
    getline( ifs, line);
    if ( line.find(word) != string::npos)
      cout << word << " found inside line " << count << "\n";
    ++count;
  }
}


这篇关于我正在提供我的C ++代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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