Tumlee 42 Junior Poster in Training
struct{
const int EAST = 0;
const int SOUTH = 1;
const int WEST = 2;
const int NORTH = 3;
};

Who in God's name would use a struct like that? That is not what structs are there for.

The enum keyword is for where you want to have different, related constants enumerated without having to use #define on everything. Logically, it should only be used when the number behind that constant has no real meaning. (For example, nobody should care that direction_north is 2, or 3, or -45, because the code should only really refer to it as direction_north ). If you have to refer directly to a value like that, you're probably using enum incorrectly.

Here is an example of how I used an enum in a game I am currently programming.

//enum for statuses returned by loadgame()
enum loadstatus_t
{
    load_success,     //Indicates that the savegame was successfully loaded.
    load_nofile,      //Indicates the save doesn't exist.
    load_badversion,  //Indicates the save is from too new or old of a version.
    load_smallscreen  //Indicates the user's screen dimensions can't fit the save file on screen
};

A struct should be created when you have several variables that 'go together'. For example, you can have this mess:

float redapple_weight;
float redapple_diameter;
int redapple_price;
bool redapple_isrotting;

float greenapple_weight;
float greenapple_diameter;
int greenapple_price;
bool greenapple_isrotting;

float crabapple_weight;
float crabapple_diameter;
int crabapple_price;
bool crabapple_isrotting;

See how much of a mess that is? You basically have to copy-paste a lot of …

Tumlee 42 Junior Poster in Training

What happens if you hold the up button? It looks to me like Lazyfoo forgot to add a check to make sure the dot wasn't on the ground. It will therefore occur that when you press the up button in a higher framerate, it will be down for more "ticks" and will jump higher.

There are no problems with SDL's timers.

Tumlee 42 Junior Poster in Training

Here's the way I would have done it.

Err... oops. I forgot:

#include <iostream>
using namespace std;
#define SQUARESIZE (input*2-1)
Tumlee 42 Junior Poster in Training

I really do like programming, it's just that the problem solving part is difficult for me.

I have devastating news for you.

I forgot to mention that I'm relatively new to C++, and I have no idea what most of that means :p

Don't fret. I've been using C++ for five years and I still don't know what most of that code means.

It was on a quiz that I had already done, and I couldn't figure out the answer.

There's no "the answer" when coding. There's always multiple different ways to do the same thing, depending on how you attack the problem. You'll see my version is vastly different than thines01, and probably different than how WaltP would do it.

Here's the way I would have done it. No templates, two loops, can be easily converted to use vanilla C. Portable. Delicious.

int main()
{
    int input;
    cout << "Input a number." << endl;
    cin >> input;

    if(input < 1 || input > 9)   //Bounds checking...
    {
        cout << "Sorry, must be between 1 and 9." << endl;
        return 0;
    }

    //Print square; the closer to the edge, the smaller the number.
    for(int line = 0; line < SQUARESIZE; line++)
    {
        for(int ch = 0; ch < SQUARESIZE; ch++)
            cout << min(min(line+1, ch+1), min(SQUARESIZE-line,SQUARESIZE-ch));

	cout << endl;
    }

    return 0;
}

"Problem solving" isn't something you can really learn on Daniweb or in a C++ class. That's just something that takes practice.

Tumlee 42 Junior Poster in Training

Well, if you're going to say that, you're going to also need a keyboard, a screen, and a pair of hands. ;)

I stand by what I said; as far as coding goes, you really don't need to switch to a different programming language.

Tumlee 42 Junior Poster in Training

Why use while(0==0) when you could just use while(true) ? Furthermore, why would you use a do-while loop when your condition is infinite?

Tumlee 42 Junior Poster in Training

Are you talking about before your class is finished, or in the long term?

If it's the latter, I recommend starting simple, like with a 2D game with simple sound effects. I highly recommend learning how to use the SDL library for this, as it has some pretty simple methods for drawing pictures to the screen, playing sounds, and getting input from the keyboard and mouse. There are tutorials all over the net which you can learn from.

Also, note that you don't really need classes to make a game, they just really make it much simpler and human-readable to write game logic. In the days when plain vanilla C was popular, you could get away with using structs and using function pointers to have different objects behave differently (although it could get pretty messy).

Since you've already learned how to use structs, you're already halfway home when it comes to learning how to use classes. The really big features that will be helpful in game design are virtual functions, inheritance, and polymorphism.

You can start with what I did when I first started with game programming, just remake Pong. It's extremely simple and will get you used to making two objects interact with eachother, as well as using classes correctly.

Here is a video of someone talking about a c++ project with Allegro.

I wouldn't recommend Allegro. It lacks quite a few features that would be useful in game design, and from my experience …

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

In the comments you'll see some potential problems with this code.

