Bench 212 Posting Pro

Such a program is known as a 'Quine' - see here for more info http://en.wikipedia.org/wiki/Quine_%28computing%29

Salem commented: Beat me to it - Salem +4
Bench 212 Posting Pro

Hello:

I was wondering if anyone in this forum could point me in a direction (a link) where I could find practice projects (similar to Python Projects for the Beginner) dealing specifically with C++ classes and their related functions?

Thank-you in advance,
reRanger

Why not use some of the suggestions in that thread? most of the suggestions there could be suitable for just about any general purpose programming language (ignoring the PyGame stuff, even then, you could probably make simplified non-PyGame versions).

Bench 212 Posting Pro

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Thanks

Are you using C++ or C? if you're using C++ then investigate stringstreams, which can be found in the <sstream> library - stringstreams make this sort of thing really, really easy!

Bench 212 Posting Pro

Just a few comments, since andor has already pointed out what appears to be the main problem.

usingnamespace std;

typo above, was that a result of colouring in the code? or didn't you copy&paste your code? as a general rule of thumb, never attempt to retype code, because typo's happen - making it hard to give helpful advice - paste it instead.

You've got the variables total and number_buffer declared global - you only need them inside your main() function - you can pass them around to functions if you need to (which you have done), so there's no need for them to be in the global scope. Using global variables is a bad habit, so good design avoids them whenever possible.

The header "stdafx.h" is unnecessary - something which MSVC++ uses when you have precompiled headers enabled. there are loads of hits on google which tell you how to disable precompiled headers.

Bench 212 Posting Pro

Your professor sounds like he's leading you up the garden path - the system() function takes one argument (a const char*) and returns an int, which, in all cases i'm aware of, is purely an indicator for whether or not the call was successful (0 for success, and non-zero for failure). The option of storing the output in a text file sounds like the best one by far.

Bench 212 Posting Pro

Since you're using C++, have you done any research into STL and the standard library algorithms? these sorts of tasks are exactly the kind of thing which the STL excels at doing - eg,

#include <iostream>
#include <vector>
#include <algorithm>

template<const int N>
int array_size(const int (&arr)[N] )
{ 
    return N;
}

int main()
{
    typedef std::vector<int>::iterator iter_t;

//Just for the purpose of this example, populating
// an STL vector container using an array initialiser.
    const int lookup_table[] = { 2,3,5,5,11,13,11 } ;
    std::vector<int> my_vec(lookup_table, 
                            lookup_table + array_size(lookup_table) );

//Sorting all elements in the container:
// Make sure that all elements in the container are in order
// so that std::unique can identify all duplicates.
    std::sort(my_vec.begin(), my_vec.end());

    iter_t new_end;
    new_end = std::unique(my_vec.begin(),
                          my_vec.end() );

//Now the container is too big and has some unwanted data at the end
// lets get rid of those unwanted elements.
    my_vec.erase(new_end, my_vec.end());

    for( iter_t it = my_vec.begin(); it != my_vec.end(); ++it)
        std::cout << *it << std::endl;
}
Bench 212 Posting Pro

The binary-to-decimal link which Dave has given you is the 'C' way of doing things, which is fine, and you should learn to do it that way, although, C++ gives you a really nice easy way to do this - Using the <bitset> library. (a bitset is a container which only holds 1s and 0s)

#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    const int i = 100;
    const int total_bits = sizeof(i) * 8; // The number of bits in 'i'
    bitset<total_bits> b(i);
    cout << b << endl;
}

and, slightly more complicated, but really just the same logic in reverse

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

int main()
{
    unsigned long i;
    const int total_bits = sizeof(i) * 8; // The number of bits in 'i'
    const string s = "1100100";

    bitset<total_bits> b(s);
    i = b.to_ulong();  // turns 'b' into an unsigned long int
    cout << i << endl;
}
Grunt commented: Good-[Grunt] +1
Bench 212 Posting Pro

I'm actually not sure whether I should start with C, jump into C++ or bother learning the language at all. (mainly because of the massive time commitment)

