Narue 5,707 Bad Cop Team Colleague

Can you post a more complete example that still throws your error?

Narue 5,707 Bad Cop Team Colleague

If I'm reading your post correctly, you want to change how the watch window in your IDE shows arrays? I'll go out on a limb and say that you're SOL unless you want to write a plugin to alter the behavior.

Narue 5,707 Bad Cop Team Colleague

So much time wasted for such a simple task. I wonder if the OP really wants to do it and isn't just looking for a handout.

Narue 5,707 Bad Cop Team Colleague

No it is not a Class Assignment !

That's irrelevant at this point. We're cracking down on "begging" questions, so in the future you may find similar threads to this one deleted. By "begging" we mean questions that fail to show proof of effort such as described here.

Narue 5,707 Bad Cop Team Colleague

According to me,

This is an odd phrase. Just saying.

this code should produce indefinite result because of no specified null character at the end of the string

Correct.

Why is the null character present in the 13th memory location EACH TIME?

In practice, it depends on how your compiler generates the stack frame for main() and what direction the pointer is walking along the stack. But the correct answer is that this code invokes undefined behavior, so anything could happen and there's no useful explanation except that the code is broken and needs to be fixed.

Narue 5,707 Bad Cop Team Colleague

Why 50? Changing a user's name is quick and easy, provided the new name is available, so I don't see a problem with allowing it regardless of post count. Otherwise those with less than 50 posts would probably create a new account and take up both names, which just clutters the database.

Narue 5,707 Bad Cop Team Colleague

Line 23 will short-circuit if ptr is NULL and hence the CantGetHere() function won't be called, right?

Correct.

Just want to confirm that ||, like &&, always works from left to right.

Yes. Both are short circuited and have left to right associativity.

Narue 5,707 Bad Cop Team Colleague

You're not doing what you think you're doing, which is set a preprocessor symbol during compilation. Try using a compiler switch instead:

$(GPP) -D UBUNTU -g -c -o Tester_dbg.o Tester.cpp
Narue 5,707 Bad Cop Team Colleague

Who is Dani in this website?

Dani's member name is cscgal.

Where are most of the members of this website from? I think most of you are from India, aren't you?

Members are scattered all over the world.

Why do I see some professional members that have only 1 to 10 posts?

The majority of our members ask one question, get the answer, and don't return. The number of active members (those who post regularly) is relatively small compared to the total number of registrations.

How does it happen that people here misunderstand each other frequently?

Communicating with text is imprecise. You don't have the visual cues from face to face conversation, and the language barrier of non-native English speakers or even differences in education/writing ability can be significant.

Do some feel that others are their enemies??

I have no enemies here, though I'm confident that the feeling is not mutual given my posting history. ;)

jingda commented: Awesome answer +0
Narue 5,707 Bad Cop Team Colleague

Are you setting an environment variable, or creating a symbol definition? Those are two completely separate things. Can you show an example of the make file?

Narue 5,707 Bad Cop Team Colleague

What you probably want to do is accept any character in the loop, but only print it if it matches the criteria:

while (cin >> letter) {
    if (is_vowel(letter))
        cout << letter;
}

Obviously this is a non-working example that you would need to flesh out (such as writing is_vowel()), but it should suffice to show the concept.

Narue 5,707 Bad Cop Team Colleague

I thought it was just getting the address of the first element of the array.

That's exactly what it's doing. However, that address is being interpreted by scanf() as the starting address of a string. To print the address itself, use %p and cast to void* :

printf("%c %p %s\n", course[6], (void*)course, course);
printf("%c %p %s\n", course[6], (void*)&course[0], &course[0]);

Also note that in value context (ie. just about everywhere) course and &course[0] evaluate to the same thing.

Narue 5,707 Bad Cop Team Colleague

Can do.

Narue 5,707 Bad Cop Team Colleague

If the issue was with the IRC client, how do i go about resolving it?

Try a different client.

Or do you think this have to do with my internet?

It's possible, though I would expect symptoms to show up elsewhere as well. If your only problem is IRC and everything else works fine, your connection probably isn't the culprit.