#include<stdio.h>
#include<string.h>
int main()
{
    //You're intializing a two dimensional array with a one-dimensional initializer.
    //This may cause problems.
    char a[4][4]={'j','u','s','t'};
    int i,l,j,k,b=4,n; //If b is the number of characters, why not name it numchars or something like that?

    //Asking the user to enter a string and not giving him a chance to input.
    printf("Enter the string");
    printf("\n");

    //What's the point of this loop? It only executes once while i is zero.
    for(i=0;i<1;i++)
    {
        for(j=0;j<b;j++)
        {
            printf("%c",a[i][j]);
        }
    }

    printf("\n");
    n = b;
    
    //Huh? i==0? This is probably your problem. i is never changing in this loop at all.
    //b also never changes, so your loop will go on forever.
    for(i=0;i<b-1;i==0)
    {
        //Even if i was properly incrementing, you never set all 16 the values in the
        //array. You'll be reading from uninitialized garbage when i is greater than zero.
        for(k=n-1;k<b;k++)
        {
            printf("%c",a[i][k]);
        }
        for(j=0;j<n-1;j++)
        {
            printf("%c",a[i][j]);
        }
        printf("\n");
    //Because your loop is infinite, n becomes a negative number, forcing this value
    //into k will force your program to read a negative index in the array, crashing
    //your program.
        n--;
    }
    //ALL programs should have a return statement at the end of main().
    //In most cases, you want to return 0;
}
Tumlee 42 Junior Poster in Training

There is nothing complicated about modern IDE's. If you install Code::Blocks correctly, all you have to do is create a project, punch in some code, and hit "Build and Run". I can't imagine anything more simple.

Tumlee 42 Junior Poster in Training

string compliments[4] = {"String 1", "String 2", "String 3", "String 4"}; This will create an array of strings that you can output. If you're going to use the string class, you're going to need to include the proper header, which I believe is either called <string> or <strings>

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

Exactly who keeps telling people to keep using Turbo C? Is there some antiquated book from the 1980's floating around and encouraging this? I'm assuming it's the same people telling everybody to use void main() .

Tumlee 42 Junior Poster in Training

You're supposed to mark the thread as solved if the problem is solved. (And up my rep if you're feeling kind :] )

Tumlee 42 Junior Poster in Training

The code I just showed you would compare them. You can't guarantee that there won't be any ties, even with an odd number, though, because of the fact that there can be a tie of the computer and user throw the same hand.

You would need two more variables, ComputerPoints and UserPoints , and increment them whenever one wins more hands.

Tumlee 42 Junior Poster in Training

Actually, my bad, it should be going inside the do-while loop but outside of the for loop.


And here's how to compare the two values.

if(totalComputerWins > totalHumanWins)
{
    cout << "The computer has more wins." << endl;
}
else if(totalComputerWins < totalHumanWins)
{
    cout << "The human has more wins." << endl;
}
else
{
    cout << "Both have the same amount of wins." << endl;
}

Also, are you using totalHumanWins for each round that is won or each set of rounds? It looks like you're trying to use the same variable for two different things.

Tumlee 42 Junior Poster in Training

I discovered another problem in that code: totalComputersWins and totalHumanWins are set to zero every time the loop loops. You need to put that at the very beginning of your program or nobody will ever win.

Also, why not just directly compare the two values directly to see who wins?

Tumlee 42 Junior Poster in Training

First of all, you're not using your characters and integers consistently at all. It's going to cause you a lot of confusion in the future. The character '1' is not equal to the number 1 In readAndValidateUserMove() , there is really no need to use tolower() on the input because there is no such thing as a capital number. The next thing you should do is make it so that function converts the character the human typed into an actual integer. To do this, rewrite your return value as return result - '0'; After that, get rid of all those silly quotes around all of your numbers, and get rid of that 48 in generateCompMove() . The way you're handling those numbers is almost guaranteed to cause problems down the road.

Now, onto what you're asking: You want to log the number of times each player has won, correct? You already have all the information you need, it's being logged in totalHumanWins and whatnot. All you need to do is print that information at the end of the program.

EDIT: Yes, that new code you just wrote would be correct, but only if you also correct that other code.

Tumlee 42 Junior Poster in Training

result = SaveAudioFile(pFileName); Are you sure a JPEG is supposed to be an Audio file? Furthermore, what libraries are you getting all of these functions from?

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

That raises an interesting question: Once (if?) tablets become more popular for Windows users, how will that affect already existing software? Can we expect to see popular software redesigned with bigger buttons, or retooled to take advantage of multi-touch touchscreens? What about free libraries, like SDL (which has been in developmental standstill for about three years now)? Will somebody come and retool those so they can use multitouch?

Tumlee 42 Junior Poster in Training