I know I will be learning C++ for the next four years in university. But I also know you shouldn't blindly accept what school teaches you as Gospel. Countless articles on reddit talk are about how Java sucks, schools that teach it put out mediocre hackers, etc. etc.

If you're considering learning C purely as a grounding for C++, then please save yourself the wasted effort and skip 'C'. the two languages sometimes look similar at first glance (C++ inherited alot of the C syntax), but they are fundamentally completely different languages - even the "Hello World" programs you learn to write on day 1 for each language are completely different. (You've probably already written a "Hello World" in C, so here's a quick example in C++ to contrast)

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!";
}

I'd like to backup Grunt's reccomendations for Bruce Eckel's Thinking in C++, and Accelerated C++ by Koenig & Moo. Both excellent beginner's books. Also, C++ Primer by Lajoe, Lippman & Moo comes highly reccomended for beginners.

Francis Glassborow also has a book written for people with no prior programming or computer science knowledge, called You Can Do It. that's also an excellent book, but the compiler & libraries included with the book only work on Windows. (So learning from …

Bench 212 Posting Pro

if you run this code in c++ you get the following error at the underlined part is where the problem is
: error C2143: syntax error : missing ';' before 'constant'
: fatal error C1004: unexpected end of file found
Error executing cl.exe.

What you've got there is barely C++ at all, it looks like C code, but with dozens of syntax errors. Perhaps your editor didn't copy/paste it properly, but i think you need to do several things..

(1) break your code down into smaller chunks - you can easily move alot of that into header files and test each each bit more modularly.

(2) fix all the syntax errors, eg, void help=F1*>() Whatever on earth that is supposed to be, its illegal in C and C++.
..or..
(3) if the syntax errors really are just a problem with your IDE's copy and paste, then try copying the code from a plain text editor instead (eg, notepad)

(4) get rid of all the superfluous output formatting, text colouring and other "funky" stuff, in order to narrow down your problem. All its doing is adding massive extra baggage to your code, which is already cluttered. that sort of thing isn't helping anyone debug it. You can always add it back in when you're sure the rest of your program works fine.

Bench 212 Posting Pro

Hope you will try to understand the way these things have been derived and put some more effort next time.

I very much doubt it, you just gave him a free lunch. The real problem with doing other peoples' homework is that they've put no effort into it themselves, so they don't actually learn anything, having not been through the learning processes of thinking about the answers to the questions, or research from textbooks & notes. In the long run, spoonfeeding like that is really a disservice to learners.

A few words to ponder for the OP, intended for Mathematics students originally, but I think it applies to anybody who claims to be in the process of learning something new.

Using a calculator isn't cheating.
Looking up the answers at the back of the book to see how they got there isn't cheating.
Cheating is pretending to understand when you don't.
That is when you are cheating yourself

Bench 212 Posting Pro

I get a segfault when I run the following. Seems to fail at strcpy.

#include <cstring>
using std::strcpy;

int main()
{
    char * word = "word";
    strcpy(word, "what");
}

If I change char * word to char word[], it's fine. I thought that both declarations effectively do the same thing. What am I missing?

Thanks in advance.

They both do nearly the same thing. The version with a char * word = "word"; creates a pointer to the string literal "word". You cannot assign to or overwrite a literal.
eg, try compiling a program containing the line 5=7; and you'll get an error.

wheras the version with char word[] = "word"; creates an array of 5 characters in memory; 'w', 'o', 'r', 'd', '\0' . since the array is an object rather than a literal, its contents may be changed.

Grunt commented: Good -[Grunt] +1
Bench 212 Posting Pro

I think what you may be looking for is more general computer science theory than anything specific to C++ (especially if you have no interest in learning the language at this time). You should google around for OO design theory, since it applies to many other languages, not just C++ (in fact, if you are looking to learn a language purely for its OO features, you could choose to learn Smalltalk or Python instead).

Polymorphism in C++ might sound a little odd to you. it doesn't work on objects as such, but with pointers-to-objects (and if you don't know what a pointer is, then this explanation maybe won't be much help to you)

When a pointer is created, it's given a "pointed to" type. eg, for a non-polymorphic example, if a pointer has an int as its "pointed to" type, then you may point it to any object within your program which is of the type int.

