尝试使用int变量打印出文件的行号

时间:2017-03-27 17:28:27

标签: c++ printing numbers line

我正在使用c ++编写一个cs项目,我们必须打开一个文件读取这些文字并按字母顺序打印出来,然后在单词print旁边找到它所在的行。我使用递归函数来执行此操作但由于某种原因,我用于标记行号的计数器将不会更新。我尝试使用指针,但仍然没有。我可能已经完成了错误的指针,但我将int var设为全局,以便应该处理它,但仍然没有。我已经完成了作业,但我想知道为什么计数器从未奏效。这段代码中有一些hacks,比如将string转换为c_str(),但这只是为了尝试让我的OR参数工作,请忽略它们。 有什么建议吗?

#include<iostream>
#include<algorithm> //This is to do a comparison of ASCII characters.
#include<cctype>    //This is to convert capital letters to lowercase.
#include<string>    //This is to work with strings
#include<fstream>   //This is to work with getline().
#include<cstring>


using namespace std;
// Node
 struct node {
    int line;
    string word;
    node* left;
    node* right;
};
// MakeNode Function creates nodes
node* makeNode(string word, int line) {
    node* newNode = new node();
    newNode->word = word;
    newNode->left = newNode->right = NULL;
    return newNode;
    delete newNode;
}
// Function to insert new nodes into the tree.
node* Insert(node* root,string word, int line) {
    if(root == NULL) { // empty tree
        root = makeNode(word, line);
    }
    // If word is less then root-word
    else if(word <= root->word) {
        root->left = Insert(root->left,word, line);
        root->line = line;
    }
    // If word is greater then root-word
    else {
        root->right = Insert(root->right,word, line);
        root->line = line;
     }
    return root;
}
// Print function to print tree
void printTree(node* root)
{
     if (root == NULL){// If tree doesn't exist

      return;
     }
     else{

    printTree(root->left);
    cout<<root->word<<"\t"<<root->line<<endl;
    printTree(root->right);
    }
}






int main() {

int lineNum = 1; // Set line equal to one.
// cout<<lineNum<<endl;
node* root = NULL;  // Creating an empty tree
string word;        // Var to hold word

ifstream quote ("quote.txt"); // Opens the text file

getline(quote,word,' '); // Gets the words from the text file.

root = Insert(root, word, lineNum);
while(!quote.eof() || word != "#"){// While loop to read all words from text      file.
     int * line = lineNum;
        *line++;
    cout<<"This is the word right now: "<<word<<endl;
    char *newLine  = new char[word.length()+1];
    strcpy(newLine, word.c_str());

    cout<<"This is the newLine[0] value right now: "<<newLine[0]<<endl;
    cout<<"This is lineNum after the if"<<*line<<endl;

    if(newLine[0] == '\n'){// Increments line by keeping track of \n line char.


        cout<<"This is lineNum after the if"<<*line<<endl;
    }
    unsigned wordSize = word.size();
    if(wordSize > 10){// Shortens word if it is longer then ten chars.
        word.resize(10);
    }
    root = Insert(root, word, *line);
        quote>>word;

        delete[] newLine;





}// End of While

quote.close();


printTree(root);
}

/ ********************************************** *************************     这是报价文件的内容:

civilization of science
science is knowledge
knowledge is our destiny
#// this hash is to mark the end of the paragraph.



/**************************************************************************
This is a scaled down version of what the problem is, as you can see the 
*ptr variable is not updating.*/

int main()
{

int num = 1;
int *ptr = &num;

cout<<*ptr<<endl;

int key = 0;
while(key < 5){

    cout<<"This is *prt: "<<*ptr<<endl;
    cout<<"This is key: "<<key<<endl;
    key++;
    *ptr++;
}

return 0;
}

1 个答案:

答案 0 :(得分:0)

虽然Nathan Oliver的评估是正确的,但你可能也想看看这个:

<activity android:name=".MainActivity" android:screenOrientation="landscape">

我不确定你为什么要使用指针,但是考虑到你的其余代码,我不确定A)它正在做你认为它正在做的事情和B)为什么它不是&和#39; t崩溃。如果我是一个赌博的人,我打赌这是你的错误。