NathanOliver 429 Veteran Poster Featured Poster

I would re write your function like this

string backwards(string input)
{
    string temp;
    for (size_t i = input.size() - 1; i >= 0; i--)
       temp += input[i];
    return temp;
}

// then in you main function 
cout<<"Input backwards is : " << backwards(input);

FYI size_t is the type returned by the size() function. It is normally an unsigned int.

NathanOliver 429 Veteran Poster Featured Poster

you can use string streams to do this but you will to be able to put that number into an int. An int only holds integer values. You will need to put it into a double or float data type.

#include <sstream> // you will need this to use stringstream

string number = "6.538e-5";
double value;
stringstream ss;
ss << numbers;
ss >> value;
NathanOliver 429 Veteran Poster Featured Poster

What happens if you do this?

ofstream f1("s.dat", ios::binary);
//... output to file here
f1.close();
NathanOliver 429 Veteran Poster Featured Poster

if you use strings' find_first_of() to locate the < you can store the position of the < in a variable and then call find_first_of() for > starting at position + 1.

size_t firstPos = 0, secondPos;
string temp = "4ab030 <__do_global_ctors_aux>";
string sub;
if ((firstPos = temp.find_first_of("<", firstPos)) != string.npos)
    if ((secondPos = temp.find_first_of(">", firstPos + 1)) != string.npos)
        sub = temp.substr(firstPos, (secondPos - firstPos + 1))
NathanOliver 429 Veteran Poster Featured Poster

If you have long lines that go off the screen just separate it onto multiple lines