Narue 5,707 Bad Cop Team Colleague

It could be your IRC client. I've been logged in all morning with no issues.

Narue 5,707 Bad Cop Team Colleague
if(v.empty()) cout<< "v is empty!" <<endl;

:icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

I don't have Visual C++ Express installed right now, but it should be in the linker settings. Just search through the project properties.

Narue 5,707 Bad Cop Team Colleague

Go to Project -> Properties -> Linker -> System -> Subsystem and choose Console.

oltokom commented: it was magic +0
Narue 5,707 Bad Cop Team Colleague

So crappy answers are most likely to be ignore.

Unless they otherwise violate the rules. It's not the moderators' job to ensure post quality. They simply enforce the rules.

Also duplicated post will not be dealt with but duplicate thread will, right?

Exact duplicates will be deleted. Your example was not an exact duplicate, so whether it would be deleted or not is up to the discretion of the moderator.

Narue 5,707 Bad Cop Team Colleague

Perfect example of what i am saying. Except no spam links.

We don't police content that isn't in violation of the rules[1]. On top of being draconian, the workload for mods would increase exponentially (there are a lot of stupid posts every day). If the posts have identical content with different wording, feel free to report the most recent one as a duplicate, but I won't guarantee that it will be treated as such.


[1] With the exception of the Viruses, Spyware and Other Nasties forum, where bad advice can be catastrophic.

Narue 5,707 Bad Cop Team Colleague

What happens when you set an int or double value to less than the minimum?

Bad Thingsâ„¢.

I need my program to recognize when a number is smaller then the minimum, and then set the value to equal the minimum instead.

Or you could just not let your value go below the minimum. These overflow/undeflow calculations aren't terribly difficult. It's certainly a lot better than invoking undefined behavior and hoping for the best.

Narue 5,707 Bad Cop Team Colleague

You are saying that "operator|(const Vec& a, int n)" has performance benefits over "operator|( Vec a, int n)", correct?

Yes. The former passes a reference while the latter calls the copy constructor to make a copy of the Vec object. With a class like Vec (or most user-defined types), I don't imagine that passing by value would be faster than by reference.

Narue 5,707 Bad Cop Team Colleague

For Vec a, i use "const" because a is never changed. Correct me if this is the wrong usage.

a is passed by value, so you're already working with a copy. While it's not wrong to pass by const value, it's not very common. The reason I think your parameter is wrong, is because a should be passed by reference for performance reasons. Calling the copy constructor to pass by value can slow things down drastically:

Mat operator|(const Vec& a, int n);

Now a is passed by const reference, which gives you the performance benefits of passing by reference without the risk of modifying the original object.

So my question is, should I use "const int n" in stack, because technically n doesn't change in stack, even though it changes in operator|.

I wouldn't bother, but passing by const value is a somewhat controversial topic. Pick whichever option you find most intuitive.

Also, should I use "const" within a function for a variable which will not change?

Once again, use your own judgment on what should be const in these cases. Eventually you'll have enough experience to know where the risk of modifying something that should change is enough to justify making it const and where it's unnecessary.

Narue 5,707 Bad Cop Team Colleague

Change this

for(d=z; d>=1; d--)

to this

for(d=x; d>x-z; d--)

Now tell me why that works and what you were doing wrong. :)

Narue 5,707 Bad Cop Team Colleague

Old threads are not automatically closed because there's no reason why the discussion could not be continued profitably by newcomers. We've discussed the issue of bumping before, and concluded that it's not enough of an issue to introduce such heavy handed methods.

However, bumping is indeed frowned upon if the new post contains nothing that adds to the thread. The reason it's frowned upon is because bumping a thread that was not originally on page 1 of the forum will drop a thread that was to page 2. Falling to page 2 is the kiss of death for a thread, and it's completely unfair to the OP.

As such, I prefer to delete bumps (without applying an infraction or warning) unless they have significant value. "Thank you" and "me too" posts do not fall under that category, so I would suggest reporting them as you see them.

