1

I want to start of by saying that I am still learning and some might think that my code looks bad, but here it goes.

So I have this text file we can call example.txt.

A line in example.txt can look like this:

randomstuffhereitem=1234randomstuffhere

I want my program to take in the numbers that are next to the item= and I have started a bit on it using the following code.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

    string word;

int main()
{
    ifstream readFile("example.txt", ios::app);
    ofstream outfile("Found_Words.txt", ios::app);
    bool found = false; 

    long int price;
    cout << "Insert a number" << endl;
    cout << "number:";
    cin >> number;
    system("cls");
    outfile << "Here I start:";
    while( readFile >> word )
    {
        if(word == "item=")

Here is the problem; first of all it only searchs for "item=" but to find it, it cannot be included with other letters. It has to be a standalone word.

It wont find:

helloitem=hello

It will find:

hello item= hello

It has to be separated with spaces which is also a problem.

Secondly I want to find numbers next to the item=. Like I want it to be able to find item=1234 and please note that 1234 can be any number like 6723.

And I dont want it to find what comes after the number, so when the number stops, it wont take in anymore data. Like item=1234hello has to be item=1234

            {
            cout <<"The word has been found." << endl;
            outfile << word << "/" << number;
            //outfile.close();
                if(word == "item=")
                {
        outfile << ",";
                }

        found = true;
            }
    }
    outfile << "finishes here" ;
    outfile.close();
    if( found = false){
    cout <<"Not found" << endl;
    }
    system ("pause");
}
Csq
  • 5,775
  • 6
  • 26
  • 39
Thomja
  • 259
  • 5
  • 15

1 Answers1

0

You can use a code like this:

bool get_price(std::string s, std::string & rest, int & value)
{
    int pos = 0; //To track a position inside a string
    do //loop through "item" entries in the string
    {
        pos = s.find("item", pos); //Get an index in the string where "item" is found
        if (pos == s.npos) //No "item" in string
            break;
        pos += 4; //"item" length
        while (pos < s.length() && s[pos] == ' ') ++pos; //Skip spaces between "item" and "="
        if (pos < s.length() && s[pos] == '=') //Next char is "="
        {
            ++pos; //Move forward one char, the "="
            while (pos < s.length() && s[pos] == ' ') ++pos; //Skip spaces between "=" and digits
            const char * value_place = s.c_str() + pos; //The number
            if (*value_place < '0' || *value_place > '9') continue; //we have no number after =
            value = atoi(value_place); //Convert as much digits to a number as possible
            while (pos < s.length() && s[pos] >= '0' && s[pos] <= '9') ++pos; //skip number
            rest = s.substr(pos); //Return the remainder of the string
            return true; //The string matches 
        }
    } while (1);
    return false; //We did not find a match
}

Note that you should also change the way you read strings from file. You can either read to newline (std::getline) or to the end of stream, like mentioned here: stackoverflow question

Community
  • 1
  • 1
Aneri
  • 1,342
  • 8
  • 21
  • Problem is that I have no idea what that code does. I dont know what 99% of that is. My code is simpler, maybe not good... Probably horrible... But it's easy to understand. Nor do I know where to insert your code. – Thomja Mar 10 '13 at 19:53
  • So I made this code (No idea if this is what you had in mind but yeah...) EDIT: I cant paste code in here... where do I go to paste code??? I dont wanna answer my own question... – Thomja Mar 10 '13 at 19:57
  • If you intend to learn programming, you need to try to understand the code I posted. I have annotated it with per-line comments to simplify it. You can paste this code as a separate function (i.e. above `int main()`). Then you call it like `get_price(word, remainder, number)`. Your code does not solve the task, so you need to change it. I have shown you a sample how you can change it. I don't think you can post code in comments, but you can edit the question or upload code to pastebin.com and post a link. – Aneri Mar 10 '13 at 22:09