When you're talking about runtime, a computer honestly can't tell the difference and most of these differences between arrays and pointers are resolved during compile. sizeof() just happens to be one of those static expressions that gets resolved in such a way. The wording of my answer had to do with "abstract representation" in the OP's title.

Tumlee 42 Junior Poster in Training

When you use #include <blah.h>, that doesn't mean you're actually using the libraries, those are just guides so the compiler knows what functions and variables exist outside of your own program. You need to actually link them, or you're going to get all kinds of errors like the ones you see during the linking process. Usually you'll be able to set up the linking libraries in your Project Options depending on which IDE you're using. Libraries usually end in the extension .a or .lib (sometimes you're even able to get away with linking to a .dll)

Tumlee 42 Junior Poster in Training

An array is just a pointer. The only difference is that arrays always point to the same memory and will have a static number of elements.

int main(int argc, char** argv)
{
    int* ptr;      //A pointer to an int.
    int array[4] = {2, 4, 6, 8};  //A static array.
    int fifi = 20; //A regular integer.

    ptr = &fifi; //Makes ptr point at fifi.
    printf("%i\n", *ptr);   //This will printf the value of fifi, perfectly legal.
    printf("%i\n", ptr[0]); //This is also legal, does the same thing, but counterintuitive.
    ptr = &array[1]; //You can make them point to the middle of an array.
                     //This effectively makes ptr be a 3-element array with the values
                     //4, 6, and 8.
    printf("%i\n", ptr[2]); //This should print the 8 located at array[3].
    array = &fifi;    //THIS, HOWEVER, IS ILLEGAL C. ARRAYS CANNOT BE MOVED.
    return 0;
}

Also, notice how some people use "char* argv[]" and others use "char** argv"? They're both the same thing for that very reason.

Tumlee 42 Junior Poster in Training

i is used probably because it stands for iteration. People choose to use it because i is short and it looks clean when you use it as an array index (compare array to array[numloops]).

Tumlee 42 Junior Poster in Training

This is precisely why many C++ guides advise against using the equality operator with floating point values, because it can give you headaches like this. firstPerson's solution is okay for most applications. float == 0.0 is one of the few values for which it will work 100% of the time.

Tumlee 42 Junior Poster in Training

I actually found it Visual Sudio's "Intellisense" irritating when it was pestering me, putting red underlines on all my code and giving me suggestions when I wasn't done writing my program yet.

I prefer CodeBlocks because it's still an IDE that allows you to easily manage a large project, but still leaves you alone when you're trying to code something.

Tumlee 42 Junior Poster in Training

Yeah, int64_t has the same problem, but you will be less likely to run into that limitation if you use a larger int size.

Tumlee 42 Junior Poster in Training

...by this I mean you should never use a break statement in anything other than a switch

I agree with most of the stuff you said, but certainly not this. You should avoid using a break statement in for loops when you can, but there are an awful lot of cases in the real world where it's more clean or even more efficient to use break statements in for loops.

Ancient Dragon commented: agree +17
Tumlee 42 Junior Poster in Training

If I knew myself, I would have told you how to do it. ;)
http://www.cprogramming.com/tutorial/initialization-lists-c++.html
It's just one of those fancy C++ ways of making the code look prettier when initializing structs and classes. I've never found a use for it myself, so that's best left to somebody else.

Tumlee 42 Junior Poster in Training

Yeah, main() should never be called. I'm actually surprised that the compiler didn't yell at you for something like that, in many environments it's not even legal C. You should also not have to prototype main().

Once that code gets fixed, however, you might have problems because I'm not entirely sure if cin strips trailing newline characters.

About the exit() debate, I've had to use exit() a few times, but you'll almost never use it in a simple program. When you start making large programs, however, you'll find usefulness in atexit(), which registers a function to be called during an exit() call. It's also nice to have a catch-all fatal-error handling function that prints a useful message and then quits with status EXIT_FAILURE.

Tumlee 42 Junior Poster in Training

That is very similar to how I have solved my problem, the only difference is that I used C-style strings and I got the directory of the executable from argv[0], and I avoided using Windows-specific functions because I had to cross-compile it eventually. What I ended up with was this, if you're curious:

char* get_exe_dir(void)
{
   static char* exedir = NULL;
   if(!exedir)   //Generate the exedir by looking at argv[0]
   {
   //Search for the last directory seperator.
      int slashpos = -1;
      for(int i = 0; global_argv[0][i] != '\0'; i++)
      {
         if(global_argv[0][i] == '/' || global_argv[0][i] == '\\')
            slashpos = i;
      }
   //This should work even if slashpos == -1
      exedir = new char[slashpos+2];
      memcpy(exedir, global_argv[0], slashpos+1);
      exedir[slashpos+1] = '\0';

   //For consistency, change all backslashes to forward-slashes.
   //All operating systems, including Windows, support forward-slash as a dir separator.
      for(int i = 0; exedir[i] != '\0'; i++)
         if(exedir[i] == '\\')   exedir[i] = '/';
   }
   return exedir;
}
Tumlee 42 Junior Poster in Training