It does bother me because most of them are signature spammers

That's another issue entirely. Spammers should be reported so that they can be banned as quickly as possible.

happygeek commented: yep +0
Narue 5,707 Bad Cop Team Colleague

Is there a simpler way to do it? If not, I'll just have to do some major reading, or I may just abandon this hobby project.

If those are your options, I'll go with no, there's not a simpler way to do it. If you choose to do some major reading then you're well suited to programming. If you choose to abandon the project, I'd suggest finding another hobby, because any project worth doing will push your limits. Quitting over so small a matter means you won't accomplish anything.

Narue 5,707 Bad Cop Team Colleague

Why is it a bad idea to use the std namespace for my new functions?

If you mean something like this:

namespace std {
   // Add myfunction() to namespace std
   void myfunction();
}

It's a bad idea because doing so invokes undefined behavior. If you mean something else, please clarify.

If all of my functions have names which are different than the names of std functions, it is no harm, right?

Hmm, how do you know all of your names are different? Why even bother cross referencing with the standard (and any other libraries you use) when you could just create a unique namespace and not worry about it?

Narue 5,707 Bad Cop Team Colleague

If you're taking two characters and turning them into the represented integer value, there's more to it than simply adding the two characters together:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string code;
    
    cout << "Enter a code: ";
    
    if (cin >> code) {
        vector<int> decoded;
        
        for (string::size_type i = 0; i < code.size(); i++) {
            int value = code[i] - '0';
            
            if (++i < code.size())
                value = 10 * value + (code[i] - '0');
                
            decoded.push_back(value);
        }
        
        copy(decoded.begin(), decoded.end(), ostream_iterator<int>(cout, "\n"));
    }
}
Narue 5,707 Bad Cop Team Colleague

Is C# better than ASP.net to download files automatically from the internet, update and retrieve information from databases (especially MySQL)??

I think you're confused. What exactly are you trying to do? Yes, you want to download files and connect to a database, but for what purpose?

Narue 5,707 Bad Cop Team Colleague

If you check syria's second post that includes his code it says "char main()" so I assumed it was correct

Yeah, it's not really the best practice to assume any kind of correctness from the code of obvious novices. ;) But hey, you can use this as a heads up to research things such as the valid definition of main(). Win.

Salem commented: win indeed +17
Narue 5,707 Bad Cop Team Colleague

Of all the errors I've seen for main() , this is the first time for this one...

Sadly, not the first time for me. Usually the rationalization is that char is an integer type and the return values are all well within the range of char.

Narue 5,707 Bad Cop Team Colleague

Hi,
I am writing an application in which I make
a map of strategies keyed by a string symbol. In
order to construct the strategies I pass the constructor
a filename, out of which it reads out some data, into an initializing map.

It is possible that some data might be missing from the initializing map,
in which case I simply want to skip this strategy. I am
thinking that, to implement this I need to throw an exception
if the data is missing. But I am unsure how to implement this, as
I have never used exceptions before.

Throwing an exception is one option, but is this truly an error condition? You're just catching the exception and swallowing it, which suggests alternative strategies. For example, a factory can be used to verify the file and pass the verified data into a private constructor. That way you can simply return a null pointer if the file is bogus rather than throw an exception:

class TradingStrategy {
    ...
public:
    static TradingStrategy *create(const string& file);
private:
    TradingStrategy(map<string, string> initMap);
};

TradingStrategy *TradingStrategy::create(const string& file)
{
    map<string, string> tempMap;
    
    // Initialize tempMap from the file
    
    if (tempMap.find("MuSCurve") == tempMap.end())
        return 0;
        
    return new TradingStrategy(tempMap);
}
while (symbolStream >> symbol) {
    TradingStrategy *ts = TradingStrategy::create(calFileDirectory_ + symbol);

    if (ts != 0)
        _strategies[symbol] = ts;
}

The issue here is that your TradingStrategy class is actually doing two things:

  • Parsing a file
  • Managing a …
Narue 5,707 Bad Cop Team Colleague

thus iterators predefined functions?

