Tumlee 42 Junior Poster in Training

First code: You're calling mainPage() as if it belongs to the global scope. The way you've written your code, though, it actually belongs to your class foo. My best bet is, because of the context, it assumes you're declaring an implicitly-int function called mainPage() inside of both minus() and add(), and therefore complaining about multiple definitions.

You probably meant to have those functions be members for foo anyway, so try rewriting lines 5 and 11 as void foo::add() { and void foo::minus() { respectively.

Second code: When you have an inline function, it can't actually be linked like a normal function. As a result, any code that uses foo::mainPage() must be able to see its full source or you will get undefined refernece errors. In order to mitigate the errors, you would actually have to move your inline function to "foo.h".

On a side note, this code shows very bad flow. You're calling mainPage() from add() and you're calling add() from mainpage. You're basically causing your function to recurse, possibly indefinetely, based on user input. It may be a much better idea to use a real loop inside of mainPage() and just return normally from the other functions.

Tumlee 42 Junior Poster in Training

You would make the fuction virtual, which allows you to override the behavior of a function when defining the child class. It should be as simple as changing void set_dimensions() as virtual void set_dimensions() in your shape2D class, and then rewriting shape3D::set_dimensions() to accept all three dimensions.

There may also be a way to call overridden virtual functions in a child class, but it's been so long and I have so little use for it that I don't remember how to use that feature anymore.

Tumlee 42 Junior Poster in Training
main.c:7:6: error: 'i' undeclared (first use in this function)
main.c:7:13: error: 'y' undeclared (first use in this function)
main.c:9:3: error: 'x' undeclared (first use in this function)
main.c:11:2: error: 't' undeclared (first use in this function)
Tumlee 42 Junior Poster in Training

You're spoonfeeding the answer to what is clearly a homework assignment to somebody who doesn't want to do any of the programming themselves. This is the reason so many people can get CS degrees without even knowing how to do FizzBuzz. I hope his professor is at least smart enough to mark him down for void main() when he turns this one in.

Tumlee 42 Junior Poster in Training

NathanOliver: There is actually a getline() for std::string as well. istream& getline (istream& is, string& str);

Tumlee 42 Junior Poster in Training

jwenting: In India, schools have unfortunately standardized around Turbo C/C++.

Tumlee 42 Junior Poster in Training

As a general rule of thumb, if your program can be "optimized" by changing one little operator like that, then the compiler will likely do it for you.

That being said, the short-circuiting behavior mentioned by deceptikon is important to note. Using the && operator ensures that unnecessary checks are skipped if the left operand is false (because this automatically makes the entire expression false). This could potentially mean that bitwise & would be slower in some cases.

Tumlee 42 Junior Poster in Training

PS: I see that a lot of people are already familiar with C++11 to the point that they don't think about it when they use its features. Is it now considered 'best practice' to use the new features? I am mainly concerned due to the lack of universal support for it and the fact that some C++ programmers have yet to learn the changes so I fear that C++11 code may not be understood.

I would consider it 'best practice' in most circumstances. C++11 is practically two years old now and a lot of its feature set was known before it even officially became a standard. If people can't compile C++11 code now, it's because they're using some really old crap like Turbo C++ or dev-c++ or whatever. I can't think of a modern compiler that doesn't support a majority of its feature set.

Tumlee 42 Junior Poster in Training

At lines 33 and 38 your formal parameters are floats but you call the function using doubles - you can fix this by changing floats to doubles ( ex: 5.5 to 5.5f ) or simply change the parameter's type from the methods's header.

Doubles are automatically converted to floats when needed, and vice-versa. This is not necessary.

Tumlee 42 Junior Poster in Training

The while statement is a loop that contininues to execute until some condition is met.

I think you meant to say a while statement is a loop that continues to execute while some condition is met.

Tumlee 42 Junior Poster in Training

For some reason, I am able to run the first program without any issues whatsoever, which absolutely baffles me because looking at it, it probably should crash. You create a pointer called pr1 which is supposed to point to a linkedlistchain, but you never initialize it nor set it to point to anything. Your insert() function calls the isEmpty() function, which in turn reads memory that doesn't belong to your program, crashing it. That's my best guess.

The reason your second program runs without any issues if obvious. You can read the value of ptr no matter what it is, whether it's NULL or pointing to valid memory. If you try to write to that address or dereference it, that's when you start to get problems if it doesn't point somewhere valid. Adding the following line to your second program will probably cause it to crash:

cout << "The value of *ptr is" << *ptr << endl;
Tumlee 42 Junior Poster in Training

You're accessing your arrays incorrectly. When you have an array declared as int num[2], num[0] is your first element and num[1] is your second element. You're passing values of &num[1] and &num[2] to the scanf() function.

As a result, when you think you're entering your first number, you're actually entering the second number. When you think you're entering your second number, you're writing to something beyond the edges of the array and are probably overwritting opcode as a result. If this gets overwritten to something outside of the range of 1 through 4, no answer will be displayed on your screen.

Tumlee 42 Junior Poster in Training

When used correctly, polymorphism is easily the most useful feature in all of C++. It's more particularly useful when you start getting into game design, where you often have large linked lists of actors, all belonging to different subclasses but all needing to behave differently from one another. In such a case, you'd often give them different "thinker functions" so they can reliably execute even though the programmer doesn't have to keep track of which actor belongs to which class.

#include <iostream>

using std::cout;
using std::endl;

//The parent class upon which two polymorphic classes
//will be defined, dog and cat.
class animal
{
    public:
        bool tailwagging;

    //This is a polymorphic function. What this line does is
    //tell the compiler that all subclasses that inherit from
    //"animal" will have an is_angry() function that returns a bool
    //and takes no arguments.

    //This " = 0" at the end tells us that the "animal" class itself
    //has no is_angry() function, so "animal" is an abstract class.
        virtual bool is_angry(void) = 0;
};

//A cat class that is an animal. It has its own definition of whether it's
//angry or not, so it should have its own implementation of the "is_angry()" function.
class cat : public animal
{
    public:
        virtual bool is_angry(void)
        {
            //If a cat's tail is wagging, it's angry.
            return tailwagging;
        }
};

//A dog class that is an animal. We use different logic here for determining whether
//or not it is angry. It's still an animal, though, so it will …
Tumlee 42 Junior Poster in Training

argv is not a char, it's a pointer to a pointer to a char, so the comparison argv != '\0' makes no sense. You really shouldn't be modifying argv directly by incrementing it anyway.

Tumlee 42 Junior Poster in Training

Your variables will only be reset to zero if you set them to zero yourself, or if you call a function that sets those variables to zero. I don't know what your "LevelUp" functions do, because you never provided them in this thread, but they shouldn't really affect the variables being passed to them unless you're passing-by-reference instead of by value.

Tumlee 42 Junior Poster in Training

You're declaring all these variable as extern, but they don't seem to actually exist anywhere. When you say extern std::string nomEnnemi;, for example, you're not actually declaring a variable, but simply promising the compiler that it exists in another file.

You actually do declare these variables, but you only do it in a scope that's local to the InitVariable::InitialiserVariable() function. Those variables don't actually exist outside of this function. Moving these declarations outside of the function will certainly solve some of your problems.

Once you get past this problem, there will be a few other errors, particularly where you are trying to access member data that belongs to the Personnage class in a function that belongs to the SystemeBattaille class, but I don't actually know what your code is supposed to do, so I'll leave you to figure those problems out.

Tumlee 42 Junior Poster in Training

You forgot to post Personnage.h so we might not be able to help you troubleshoot your problem. Can you at least tell us which variables it's complaining about being undefined?

Tumlee 42 Junior Poster in Training

On line 43, while(stillguessing == true); you have actually results in an infinite loop if stillguessing is true, because of the semicolon. Remove the semicolon to fix it.

Tumlee 42 Junior Poster in Training

thats why i am using memset which fails.

This isn't what memset() is supposed to be used for. It does byte-by-byte setting to a specific value. You're going to have to use the loop.

Tumlee 42 Junior Poster in Training

There is no difference, functionality-wise between char** argv and char* argv[]. It's all a matter of preference.

Tumlee 42 Junior Poster in Training

We would have to get a copy of what file you're working with to even begin to understand what your problem is.

Tumlee 42 Junior Poster in Training

What you're probably looking for this the pre/post build steps tab under your project's Build Options... menu, which will allow you to set certain scripts to be executed whenever you build your project. Go to Project > Build Options... and then go to the tab that says Pre/Post build steps.

Tumlee 42 Junior Poster in Training

When you're using macros like this, please pay attention to exactly what the code looks like when your macro is expanded. The code you have here is equivalent to writing the following:

#include <stdio.h>
#include <conio.h>
void main()
{
int i=5;
int j=for(typeof(i) i=2; i<=n; i++){f *= i};
printf("%d",j);
getch();
}

Obviously this code makes no sense. You can't set a variable equal to a for loop, because it's not a function and therefore doesn't return a value. Furthermore, the variable f doesn't even exist in this context. This sort of thing is simply too complex to do with a macro. What you'll want to do instead is make a function to perform the same task.

#include <stdio.h>

int fac(int parm)
{
    int total = 1;
    int mult;

    for(mult = 2; mult <= parm; mult++)
    {
        total *= mult;
    }

    return total;
}

int main(void)
{
    int i = 5;
    int j = fac(i);
    printf("%d", j);
    return 0;
}
Tumlee 42 Junior Poster in Training

The reason it's skipping is becuase char is actually a signed type, which means that it includes positive and negative numbers. This means that, when trying to interperet a char as an 8-bit integer, its effective value range is actually -128 to 127 instead of 0 to 255. When a character with an ASCII value higher than 127 goes into this if statement, your code is actually reading it as a negative value (147, for example, gets read as -108), which is always going to be less than 122.

To circumvent this behavior, all you need to do is typecast the character into an unsigned char, like so:

if((unsigned char)userInput[value] > 122)
{
    //Code...
}
Tumlee 42 Junior Poster in Training

Here's how I comment your code. Please note that, according to your code, 2 is not a prime number. You might want to fix that.

import java.util.Scanner; //This line helps the compiler find the class file Scanner. 
public class PrimeNumber{ //Defines a class called PrimeNumber
public static void main(String args[]){ //Declaration of the main method of the program.
int n; //This line declares an integer variable called n.
Scanner Prime = new Scanner(System.in); //Declares a scanner, a class that takes input from the user.
System.out.println(" Please enter a number: "); //Prints the phrase " Please enter a number: " to the screen on its own line.
n = Prime.nextInt(); //Grabs the very next integer the user types, and stores it in the variable n.
if (isPrime(n)) //An if-statement that calls isPrime() to check if n is prime.
System.out.println("The number is prime.\n"); //Prints the phrase "This number is prime." to the screen on its own line, and then skips a line.
else    //Tells the program to execute the next line of code, in case n isn't prime.
System.out.println("The number is not prime.\n"); //Prints the phrase "This number is not prime." to the screen on its own line, and then skips a line.
}
static boolean isPrime(int n) { //Declares the isPrime() function, which returns true if the parameter 'n' is prime, and false otherwise.
if (n%2==0)return false; //Use the modulus operator to determine if n is divisible by two, in which case it is even and therefore not prime. This …
jalpesh_007 commented: you're right.. +3
Tumlee 42 Junior Poster in Training

Until we see what the CALL_DATA struct or class looks like, we can't really know for sure.

Tumlee 42 Junior Poster in Training

You're already halfway there when it comes to understanding this line, then. For this code to make sense, src has to be a pointer to a pointer type, such as char**, and offset has to be a pointer type, such as char*.

What this particular line of code is doing is saying "Make the memory at the address pointed to by offset equal to the value at the address pointed to by *src (src itself being an address that points to that address), and then increment *src so it points somewhere else." It sounds like a mouthful, but that's how it works.

Tumlee 42 Junior Poster in Training

Your problem is in line 11.

void insert(int i, string elem, vector<string> st)

What you're doing here is a pass-by-value operation. This means that you're not actually passing your vector to the function, but rather just a copy of that vector. Your function modifies the copy, but the original vector remains untouched.

The fix is very simple --- turn it into a pass-by-reference operation by putting an ampersand after vector<string>.

void insert(int i, string elem, vector<string>& st)
Tumlee 42 Junior Poster in Training

Let's go through this problem by problem.

1) You got completely confused while naming your member variables. As pyTony said above, in student.h:

    int newid;
    string newname;

Should be changed to:

    int ID;
    string Name;

To finish fixing these naming conflicts, change Student::Student(int ID, string Name) to Student::Student(int newid, string newname) in student.cpp.

2) In main(), you are calling student.setStudentID("1234");. When you put quotes around something, you're turning it into a char*. This function is supposed to take an int. Remove the quotation marks on this line and it should work.

3) In Student::display(), some of your chevrons are backwards when using cout. Flip those around.

Tumlee 42 Junior Poster in Training

Furthermore, it should be int main() not, void main(), and you should have return 0; at the end of your program.

Tumlee 42 Junior Poster in Training

One issue is here: &(*mPntr)
The other is here: &PntrFunc(&(*mPntr))

You can't get the location of *mPntr, because that's an expression, so your code doesn't make any sense. It would be like if you were to try to do this: int* ptr = &(a + b); It's obvious to see why --- the expression (a+b) doesn't have a permanent location, so using the ampersand on it serves no purpose. You can only get the location of an actual variable, such as theVar2.

Likewise, you can't get the location of a function call, that also doesn't make any sense. A function call also has no permanent location.

Tumlee 42 Junior Poster in Training

In your declaration of mergesort() , you are giving it two arguments.

int mergesort(int,int);

But when you actually define the function, you gave it three.

int mergesort(int first,int last,int mid)

That is a mismatch, and it results in the linker error you ended up with. You fix this by changing either your declaration or the actual function so they both match.

Tumlee 42 Junior Poster in Training

If the algorithm was "simple" it probably wouldn't compress very well. If it helps you understand better, many compression algorithms actually can end up making a chunk of data larger if it's not suited for the task (notice how sound files don't compress very well at all with ZIP compression).

Tumlee 42 Junior Poster in Training

I would recommend enum in this case because that's exactly what they're there for.

Tumlee 42 Junior Poster in Training

"Just make an empty method" I would need a little help. How to do it ?
(I know it sounds stupid but I haven't touched c++ in 10 years...)

You would just make a function put nothing in the brackets except for a return; statement in this case. If the function was non-void you'd probably want it to return an actual value. The following should probably work:

void CheckFileNames::RecordAsset(const MaxSDK::AssetManagement::AssetUser& asset)
{
    return;
}
Tumlee 42 Junior Poster in Training

C++ and C are just as capable as any other language when it comes to using the internet, networking, etc. There are all sorts of libraries out there to do that sort of thing. Even C can have, for example, a game that uses UDP that can be used for online play.

However! C++ and C are not designed for making webpages, or browser-based games.

Tumlee 42 Junior Poster in Training

new uses brackets, not parenthesis. Furthermore, it's called allocating memory, not occupying it.

char* temp = new char[255];
temp = "poiuytrewq";
Tumlee 42 Junior Poster in Training

There's a semicolon after your while statement on line 43. You need to get rid of that thing or your program will never work correctly. When you have a while loop with a semicolon after it your loop will do nothing except increment counters.

At line 59, the statement else convertchoice == 1; does nothing. What you probably meant to do was else if(convertchoice == 1) . Semicolons should also not go after if and else statements or they will also do nothing.

I can't really test the code myself ATM but that should at least make it work better than it did.

Tumlee 42 Junior Poster in Training
Scanf("((%d/%d)*%d)", &Mark1, 40, 100;

You're also missing a parenthesis at the end here. It should be:

scanf("((%d/%d)*%d)", &Mark1, 40, 100);
Tumlee 42 Junior Poster in Training

Since he's just dumping the question to a file immediately after input, he's really not "overwriting" anything but a temporary variable. So, no, he doesn't need to use an array.

Tumlee 42 Junior Poster in Training

(Good God, why does everybody here beat around the bush when somebody has a problem, just help the guy!)

What I would do in your situation is to monitor all the variables involved (you don't even need a debugger for this, just make it print your variables in a loop). If your loop is going on forever, and the condition is i<n , then check the values of i and n to make sure they're not changing unexpectedly. At the end of your loop, do this:, and it may help you find the problem yourself.

cout << "(DEBUG) i = " << i << endl;
cout << "(DEBUG) n = " << n << endl;

There are some other things that you're doing that will cause you headaches in the future. As said above, always use int main() . Second, your formatting is horrible. Braces (the { and } characters) should, as a general rule, be given their own line. Your opening brace itself should not be indented further than the code before it, but code inside braces should be more indented than the surrounding code. For example, this main functions looks much cleaner:

int main()
{
    int n;
    qa  q;
    fstream file1;

    file1.open("questions.dat", ios::out);
    cout << "\n Enter the limit of questions " << endl;
    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cout << "Enter the question" << endl;
        q.getques();

        cout << "enter the four options " << endl;
        q.getopts();

        cout<<"Enter …
Tumlee 42 Junior Poster in Training

I know that running a program through an environment such as Visual C++ or CodeBlocks will automatically set the working directory to whatever you choose, if you set it up correctly. Your IDE is probably set up to always run your Debug program straight from the /bin/ folder, but when you run it directly as an EXE, it's running it from the folder it's in.

I've had a similar problem in the past which caused me many headaches with my programs. It turns out that clicking and dragging a file over an EXE in Windows will change the working directory, and this prevented my program from finding vital files in the same directory. I eventually had to write a function that would force it to load files from the directory it's in.

Tumlee 42 Junior Poster in Training

What is the problem you're having?

Are you getting freezes? NULL-pointer access? Just some general crash? Is it not compiling?

Tumlee 42 Junior Poster in Training

Are you sure it's your *compiler* that's crashing? Or is your program crashing during execution? If it's the latter, it's more than likely that "r" is pointing to invalid memory or "r" is a NULL pointer.

Tumlee 42 Junior Poster in Training

As long as that part of the input will always be a single character, yes, you will be able to do that.

Also, if you want to make your code shorter, I believe fscanf returns the number of arguments filled, so you could fill multiple data element with a single call to fscanf().

The third argument you're using looks like it has a decimal point, also, so you should probably use float to avoid headaches.

if (fscanf(f, "%d %d %f %c", &long1deg, &long1min, &long1sec, &orien1lon) == 4)
{
  //Success
}
else
{
  //ERROR!
}