It looks like Python is more similar to C++ than Perl. Perl has some strange syntax that allows if statements after the command, and it appears that a lot of variables in perl don't have any real typing, and it varies with how you use them.

Python is syntatically more similar to C++, but it depends on whitespace and tab spacing to know where everything goes, unlike C++ where most whitespace is only needed to make the code more readable. Remember, though, that Python is ultimately a scripting language, and it never really compiles into a binary.

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

You're trying to set GLPoint P, Q, R, and S to just a set of numbers in parenthesis, and your compiler doesn't know what to do with that. You're going to have to make an intializer list of setting these, or if you want a quick and dirty method of fixing it, try this:

GLPoint P = (GLPoint){x1, y1};

And so on. Also, you need to declare the variable i at the beginning of your main function, otherwise it doesn't exist.

Tumlee 42 Junior Poster in Training

You mean like converting a character into a usable input? Like pressing 'Q' to get "Quit game" and whatever? My knowledge of <stdio.h> is better than my knowledge of <iostream>, so I'm sorry if it seems too oldschool.

char input = getchar();
if(tolower(input) == 'q')
{
    //Code to quit game
}
if(tolower(input) == 'r')
{
    //Code to print the rules
}

And et. cetera.

Tumlee 42 Junior Poster in Training

Impressive, but:

1) Bases zero and one cannot possibly exist, nor could it produce any sort of sensical output nor input.
2) Using "int" will more than likely limit you. That's only four bytes in most environments, or eight hex characters. You should probably use the int64_t type.

Tumlee 42 Junior Poster in Training

Seems more like a user issue. I've had Vista for three straight years and the boot time has stayed a static 15 seconds for me. The Allow & Deny thing only happens the very first time you run a program, so that's not an issue. Windows 7 is officially more memory effecient than Vista, so you got your wish.

I would like a more verbose startup in future versions of Windows, though.

Tumlee 42 Junior Poster in Training

You're going to have to swap each string manually, character by character, if you're going to want this to work.

Tumlee 42 Junior Poster in Training

I know what the OP is talking about, I have dealt with some pretty nasty code in my time. I think weird problems like always-true loop conditions that only execute once, etc. are rare problems though and they're much easier to fix.

I once took a venture into the Doom Engine to see how much I could improve its display functions. Even some of the most advanced programmers I know agree that it's a complete mess in there. It has a huge dependence on global variables where it should just be passing values, and goto statements, and uselessly dividing an integer only to multiply it later, resulting in unnecessary accuracy loss. It's often difficult to find out which functions are setting which variables, and where some functions get their variables from. When you have such messy code, it's very difficult to extend (or even untangle!).

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

The main() function is not allowed to be called inside a program. As far as I know, this means no function pointer is allowed to point to it either. I'm sure your results may vary from compiler to compiler. The only sure way is to try it yourself.

Tumlee 42 Junior Poster in Training

More than likely, it's just because printf() and scanf() are functions that have been around since before C++ and if you were to disallow that suddenly, it would cause older code to no longer compile correctly.

Plain vanilla C didn't have all the type-safety that C++ had, and IIRC enum-int conversions were allowed back then.

My only other explanation would be that printf() and scanf() are pretty hacky functions that can't even check if the correct number of arguments are being passed to them. It's entirely possible that they can interperet enums and ints just because they don't know any better.

Tumlee 42 Junior Poster in Training

I'm a rather experienced, self-taught C/C++ programmer. I go by the name Tum in other places and I am involved in SDL game design. I am happy to help people with their coding problems when I can.

Tumlee 42 Junior Poster in Training

Game Maker is actually pretty slow and inflexible, but I guess that's the tradeoff you have to deal with for ease of use. I've had to deal with Game Maker myself when I was making amateur games and even their built-in collision detection functionality was broken for objects that moved quickly.

Tumlee 42 Junior Poster in Training

I'm not even certain if there is a standard factorial function in <math.h>. However, if there is, x! would not be valid C++ for reasons I don't want to get into. If you need a factorial function, you can use this one which I'm writing off the top of my head:

unsigned long factorial(unsigned input)
{
    unsigned long result = 1;
    for(int i = 1; i <= input; i++)
        result *= i;

    return result;
}

From that point, you can just find the factorial of x by using factorial(x)

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

On all of those cases where you commented "to be filled", you should put a "break;" statement after each one or you're going to get problems later on.