If, on the other hand, the"pointed to" type is a class, then it may point to any object within your program which is of that class.
here's where polymorphism can happen, because the pointer may also point to objects derived from that class.

Let me reiterate - OO Polymorphism in C++ is not about changing objects, it's about changing pointers. You cannot change the data type of variables within an object. and you cannot even change an object from one derived type to another (Not safely anyway.. it's …

Bench 212 Posting Pro

[De-cloak] :eek:

Assuming that your array of random numbers is constant, then your 4 elements are always indexed by the numbers 0,1,2,3 .... that should make life pretty simple :) Start it out as a simple random-number picker for the numbers 0-3 and carry on from there.

[/De-cloak]

Bench 212 Posting Pro

I need to insert a "é" and other such symbols. At first I just tried using é straight out in C++ and it returned Θ. So I looked at my character map and did the unicode version \u00E9 where I wanted the é and it still returned Θ. I guess they are using two different standards. Can anyone point me to a list of such common special characters and their c++ encodings? My attempts at a google search have been inconclusive. Thanks :cheesy:

There's nothing standard about extended ASCII characters. the extended ASCII code for that character in your IDE's font is obviously different to the code in your output console's font.

using this code, the character é yields a value of -126 on my system

#include <iostream>

int main()
{
    char c;
    std::cin.get(c);
    std::cout << static_cast<int> c;
}
Bench 212 Posting Pro

And besides, doing someone's homework for them isn't actually helping them learn, They're just given the answer with no real understanding gained.

Salem commented: Absolutely! +1
Bench 212 Posting Pro

Can you provide me with another example working out so that I can use the example question and try get an understanding out of it and attempt on my quesion.

Thanks in advance....

You should really pick up Salem's point about decimal - Just imagine you're going back to infant school maths, where you add numbers using columns:

[Thousands] [Hundreds] [Tens] [Units] . [Tenths] [Hundredths]

eg, the number 194.3 is:

One hundred + Nine tens + Four units + Three tenths

In decimal, we get our column headers by powers of the base number (decimal is base 10)

1 unit * 10^-2 = 0.01 (hundredths)
1 unit * 10^-1 = 0.1  (tenths)
1 unit * 10^0 = 1 (units)
1 unit * 10^1 = 10 (tens)
1 unit * 10^2 = 100 (hundreds)
1 unit * 10^3 = 1000 (thousands)

in Binary, the principle is exactly the same.. except binary is base 2.

1 unit * 2^-2 = 0.25 (quarters)
1 unit * 2^-1 = 0.5  (halves)
1 unit * 2^0 = 1 (units)
1 unit * 2^1 = 2 (twos)
1 unit * 2^2 = 4 (fours)
1 unit * 2^3 = 8 (eights)

So, for the number 1001.0110, we need a table like this

[8] [4] [2] [1] . [0.5] [0.25] [0.125] [.0625]
 1   0   0   1  .   0      1      1       0

So, back to infant school maths - here's our calculation

One eight + No fours + No twos + One unit = 9
+
No halves + One quarter + One eighth + No sixteenths = 0.375

result = 9.375
Bench 212 Posting Pro

Continuing the conversation with myself... :)

Now that I've had some sleep, It's pretty clear that you don't want your operator<<() to be a member function of Stack, because the syntax to pass to cout is completely counter-intuitive, compared with the rest of STL. The "best" way is for you to make operator<<() a non-member function.

stupidenator commented: Bench was a lot of help! +1
Bench 212 Posting Pro

hi comwizz ;) ,
n hi 2 every1 else readin d reply :) . m a new member in the club.i dont get exactly wat u hve typed.... but i see in the foll code u has used j as a variable n incremented i... so look out..
/*
for(j=0;j<l_no;i++)
{
*/

A good way to avoid these sorts of bugs is to only declare the loop variable at the point you actually need to use it.
As far as I can see, the program does not need j anywhere outside the scope of the for loops in which it is used.

one remedy could be

for (int j=0; j<l_no; j++)  {}
SpS commented: Good:sunny +1