Iterators are a concept for range access with a design derived from pointers, where a concept is a set of requirements for accessing the range. For example, find() and for_each() can both be written with a minimal set of requirements (assumptions about the "pointer" type):

template <typename Iterator, typename T>
Iterator find(Iterator first, Iterator last, const T& value)
{
    while (first != last && *first != value)
        ++first;

    return first;
}
template <typename Iterator, class Action>
Action for_each(Iterator first, Iterator last, Action f)
{
    while (first != last)
        f(*first++);

    return f;
}

Notice that there's absolutely nothing that forces the Iterator template type to adhere to the concept, but the concept requirements for Iterator in both of these functions are easily seen:

  • Iterator must be copy-assignable (to pass as an argument by value)
  • Iterator must be equality comparable
  • Iterator must support indirection.
  • Iterator must support incrementing (pre and post).

Iterators are thus just a set of requirements sanctioned by the standard for maximum reuse among the standard algorithms and to make extending the standard algorithms with your own functions easier.

It's somewhat confusing because iterators aren't functions or types, or anything more than an idea, really. They don't take shape until you model the concept with a type that conforms to the requirements. You're not forced to model the concept exactly either, just the parts you know will be used.

I am confuse between function and predefined functions.

Let's …

Salem commented: Nice +17
Narue 5,707 Bad Cop Team Colleague

I know you asked about the conditional operator, but the following is a more conventional way of adding leading zeros to a fixed field width:

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
    short hour = 9;
    short minute = 10;
    short second = 5;
    
    char last_fill = cout.fill('0');
    
    cout << setw(2) << hour << ":"
         << setw(2) << minute << ":"
         << setw(2) << second << endl;

    cout.fill(last_fill);
}
Narue 5,707 Bad Cop Team Colleague

So I tell you to go research how set_difference works and the first thing you do is post back here asking how it works with a different account? If you're that lazy, I won't bother ever helping you.

Narue 5,707 Bad Cop Team Colleague

I just told you your first step: do some research on set_difference to learn how it works. If you don't understand the algorithm, implementing it is impossible.

Narue 5,707 Bad Cop Team Colleague

I know how to solve your problem, but the exercise isn't about getting someone else to do your work for you, is it? Were I you, I'd start by studying up on how the predefined functions work to get a better idea of the algorithm at play.

Salem commented: +1 +17
Narue 5,707 Bad Cop Team Colleague

Because it is a homework problem in which the prof said not to use STL algorithms, I guess.

That's my suspicion too, which is why I asked (somewhat rhetorically). ;)

Narue 5,707 Bad Cop Team Colleague

may I know what is another code without using std::unique? or .erase()??

Why do you ask?

Narue 5,707 Bad Cop Team Colleague

Are you just removing repeated characters, or truly removing duplicates? For example, what would the output be for "aaabaaa"? If it's the former, you can use std::unique quite easily to do the job:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s("aaaaanffddddgjkh");
    
    s.erase(unique(s.begin(), s.end()), s.end());
    
    cout << "'" << s << "'\n";
}
Narue 5,707 Bad Cop Team Colleague

Good god, are you really that clueless? It's the same damn thing as Insensus' example, just with GetTickCount() instead of time():

#include <iostream>
#include <windows.h>
 
int main()
{
    DWORD before = GetTickCount();
 
    std::cin.get();
 
    std::cout << GetTickCount() - before << " milliseconds passed";
}
Salem commented: Getting there in the end +17
Narue 5,707 Bad Cop Team Colleague

well, they had the code, but they didnt say where to put it.

Wow. Just...wow.

Narue 5,707 Bad Cop Team Colleague

ok, so are they telling you to put the GetTickCount() syntax in the time.h header file?

Um...what?

Narue 5,707 Bad Cop Team Colleague

The ctime library only supports a granularity down to seconds. For milliseconds you must either use a non-portable library (such as GetTickCount()). The Boost Date_Time library offers sub-second resolution if the underlying platform does as well (which Windows does).

