Banfa 597 Posting Pro Featured Poster

Tokens are just a group of characters that have been delimited in some way. So for instance you might talk about tokenising a string of comma separated values into tokens. You would mean extracting the strings appearing between the commas as separate values.

Tokenising is often, but not necessarily, done 1 token at a time.

Your first listing is better, however both of them have the class data members as public. In the real world you tend to keep all your class data either protected or private to implement the OO principle of encapsulation.

As such the first listing is better because it alread has some of the required work to make the data private done (i.e. a constructor that allows the object to be constructed with default values. Properly developed I would expect this class to end up as

class person 
{
public:
    person(const string& name_str, int age_int)
        :name (name_str), age(age_int) { }

    void setName(const string& name_str){ name = name_str; }
    string getName(){ return name; }

    void setAge(int age_int){ age = age_int; }
    int getAge(){ return age; }

private:
    string name;
    int age;
};

Actually you may still want to add a default constructor, copy constructor and assignment operator to this.

Banfa 597 Posting Pro Featured Poster

It needs to have been included before you head is included.

The compiler reads everything in the order it appears in the files. So it reads the c file when it gets to a #include it opens and reads that if it finds another #include it then opens and reads that* taking each statement in turn. It requires everything it needs to compile a statement to have been already defined in what it has already parsed.

* Actually the pre-processor reads all the files and creates 1 large intermediary file (which you can often get the compiler to output if you desire) which is read by the actual compiler in a single pass.


So I guess in you c file you include your own header before stdio.h, you can either

  1. Alter the order of the headers in you c file
  2. Include stdio.h into your header**

** header files are normally written to protect against double inclusion to allow this (i.e. many headers and C file might include the same header). Also some coding standards prohibit inclusion of headers into other headers but that is a matter of style.

Banfa 597 Posting Pro Featured Poster

This s[0]&0x0F is an unusual way to convert and ASCII digit to its binary equivalent and is not portable relying on the actual character values in the ASCII character set.

A slightly more usual way and one that is portable would be s[0] - '0' which rather than relying on the actual values of the character set relies on the stated behaviour in the C Standard and works for any character set.

Banfa 597 Posting Pro Featured Poster

You are probably missing the definition of FILE which is in stdio.h

Banfa 597 Posting Pro Featured Poster

How different a time does it give? Have you accounted for the inherent error that is always present whenever you try to take some sort of measurement.

Banfa 597 Posting Pro Featured Poster

I see no new function in that code.

Surely by now you have learnt when posting to post all the information including errors?

Banfa 597 Posting Pro Featured Poster

What operating system are you running on?
What version of libraries are you using?

Banfa 597 Posting Pro Featured Poster

Line 12 you declare Imprime as void Imprime(int numerocarro, int numlugar, int situacao, int tempo); then line 93 you define it as

#
void Imprime(int numerocarro, int numlugar, int situacao, float tempo){

.

The type of the last parameter differs which is a mistake. The compiler compiles your code assuming the declaration on line 12 which works because of function overloading and then the linker throughs the error because you have not defined a function like the one declare on line 12.

Fix line 12.

Banfa 597 Posting Pro Featured Poster

While I seem to be certain that there are compilers that do this none of the ones I current use (mingw and Visual C++) do.

Lint does though.

Banfa 597 Posting Pro Featured Poster

There are plenty of other cases where you might have a loop with no body. Any calculation or algorithm where the loop actually does the calculation, for instance finding the end of a singly linked list

// Should check head is not NULL before this
for(current=head; current->next != NULL; current = current->next)
    ;
// Current now points to last entry in list

Also note that there is a semi-convention to put intentional ; following a loop on the next line of code.

In fact some compiles will produce a warning for a ; on the same line as the loop but not for a ; on the next line.

Banfa 597 Posting Pro Featured Poster

Generally speaking a definition actually creates something while a declaration just tells the compiler it exists.

This can be done for both variables and functions. A variable definition can also include a value for initialisation.

So for variables

// Assume all at file scope

int i;      // This is a definition, you'd expect to find it in a .c file

int ii = 5; // This is a definition with initialisation , you'd expect to find it in a .c file

extern int i; // This is a declaration, you'd expect to find it in a .h file

extern int iii = 5; // This is a definition with initialisation, you'd expect to find it in a .c file

The definitions on lines 3 and 5 cause the compiler to reserve memory for the variable, line 5 also provides an initialisation value. Either of these can also be written with the static keyword to limit the variables scope to the current file, otherwise the variable implicitly has global scope.

Line 7 is a declaration, it says only that the variable exists, the compiler will not reserve memory, the linker will have to resolve this symbol.

However line 9 is a definition again because it contains an initialiser, the compiler will reserve memory. Additionally it is explicitly declared as have global scope.

For a function

int Myfunction(int g);  // This is a declaration

// This is a function definition
int Myfunction(int g)
{
    return …
Banfa 597 Posting Pro Featured Poster

How do you propose to get round users changing the lan card because it has gone wrong, or something similar?

With this methodology you strictly do not need to be able to recover the MAC-ID from the encrypted text file. All you need to do is re-encrypt the MAC-ID and check that the result is the same as what is already in the text file. Just use a SHA-1 hash or something similar.

Banfa 597 Posting Pro Featured Poster

You probably should declare clientaddr as a SOCKADDR_STORAGE rather than a sockaddr_in to ensure that clientaddr is large enough for any data that accept might write to it.

However that may not actually cure your problem.

Banfa 597 Posting Pro Featured Poster

C++ has no type long long, that is a C99 type

Banfa 597 Posting Pro Featured Poster

A good TCP/IP (and UDP) primer is Beej's Guide to Network Programming which is downloadable in various formats or available as an actual book.

@kings_mitra Doh you beat me to it :D

Banfa 597 Posting Pro Featured Poster

The .o is the object file that is later linked into the program and Dev=C++ almost certainly does create the same file although it might name it .obj or do a better job of hiding it.

Link Libraries are any additional libraries you need to link your program against, libws2_32.a or just ws2_32 (that is the Windows Socket library) for example. Linker options are for extra switches to the linker program.

The make program is a utility that actually controls the build. Many IDEs don't necessarily call a make program to perform the build since they often contain that functionality themselves but some projects are supplied with a makefile which defines how to build the project and in that case you build the project (common called a Make File Project) by passing the makefile to the make program.

Wikipedia as an article that will give you the basics about makefiles.

Banfa 597 Posting Pro Featured Poster

If you are on a Unix system you can always just save the file and go command line

g++ file.cpp

Actually that works on Windows too

mingw32-g++ -Wall -pedantic file.cpp

or

cl file.cpp

for Microsoft

Banfa 597 Posting Pro Featured Poster

sscanf is not up to dealling with all those different formats. You would be better off searching the string for // and then checking to see if it is followed by a digit and then if it is using atoi or strtol to convert that to a binary value.

Banfa 597 Posting Pro Featured Poster

I use Notepad++ extensively at work, along side Eclipse and Visual Studio, and have never noticed this, I will have to check tomorrow.

Of course Geany has the advantage over Notepad++ that it is available for both Windows and Linux.

Banfa 597 Posting Pro Featured Poster

Eclipse doesn't you have to create a project. In fact in almost all IDEs I have used you have to create a project first.

If rather than working on a few multi-file projects you need to work on lots of single file programs I would recommend Geany. This is less of an IDE and more of an editor but it does allow you to compile and link any file without the need for project files.

Banfa 597 Posting Pro Featured Poster

I like Eclipse since it is cross platform, I don't have to remember different keyboard short-cuts as a switch back and forth between Linux and Windows. Although it does have a few features I find irritating, but is also has features I miss when using VC++.

CodeBlocks is good too for that reason, also it is rather more light-weight than Eclipse.

The problem with Dev-cpp is that it has been out of support for a while now (> 2 years) and the compiler that comes with it is also out of date.

Banfa 597 Posting Pro Featured Poster

Actually for an array with program lifetime (i.e. one not declare in a function or declared static in a function) you don't even need the = {0} . The compiler initialises all such arrays and variables to 0 if no other value is given.

Banfa 597 Posting Pro Featured Poster

What are you expecting the result of that series to be?

Banfa 597 Posting Pro Featured Poster
main()
{
	void USERF(double *,double *,double);
.
.
}

You can't declare functions-in-functions in C, perhaps in JavaScript or Pascal but not in C/C++. Take that declaration outside main().

Actually you are wrong, you can declare functions inside functions like that. What you can't do is define functions inside other functions like this

int main()
{
	void USERF(double *,double *,double)
	{
	}
.
.
   return 0;
}
nbaztec commented: Really! I didn't know that. Thanks for the info. +1
Banfa 597 Posting Pro Featured Poster

itoa is not an ANSI/ISO standard function and is non-portable, also it converts an integer to a string not a string to an integer.

Try atoi or strtol

Banfa 597 Posting Pro Featured Poster

If you don't get it how did you write it in the first place?

Banfa 597 Posting Pro Featured Poster

Here is your function S_NEWT with the code removed and the relevant parameter highlighted

void S_NEWT(int *n,double *x0,double e,int NMAX,[b]double (*FUNC)()[/b])
{
   <snip>
}

The () at the end of the highlighted parameter represents the parameter list for the function pointer FUNC.

Here is the prototype of your function USERF

void USERF(double *,double *,double);

The parameter list is everything between ( and ). () and (double *,double *,double) are not equivalent, you need to alter the () in your function S_NEWT to match what is in USERF.

This needs to be done both where S_NEWT is declared and defined.

Banfa 597 Posting Pro Featured Poster

Firstly its a warning not an error, the program still compiled and possibly worked. However that said plenty of programmers use the rule of thumb that all warning should be dealt with and the program should compile without warnings.

You have declared S_NEWT as taking a pointer function with no parameters but supplied it with a pointer to a function that takes 3 parameters. Try fixing the prototype of S_NEWT to take account of the 3 parameters in USERF.

Banfa 597 Posting Pro Featured Poster

Was that all the errors? Having the complete text of the error messages would help (unless that's also in Japanese).

I couple of points

main returns int, returning default int without a warning tends to suggest you are using an old compiler.

Using capital letters for you function names is a little unusual, I assumed they were macros at first, however it is not actually wrong.

You do not seem to have declared S_NEWT before trying to call it. That may be having an effect as the compiler will not know what the function prototype is.

Banfa 597 Posting Pro Featured Poster

I am using fstream, so it should include cin and cout. What I learn is that fstream is inherited from iostream,ifstream,ofstream and some other header files so includes the functions of iostream as well.

I took note of your suggestions, thanks for it. I took note of it.

No fstream in <fstream> inherits iostream in <istream>. cin in <iostream> inherits istream in <istream> and cout in <iostream> inherits ostream in <ostream>.

There is no connection in inheritance or filewise between fstream and cin or cout. To use cin or cout you must specifically include iostream.

NOTE: the class iostream is not declared in the header <iostream>

See here

jonsca commented: Excellent clarification +4
nbaztec commented: Nice. +1
Banfa 597 Posting Pro Featured Poster

That is because fstream.h (and all other C++ headers ending .h) is a pre-standardisation header i.e. over a decade old and has been depricated for sometime and I expect MS has taken action and removed it from their IDE/compiler.

use #include <fstream>

Banfa 597 Posting Pro Featured Poster

you have not defined the return types of several of your member functions making the compiler think they are incorrectly named constructors.

Banfa 597 Posting Pro Featured Poster

Nope because map is declared as int map[3][9][10] , map[1][1][1] has type int so map[1][1] must surely be something different.

Do you not get an error similar to
error: invalid conversion from 'int*' to 'int'
when you compile it. Surely that gives you a clue to the type of map[1][1] ?

Banfa 597 Posting Pro Featured Poster

You only have 2 array dimensions, what is the type of location , what is the type of map[location][direction]

Banfa 597 Posting Pro Featured Poster

You have 4 {, every dimension of array requires a { so you have 1 too many.

You inner most array initialisers have 10 entries, but the last dimension of you array has a size of 3. The sizes do not match.

Looking how you have laid out the data I suspect you should have declared int map[3][9][10] or of course fix the initialiser.

Banfa 597 Posting Pro Featured Poster

Do you mean -10^5, 10^-5 is not a large negative integer it is 0.00001

The smallest value most implementations can hold in a double is around 2.225073859e-308. This limit is reached for e^x around about x = -700. You are unlikely to be able to calculate e^-10000 using normal data types directly.

Up to x = -700 the standard library function seems to work just fine (on my implementation).

Banfa 597 Posting Pro Featured Poster

Every compiler has 2 character sets, the source/compiler character set, that is the character set the compiler uses to read the source files and the execution character set, that is the character set that the produced program will use while it is running.

Apart from that the C standard doesn't have much to say, about the only guarantee it makes is that for the digits '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' the result of subtracting a digit character from another should be the result subtracting the number from the other i.e.

'2' - '1' = 2 - 1 = 1

It does not make this type of guarantee for any other characters, for instance letters.

Apart from that any implementation is free to use any character set it chooses.

Banfa 597 Posting Pro Featured Poster

Line 8, you get an integer, presumably you input either 1 or 2.

But in your case statements you using '1' and '2', these are the characters 1 and 2 and respective have the values 49 and 50 (assuming ASCII) so your 1 and 2 input don't match.

Using the integer values is the correct approach which you have tried. The segmentation fault I assume is caused by something later in your code in the functions get() or getnext().

In case 1 since count is always initialised to 0 it will always call get(), the whole of case 1 can be reduced to a call to get() and the variable count can be discarded.

Banfa 597 Posting Pro Featured Poster

Line 14 - 17 what????

Have you not heard of else if

if (i >= 1000)
{
    n = 4;
}
else if (i >= 100)
{
    n = 3;
}
...

Line 19 - 22 what ???
Have you not heard of the modulus operator %
Line 21 (which is wrong anyway) t=(i%100)/10; Line 25
^ is the xor operator. There is no power operator in C, the is a pow function in the math library. However it uses the double type so will introduce inherent tolerance errors preventing accurate use of the operator == so you may need to write your own version using int/long.

Banfa 597 Posting Pro Featured Poster

I think your problem is as the iteration continues fsum gets bigger while term gets smaller. At some point you reach a stage where term is small enough that it can not accurately be added to fsum because of accuracy issues. This test program highlights the problem

#include <iostream>
#include <iomanip>

using namespace std;


const double delta = 1e-16;
const int count = 1000000000;

int main()
{
    double result1, result2;
    
    result1 = result2 = 1.0;
    
    result1 += delta * count;
    
    for(int ix=0; ix<count; ix++)
    {
        result2 += delta;
    }
    
    cout << setprecision(15) << result1 << " - " << result2 << endl;
    
    return 0;
}

In theory adding delta count times should give the same result as adding delta * count but because delta is small in relation to the size of the sum an accuracy problem occurs and the actual output (on my machine) is

1.0000001 - 1

This is an extreme case of what I think you are experiencing.

There is a method to offset this problem, it is called the Kahan summation algorithm which you may find helps.

Banfa 597 Posting Pro Featured Poster

Also in the function RandomArrayFill the code puts 1 more value than there is space for in the array.

That is if numero is 5 and array of 5 ints would be allocated (after the fix above) and passed to RandomArrayFill but this function would then write 6 integers into the array writing outside the bounds of the array.

Banfa 597 Posting Pro Featured Poster

Take it out of students::AddStudent and add it as a static data member of class students.

BTW line 16 is a waste of time, you may as well just change line 4 to class STUDENTS {

Banfa 597 Posting Pro Featured Poster

Line 27 (and 66) void GetMovieInfo(MovieData &m1, &m2) Every function parameter needs to have its type fully defined. void GetMovieInfo(MovieData &m1, MovieData &m2) it is not like declaring a list of variables MovieData m1, m2; Also I would have thought you teacher will want to see functions taking a single parameter being called twice. What will you do if the next part of the assignment is "expand this program to handle an array of 10 movies"?

Banfa 597 Posting Pro Featured Poster

You have a structure, prt_interpln which contains 2 arrays of pointers size 2 and you declare a pointer to that structure interp_coeff.

So you have 5 pointers, 1 to to the structure itself and 4 inside the structure to arrays of double.

All of these pointers need memory allocated and assigned to them before they are used so your code should be performing 5 allocations.

For every allocation it should be calling free, otherwise you have a memory leak. If in the course of the program you re-allocate the memory for one of the pointers you need to be sure that you have freed the memory it pointed to previously.

You must deallocate the memory that the pointers contained in prt_interpln point to before you deallocate the memory that the pointer interp_coeff points to.

Banfa 597 Posting Pro Featured Poster

Functions are in the global scope, you can return a pointer to any function (even a static one) and the pointer will be valid.

The address might well be the address of the first instruction in the function however the true answer to your question is that it is platform defined, i.e. there is no one answer that is necessarily correct for all platforms.

Banfa 597 Posting Pro Featured Poster

yes there is a rule. types are promoted to a higher type if assigned to a higher type. going from a higher type to a lower type works as well but there can be truncation of the number

That's not the complete picture, types are promoted to a higher type if used in a calculation with a higher type or when assigned to a higher type, whichever happens last so

int foo = 5;
float bar = 3.5;
float result = foo + bar;
// result is now 8.5 because foo gets converted to a float and then added.

// but
int foo = 5;
int bar = 2;
float result = foo / bar;
// result is 2.0 not 2.5 because the calculation uses integer arithmetic

In the second example the values are not promoted to float until after the calculation has happened and the result is stored in result. That means that the calculation takes place using ints and integer arithmetic and all fractions are dropped.

Banfa 597 Posting Pro Featured Poster

I hope not because 1 is not a prime number* and 2 is.

You have already got code that checks a number to see if it is prime in your own post (although it code stand some optimisation). All you have to do is repeat that in a loop for every number you wish to check, better still put it in a function and then call that function from the loop.

* What is 1 not considered prime? - in short it would break too many other Maths theorems if it was.

Banfa 597 Posting Pro Featured Poster

On line 70 you test table[index].next , table is passed from main where it is declared as an automatic variable. it is never initialised therefore the first time this test is performed it is unlikely that table[index].next is NULL so no memory is allocated for it and instead the program proceeds to try to dereference this invalid pointer through pos

Banfa 597 Posting Pro Featured Poster

You have put single quotes round a series of 3 characters, 'yes'. However single quotes are used to delimit a single character 'a' '5' 'V' '*' '\t' (<--- it's a single tab character). If you want a string you need to use double quotes like this "yes", note that a string has a zero terminator so the length of the string "yes" is 4 bytes, one each for y, e and s and 1 for the zero terminator '\0'.

From line 2 of your code I am guessing that answer is in fact a char char answer; however if answer is an array char answer[100]; then you have your scanf on line 2 wrong and you can not compare strings (arrays of char) using ==.

Banfa 597 Posting Pro Featured Poster

But my question is for SQ(a+2) >> a+(2*a)+2 ...that means when a=5 the answer is 17.

You could easily follow the same process griswolf already did he left out the - but put it in and you get

-SQ(a+2) == -a+2*a+2 == -a+(2*a)+2 == a+2

Therefore for a=5, -SQ(a+2) == 5

By the way the answer to your second post is its undefined behaviour.