for (thisReallyLongVariableName = anotherVeryLongName; thisReallyLongVariableName < 
    somethingRealyComplex; thisReallyLongVariableName += moreOfTheSameStuff
{
    //...
NathanOliver 429 Veteran Poster Featured Poster

I believe this is because your function names and your variable names are the same. try changing all of your function names to start with a capital letter

double Radius(double x, double y, double xs, double ys);
double Diameter(double x);
double Circumference(double x);
double Area(double x);

// instead of

double radius(double x, double y, double xs, double ys);
double diameter(double x);
double circumference(double x);
double area(double x);

Also please use code tags when posting code. It makes it much easier to read.

NathanOliver 429 Veteran Poster Featured Poster

Okay to get the a char converted to an integer you have to subtract '0' from it. So if you have

char ch = '2';
int a = ch - '0';

Now a is 2 and not 50. What happens is '2' is 50 and '0' is 48 so 50 - 48 is 2.

NathanOliver 429 Veteran Poster Featured Poster

No problem. Glad to help.

NathanOliver 429 Veteran Poster Featured Poster

look at line 10 and line 29. Notice anything different?

int getScore(); // line 10
int getscore()  // line 29
{
    //...
NathanOliver 429 Veteran Poster Featured Poster

Well can you post what you have now? What Taywin posted should compile so I would need to see your code to find out what is going on.

NathanOliver 429 Veteran Poster Featured Poster

did you copy his code exactly? it seams to me like you might have getScore defined one way and declared another way.

NathanOliver 429 Veteran Poster Featured Poster

@aj79 You can create you own dynamic array class for this using nothing but standard c++ keywords. You don't even need <iostream>. You should just be able you use cin.get() in a loop though for this problem. get will grab a single character at a time and you can add that to you sum. You will have to remember to subtract '0' from the char to get the actual integer.

NathanOliver 429 Veteran Poster Featured Poster

can you post your current code that you have now?

NathanOliver 429 Veteran Poster Featured Poster

I think the problem is coming from the way you are using your if...else statements. On line 54 you check to see if the area is greater than 750 and if it is you add your base_cost with 750 and store it into total_cost but if its not you don't add the base_cost. Try walking through your code with your debugger and see if its doing what you think it should be doing.

NathanOliver 429 Veteran Poster Featured Poster

you could always write your own dynamic array class and use that to store the input into it then add up all of the digits. I'm not sure if that would be considered cheating since it uses the same approach as the STL.

NathanOliver 429 Veteran Poster Featured Poster

First void main is a big no no. main should return an int and only an int. Second <iostream.h> is depreciated and should not be used as well. You should use <iostream> . Lastly please use indentation in you code. It makes it much easier to read.

jonsca commented: Yes +4
NathanOliver 429 Veteran Poster Featured Poster

why cant you sort the list? Also what do you mean by there is no comparison operator? do you mean the each element of the list will hold a binary tree? please try to show in a little more detail what you are trying to accomplish and what your restrictions are.

NathanOliver 429 Veteran Poster Featured Poster

You can use string streams to convert a string into a number. this is from dave's new wiki

NathanOliver 429 Veteran Poster Featured Poster

@ first person Thanks for your suggestions.
I added constant declarers to what I think should be const. I also changed the conversion function's name to StringStreamConverter so It looks better since as Ed pointed out any type with an operator <<() can be used with this method. I did add a check to see if the conversion returned null and if it does than it exits the function and returns a null string. Could you please show me a case where the conversion would fail with string streams that wouldn't be caught by the compiler during compiling? Here is where I am now and thanks for the help guys.

// ContainerToString.h

#ifndef CONTAINERTOSTRING_H
#define CONTAINERTOSTRING_H

#include <string>
#include <functional>
#include <sstream>

// simple string stream conversion.  supports any type with an operator<<() function.
template<typename T>
class StringStreamConverter : std::unary_function<T, std::string>
{
public:
    std::string operator()(const T input) const
    {
        std::string output;
        std::stringstream ss;
        ss << input;
        getline(ss, output);
        return output;
    }
};

// builds the entire string from the contianer and returns the final string
// returns a null string on error
template <typename ForwardIterator, typename ConvertFunction>
std::string ContainerToString(const ForwardIterator first, const ForwardIterator last, ConvertFunction function)
{
    ForwardIterator start = first;  // did this because you cant use ++ on first now.
    std::string builder;
    std::string temp;
    while (start != last)
    {
        temp = function(*start) + " ";
        if (temp.empty())
            return string("");
        builder += temp;
        start++;
    }
    builder.erase(builder.size() - 1, 1);
    return builder;
}

#endif
// Main.cpp
#include …
NathanOliver 429 Veteran Poster Featured Poster

I take it that it works that way because it is a stream and all we are doing with string stream conversions in outputting to the stream and then inputting it back into a variable? Thanks for the approval. Its always nice to have someone like what I have written.

NathanOliver 429 Veteran Poster Featured Poster

Pretty nice page from what I have read so far. Good job. You might want to add the output created from the program just to be a little extra helpful.

NathanOliver 429 Veteran Poster Featured Poster

He all

I am currently working a on a problem to convert the entire contents of an STL container into a string. For example if I had this:

int foo[] = {1, 2, 3, 4, 5};
vector<int> bar(foo, foo + 5);

I would like to convert bar into the string "1 2 3 4 5". I decided to use templates with this so I could convert most of the STL containers. When writing my function for converting to a string I also created a class that takes a single element from the container and converts it to a string. The code that I have works the way I want it to right now. My question is I think I might have made this more complex than it needs to be but I'm trying to make it as flexible as I can. That's where the conversion class comes in. I think that it should be there so any type of data can be passed into the function and it can be converted. In my code I just wrote one for numbers.

// StringAndNumberConverter.h

#ifndef STRINGANDNUMBERCONVERTER_H
#define STRINGANDNUMBERCONVERTER_H

#include <sstream>
#include <string>

template <typename T>
std::string NumberToString(T number)
{
    std::string output;
    std::stringstream ss;
    ss << number;
    ss >> output;
    return output;
}

#endif
// ContainerToString.h

#ifndef CONTAINERTOSTRING_H
#define CONTAINERTOSTRING_H

#include <string>
#include <functional>
#include "StringAndNumberConverter.h"

// converts single elements into strings
// can be used for all forms of numbers.
template<typename T>
class NumberConverter : std::unary_function<T, std::string>
{ …
NathanOliver 429 Veteran Poster Featured Poster

Try wrapping the for loop in parentheses. I also split the macro into multiple lines.

#define forallXNodes(u,G) \
    (for(arc *bfs_ee=(G.getSource())->firstOutArc(),arc *bfs_stopA=\
    (G.getSource())->lastOutArc(),u=bfs_ee->head(); bfs_ee <= bfs_stopA;u =\
    (++bfs_ee)->head()))
NathanOliver 429 Veteran Poster Featured Poster

@ OP If your compiler evaluates both functions in this code than I suggest you get a new one.

#include <iostream>
using std::cout;

bool returnTrue ( ) { cout << "ret TRUE\n"; return true; }
bool returnFalse( ) { cout << "ret FALSE\n"; return false; }

int main()
{
    if (returnFalse() && returnTrue())
        cout << "toot\n";
}
NathanOliver 429 Veteran Poster Featured Poster
NathanOliver 429 Veteran Poster Featured Poster

@firstPerson The link you provided states that both list must be sorted by a strict weak sorting method. So the OP would have to call sort() on his list first before he sends them to std::set_difference

NathanOliver 429 Veteran Poster Featured Poster

what is the type of the variable sentence

NathanOliver 429 Veteran Poster Featured Poster

Could you post the function decleration? Also the function or part of code that you are using it in.

NathanOliver 429 Veteran Poster Featured Poster

Why are you limiting the size of the triangle to 21? An unsigned int can go to at least 30.

NathanOliver 429 Veteran Poster Featured Poster

No problem glad to help.

NathanOliver 429 Veteran Poster Featured Poster

Your program starts to fall apart with numbers larger than 15. You might want to rethink your algorithm.

NathanOliver 429 Veteran Poster Featured Poster

instead of what you have on lines 118 through 126 try doing this

int counter = 1;
for (int i = 1, j = 0; i < numberUsed; i++)
{
    if (entry[i] == entry[i - 1])
        counter++;
    else
    {
        store[j] = entry[i - 1];
        count[j] = counter;
        counter = 1;
        j++;
    }
}
Griff0527 commented: Provided an excellent example that still required me to correct my own errors +1
NathanOliver 429 Veteran Poster Featured Poster

Hey guys not sure if you realize but this thread was resurrected by yangclan. I didn't notice until he replied back that he had bumped a dead thread.

NathanOliver 429 Veteran Poster Featured Poster

what would happen if you change you include section to look like this

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <assert.h>

using namespace std;
NathanOliver 429 Veteran Poster Featured Poster

Sorry this is a little of topic but does Ein have anything to add to the conversation Edward?

NathanOliver 429 Veteran Poster Featured Poster

Making a 2d array for this is pretty strait forward. Because you don't know how many duplicates are in the array you would need to have an array like

int ** twoDarray = new int*[size];
for (i = 0; i < size; i++)
    towDarray[i] = new int[1];

Now all you have to do is loop through the array with a for loop that has two separate counter. One will be for the one D array and the other will be for the 2d array. Something like this

int counter = 0;
for (int i = 0, j = 0; i < size; i++)
{
    //...
}

As you are going through the loop test to see if the element at oneD is the same as the previous element and if it is increment counter. If it is not than then store the counter into twoDarray[j][0], then reset counter to 0 and then increment j. This isn't very elegant but it should get the job done.

NathanOliver 429 Veteran Poster Featured Poster

The way you have declared the templated function is wrong. if your function is a void function then instead of this

template <class T>
REGISTER_BUILDER<T>(Builder, T, "RecoBuild", ViewType::kAll3DBits);//(name, type, purpose, view)

Try this

template<class T>
void REGISTER_BUILDER(Builder, T, "RecoBuild", ViewType::kAll3DBits);//(name, type, purpose, view)

When you template a class the you have to use <...> but with functions you don't. When you actually go to use the code in you program you can write

REGISTER_BUILDER<int>(Builder, 5, "RecoBuild", ViewType::kAll3DBits);

Check this link out and you should see how it got confused. http://www.cplusplus.com/doc/tutorial/templates/

NathanOliver 429 Veteran Poster Featured Poster

Well i ran a couple of test with i being equal 10, 100, 1000 and 10000 and it appears that there is about a 3.5 to 1 ratio with the destructor calls against constructor calls. This is the code I used to test the amount of calls used.

#include <iostream>
#include <vector>

using namespace std;

int constructorCount = 0, destructorCount = 0;

class Foo
{
public:
    Foo() { constructorCount++; }
    ~Foo() { destructorCount++; }
};

int main()
{
    vector<Foo> bar;
    for (int i = 0; i < 10000; i++)
        bar.push_back(Foo());
    cout << "constructorCount = " << constructorCount << endl;
    cout << "destructorCount = " << destructorCount << endl;
    cin.get();
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

for a set of random letters like only vowels you can create an array that holds the vowels and then use rand % (size of array) to get the index for the element in the vowel array.

NathanOliver 429 Veteran Poster Featured Poster

Just turn right.

NathanOliver 429 Veteran Poster Featured Poster

Thanks iamthwee

NathanOliver 429 Veteran Poster Featured Poster

@ VirtualAssit Its nice to see that you are trying to help but please use code tags. Secondly the code that you posted is non standard. void main() is a big no no. main should only return an int according to the c++ standard. The function clrscr() you used is also non standard and should not be used. goto is also another non c++ standard item. try using loops for program flow control. Why are you returning the head to the list you created when you end the program? main should return 0 on success and some other int like -1, 1 on failure. return something the OS isn't able to support or needs to pass to something else than cant handle it can cause problems. Lastly please don't spam links.

NathanOliver 429 Veteran Poster Featured Poster

@ iamthwee
Here is the driver I used to test my function. The little dictionary file I made is also attached. The function was in a header called WordFinder.h and this is my Main.cpp

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "WordFinder.h"

using namespace std;

int main()
{
    string word = "individual", temp;
    vector<string> wordList;
    vector<string> foundWords;
    ifstream fin("dictionary.txt");
    if (fin.fail())
    {
        cout << "Unable to open file!";
        cin.get();
        return 0;
    }
    while (getline(fin, temp))
        wordList.push_back(temp);
    foundWords = AnyWordFinder(word, wordList);
    size_t size = foundWords.size();
    for (size_t i = 0; i < size; i ++)
    {
        cout << foundWords[i] << " found\n";
    }
    cin.get();
    return 0;
}
NathanOliver 429 Veteran Poster Featured Poster

Well the main problem with starting the construction of a class from the derived part up to the base is that any variables you are using that are in your base in a function in your derived class would be undefined. Assume this situation.

class A
{
    int number;
};

class B : public A
{
    void foo() { number++; }
};

In this situation the compiler would have no idea what number is when it tried to compile it if it were to start at B and go to A. Since it goes the other way there is no problem.

NathanOliver 429 Veteran Poster Featured Poster

well you have a doubly linked list so in reverse start at the last node and then traverse your way backwards by calling previous.

NathanOliver 429 Veteran Poster Featured Poster

Well I came up with a solution for this that I thinks works like iamthewee's code but I'm not sure. It will run through all the words supplied to the function in the vector against the string supplied to the function. It got away from me a little bit and is a little more complex than I thought it would be but it works for what I have tried.

#include <string>
#include <vector>
#include <set>

std::vector<std::string> AnyWordFinder(const std::string & input, const std::vector<std::string> & wordList)
{
    std::vector<std::string> foundWords;
    std::set<int> foundCharacters;
    std::vector<std::string>::const_iterator it = wordList.begin(), end = wordList.end();
    std::string temp;
    while (it != end)
    {
        size_t size = (*it).size();
        size_t pos = 0;
        if (size > input.size())
        {
            it++;
            continue;
        }
        for (size_t i = 0; i < size; i++)
        {
            pos = input.find_first_of((*it)[i], 0);
            if (pos == std::string::npos)
                break;
            if (foundCharacters.insert(pos).second != true)
            {
                // this sees if there are any more (*it)[i]'s in the input string
                while ((pos = input.find_first_of((*it)[i], pos + 1)) != std::string::npos)
                {
                    if (foundCharacters.insert(pos).second == true)
                        break;
                }
                if (pos == std::string::npos)
                    break;
            }
            temp += (*it)[i];
        }
        if (temp == *it)
            foundWords.push_back(temp);
        foundCharacters.clear();
        temp.clear();
        it++;
    }
    return foundWords;
}
NathanOliver 429 Veteran Poster Featured Poster

You are right with that arkoenig. I didn't realize that <= wasn't supported by bidirectional iterators.

NathanOliver 429 Veteran Poster Featured Poster

Sorry about the bad syntax with string length!. I should of written as (string length)!.

NathanOliver 429 Veteran Poster Featured Poster

I had a go at that last night and it can take a long time using words of a longer length. The number of possible permutation is string length!

NathanOliver 429 Veteran Poster Featured Poster

First - declare a counter variable and set it to zero.
Second - use a while loop with a getline call as your loop condition.
Third - increment the counter variable.
Fourth - see if the word you are looking for is in that line.
Fifth - If it is return the counter variable or if you want to see the entire line the word was found it return that.
Sixth - If not keep the loop going.