Standard solutions include dividing the result by CLOCKS_PER_SEC (be sure to cast either of the operands to double) and hoping for the best, or taking advantage of the new <chrono> stuff in C++0x. Both are non-portable at this time. The former is unconditionally non-portable and the latter depends on adoption of the upcoming standard in compilers.

Narue 5,707 Bad Cop Team Colleague

Well, you don't seem to understand serialization at all if the problem is serializing a pointer rather than the pointed to data. The simplest method of serialization is converting the collective value of an object into a string. For example:

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class foo {
    string _id;
    vector<int> _data;
public:
    foo(const string& id, const vector<int>& data);
    string to_string();
    foo& from_string(const string& s);
};

foo::foo(const string& id, const vector<int>& data)
    : _id(id), _data(data)
{}

string foo::to_string()
{
    string result(_id + "[");
    
    for (vector<int>::size_type i = 0; i < _data.size(); i++) {
        result += boost::lexical_cast<string>(_data[i]);
        
        if (i < _data.size() - 1)
            result += ",";
    }
    
    return result + "]";
}

foo& foo::from_string(const string& s)
{
    // Assuming s is formatted properly for brevity
    
    string::size_type begin = s.find_first_of("[");
    string::size_type end;
    
    _id = s.substr(0, begin);
    
    while ((end = s.find_first_of(++begin, ',')) != string::npos) {
        string data_field = s.substr(begin, end - begin);
        
        _data.push_back(boost::lexical_cast<int>(data_field));
    }
    
    return *this;
}

int main()
{
    vector<int> v;
    
    v.push_back(11);
    v.push_back(22);
    v.push_back(33);
    v.push_back(44);
    
    foo f1("test", v);
    string s = f1.to_string();
    foo f2 = f1.from_string(s);
    
    cout << s << '\n' << f2.to_string() << '\n';
}
Narue 5,707 Bad Cop Team Colleague

May someone please explain to me why the rename function isn't working?

rename() sets errno. Call perror() to get a more useful error message.

Salem commented: Yes +17
Narue 5,707 Bad Cop Team Colleague

wouldn't that be a problem when I allocate a buffer with the size of the class(you know, to send it)?

You need to read up on serialization. Real serialization, not this shallow copy crap.

Narue 5,707 Bad Cop Team Colleague

Clear and redraw the text in a loop.

Narue 5,707 Bad Cop Team Colleague

>cout<<user;
user is a pointer, you need to dereference it first:

cout << *user;

Though bare pointers are so passé. You should prefer smart pointers unless there's good reason to do otherwise:

string name, pass;
 
cout << "enter name: ";
cin >> name;

cout << "enter password: ";
cin >> pass;

auto_ptr<CGuser> user(new CGuser(name,pass,0));

cout << *user;

Note: auto_ptr is used in the example because you're guaranteed to have it available. However, it's problematic in terms of ownership and has actually been deprecated in the upcoming standard. The replacement in the new standard is unique_ptr. You can also use boost::shared_ptr or tr1::shared_pointer with current compilers.

Narue 5,707 Bad Cop Team Colleague

But recently I was writing a string class for myself only to learn and find out that definition of overlaoding of << can also be in the class, my question is why is this working?

Magic. This definition is allowed, and has a few noticeable benefits:

  • The function is implicitly declared as inline.
  • Name lookup in the definition begins with the enclosing scope, which means you don't need to fully qualify names (this is a huge win for mitigating the verbosity of templates).
  • (Advanced) This definition technique ensures that the function can only be found through Koenig lookup.

The last point is why your what() function isn't visible. You would need to add a parameter of the my_string type for Koenig lookup to find it:

friend void what(my_string&) {
   ...
}

if I take of the friend from infront of the method name, it fails compiling.

Because a two argument operator<< member function is not allowed. The expected member function takes only a single argument:

ostream &operator<<(ostream &stream){
    stream << c_str;

    return stream;
}

However, this isn't often desirable because it flip flops the syntax for the operator such that the string object comes first rather than the stream object:

my_string<char> s("test");
s << cout << '\n';