Register
Email: Password:
Forum » Learning C++
  • « previous
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • » next
  • Learning C++

    NeoGangster 19 years ago
    if we have problems with C++ we can ask ville
    hmm...maybe I'll join the contest
    The only thing that i can do in windows(without directX) is draw a dot and move it around XD thats my game *g* I'll call it..."The dot moving game" the longer you the more points you get XD
    #
    HarmlessHermit 19 years ago
    Make the dot a ship with weapons, make the plain screen a 60+ system map and add "sister-dots" which you fight with
    #
    void 19 years ago


    if (GetKeyState('S') & 0x80) // Whats with the 0x80?
    {


    }




    Ville how would I do this for a mouse? I want the it to check if the mouse is in a cetain area of the screen and if it is display.. whatever.

    I'm assuming you did this for Notrium so you should know........
    #
    ville 19 years ago
    "void" said:
    Ville how would I do this for a mouse? I want the it to check if the mouse is in a cetain area of the screen and if it is display.. whatever.

    I'm assuming you did this for Notrium so you should know........

    In Notrium I used a graphics engine. It had a command, something like CheckMouseLocation. I just used that to get the mouse location, and which buttons are pressed. If you are doing a simple game and don't want to be bothered with technicalities (like I don't), you should use a graphics engine. Or better yet, use a whole game engine. Sourceforge is the place to go for these.

    So in essence, I have no idea how to do that for the mouse.
    #
    void 19 years ago



    your cruel


    (when I find out how to I'll post it)
    #
    void 19 years ago
    Soooo......

    What progress has been made? Tell us what you current programming ability is and what you're learning (or not learning ).

    I'm still strugling to learn directx and win32. I hoping to find a 3d engine that will let me skip learning directx for now.
    #
    Amarth 19 years ago
    For me... I haven't done much yet. I just received my new computer (finally ) and right now I'm installing windoze, later on i'll be trying linux(downloading right now), and i think i'll start programming in about a couple of months...
    I'm thinking of using SDL for my game, it seems like a very useful library.
    I haven't coded C++ for a long time, I hope I still can do it after a couple of months of intensive Java... Not that much of a difference, but memory management and error cathing will be a major pain I think... Hopefully SDL will take most of it out of my hands.
    Thus, I'll start diving into C++ and SDL the following weeks... I'll keep you informed of major breaktroughs . When it's worth it, I'll make a website for the game, too.

    -Amarth
    #
    Marevix 19 years ago
    I'm learning C++ in my spare time. I don't know why everyone says it's so hard. It isn't that difficult if you take your time and learn the basics thoroughly before trying anything else. Then again, I don't know much more than the basics. The key to learning properly, like I've heard from people, is to make programs. Once you go through a tutorial and understand what it all means, make one yourself, only looking at the tutorial source if you don't remember the syntax(i.e. I always thought it was #include (program) instead of <program> for a while) until you can make that program easily without having to debug too much.

    Like I've also heard other people say, C++ is actually [not] as hard as people make it out to be.

    [Edit: Jesus, I must've been high when I wrote that.]
    #
    void 19 years ago

    [Edit: Jesus, I must've been high when I wrote that.

    Why do you say that? What's getting you down? Structures, arrays, pointers, file handling, or are you further down the line then all that?



    I sad that nobody has been posting the code they're getting confused with If I had this forum back when I was learning it would have been flooded with code I didn't get.
    #
    Marevix 19 years ago
    Read the bracketed section of the last couple sentances, I forgot to put the 'not' in 'It is [not] as hard to learn C++ as most people say' part. The bracket is for the edit.

    [Edit: Removed Kazaa refrence, broke the no illegal warez rule]
    #
    Amarth 19 years ago
    "void" said:
    I sad that nobody has been posting the code they're getting confused with If I had this forum back when I was learning it would have been flooded with code I didn't get.

    I'd post my trouble code on a specialized forum, thay're more likely to help you and to provide good help. Not that i doubt anyone being helpful here, it just doesn't seem appropriate....

    Current status: searching my copy of MSVS... I fear I have left it at home, meaning 2 weeks without it... I'd better download a free compiler, but I'm almost at my download limit, anyway when I'm changing to Linux (I NEED to try that once in my life ) I'll need to download one anyway... Thus next week when the new month starts, I can download at full speed and start doing some *useful* things

    -Amarth
    #
    Marevix 19 years ago
    Does anyone know a command in the iostream library that can stop the rest of the program from executing and letting it close from a press of the enter key? I'm trying to make a small program to ask a few easy mathematical questions, and I want the program to end at the press of the enter key if someone gets a question wrong. Here's the code itself, it compiles fine. If you test it you might see what's going wrong.


    #include <iostream>

    using namespace std;

    int main()
    {
    int one;

    cout<<"What's 2+2? ";
    cin>> one;
    cin.ignore();
    if ( one == 4 ) {
    cout<<"Correct!\n";
    }
    else {
    cout<<"Incorrect.";
    cin.get();
    return (0);
    }
    cout<<"\nWhat is 4 to the third power? ";
    cin>> one;
    cin.ignore();
    if ( one == 64 ) {
    cout<<"Correct!\n";
    }
    else {
    cout<<"Incorrect.";
    cin.get();
    return (0);
    }
    cin.get();
    }
    #
    void 19 years ago
    THANK YOU!!!!!!!! I'm glad we finally have someone posting code. I think what your looking for is the return() statment.

    int main() is a function expecting an integer to be passed to it thus when you write 'return (0)' it passes an integer an stops the program (or exits the function).

    The bits of the code I commented out with the // are bits that I never use and to be honest I don't even know exactly what they do, the program will run fine without them.

    Anywho... Try compiling the code and tell me if it works for you.

    #include <iostream>

    using namespace std;

    int main()
    {
    int one;

    cout<<"What's 2+2? ";
    cin>> one;
    // cin.ignore();

    if ( one == 4 )
    {
    cout<<"Correct!\n";
    }
    else
    {
    cout<<"Incorrect.";
    // cin.get();
    return (0);
    }

    cout<<"\nWhat is 4 to the third power? ";
    cin>> one;
    // cin.ignore();

    if ( one == 64 )
    {
    cout<<"Correct!\n";
    }
    else
    {
    cout<<"Incorrect.";
    // cin.get();
    return (0);
    }

    // cin.get();
    return (0);
    }


    Nice little app by the way, it's good to see your learning. How long have you been learning anyway?
    #
    Marevix 19 years ago
    Thank you very much, it works. I've been coding for about a week, I'm getting better. Working on making a command-line calculator.
    #
    void 19 years ago
    That's impressive progress for a week. Make sure you keep on asking questions.

    On an unrealated topic

    you can leave this out:

    using namespace std; 


    if you change

    #include <iostream> 


    to

    #include <iostream.h> 


    I only realized that when I was compiling your code. Not sure why. I really need to look some of this up.
    #
    Marevix 19 years ago
    About 40 lines into the code, I ran into a problem. I know that to multiply you have a function called int mult ( int x, int y );. What are the addition, subtraction, and division versions of this function?

    Btw, I know how the function works, just to be sure you know.
    #
    Amarth 19 years ago
    Can't you just use x*y, and as well x+y, x-y and x/y? That sounds much easier to me anyway

    -Amarth
    #
    Marevix 19 years ago
    The only way I know how to code the calculator is to have 4 seperate int functions after int main() that return the answer to the question. I'll try your idea though, but I'm not sure if it'll work.

    [Edit: Amarth's way worked as well, but I'd still like the names of those functions in case I ever need more complex ones without cluttering up the main code, like powers or something]
    #
    Marevix 19 years ago

    #include <iostream>

    using namespace std;

    int main()
    {
    int x;
    int y;
    int choose;

    cout<<"Command Line Calculator\n-Marevix, 2004.\n\n";
    cout<<"Please select your form of calculation:\n";
    cout<<"1= Add\n2= Subtract\n3= Multiply\n4= Divide\n5= Exit\n";
    cin>> choose;
    cin.ignore();
    if ( choose == 1 ) {
    cout<<"\nPlease enter two numbers to be added.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"Answer: "<< x + y <<".";
    cin.get();
    return (0);
    }
    else if ( choose == 2 ) {
    cout<<"\nPlease enter two numbers to be subtracted.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"Answer: "<< x - y <<".";
    cin.get();
    return (0);
    }
    else if ( choose == 3 ) {
    cout<<"\nPlease enter two numbers to be multiplied.\n";
    cin>> x >> y;
    cin.ignore ();
    cout<<"Answer: "<< x * y <<".";
    cin.get();
    return (0);
    }
    else if ( choose == 4 ) {
    cout<<"\nPlease enter two numbrs to be divided.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"Answer: "<< x / y <<".";
    cin.get();
    return (0);
    }
    else if ( choose == 5 ) {
    cout<<"Goodbye.";
    cin.get();
    return (0);
    }
    else {
    cout<<"\nYou entered an invalid answer, terminating due to lack-of-info bug. Hahah.";
    cin.get();
    return (0);
    }
    }


    Amarth's idea worked. Here's a calculator.
    #
    Amarth 19 years ago
    Other functions are as easy to write yourself. To give you some ideas (this is not C, this is some half c-half java-half pseudo i write my ideas in, thus it'll probably not compile but it ought to give you the idea)

    int power(int base, int exp){
    int answer=1;
    for(int i=1;i++;i<=exp)
    answer *= base;
    return answer;
    }

    I think you should try to search the mathematical definitions for each function you want to implement and then code them... But of course these functions may already exist, there certainly are functions for sin and cos and the like, but you'll have to find'em .

    -Amarth
    #
    void 19 years ago
    I think the functions you want are just 'int add()' and 'int subtract()' they should work.

    int Add(int a, int b)
    {
    return a+b;
    }


    The layout of your program looks scary (I'm pretty sure that some forum members have looked at it and instantly decided that c++ is impossible.) Now you should try to optimize it. I'll give you a challenge.


    - replace the giant if statement with a switch case statement (you'll be able to google it in a matter of seconds)

    - When you've done your first calculation make it go back to the menu so you can do more sums without having to reload the app. (hint: maybe you could use a loop of some sort)

    - Make it calculate decimals as well ( if your not sure how to do this I'll tell you it's very simple and all you have to do is change the 'int x, int y' to 'float x, float y')

    - Make the code look neat and easy to read (use the TAB key)


    If you can do this I'll be very impressed. It is possible to make the changes I've said without having to increase the size of the program. Good luck and if you have trouble just ask.
    #
    Marevix 19 years ago
    Giant if statement- can fix, learned how to do switch statements.
    Loop- Wanted to do, but couldn't figure out how. Will re-read loops tutorial, figure out how.
    Decimals- Can do.
    Code neatness- I did that, but it doesn't stay formatted through copy and paste, would have to rewrite in message box.

    [Edit: It's done.]


    #include <iostream>

    using namespace std;

    int main()
    {
    float x;
    float y;
    int choose;
    int loop;

    cout<<"Command Line Calculator\n-Marevix, 2004.\n\n";
    while ( loop != 1 ) {
    cout<<"Please enter your form of calculation.\n";
    cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\n";
    cin>> choose;
    cin.ignore();
    switch ( choose ) {
    case 1:
    cout<<"\nPlease enter two numbers to be added.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"\nAnswer: "<< x + y <<".\n\n";
    cin.get();
    break;
    case 2:
    cout<<"\nPlease enter two numbers to be subtracted.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"\nAnswer: "<< x - y <<".\n\n";
    cin.get();
    break;
    case 3:
    cout<<"\nPlease enter two numbers to be multiplied.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"\nAnswer: "<< x * y <<".\n\n";
    cin.get();
    break;
    case 4:
    cout<<"\nPlease enter two numbers to be divided.\n";
    cin>> x >> y;
    cin.ignore();
    cout<<"\nAnswer: "<< x / y <<".\n\n";
    cin.get();
    break;
    case 5:
    return (0);
    break;
    default:
    cout<<"\nYou have entered an invalid command.\n";
    cin.get();
    break;
    }
    }
    }
    #
    Amarth 19 years ago
    I also noted code indentation doesn't stay as it's supposed to be... From now on, use code-tags everyone (including myself).

    For the loop... I'll give you a hint:
    There are 2 ways, the Good and the A Bit Less Good Way.
    The second way works like this: have a never ending loop in your program (while(true)), and put an item in your menu that jumps to some 'return(0)' code... so your program will exit.
    The Better Way works almost the same, but your while-loop now checks a boolean variable you set at true at the start of the program, and at false once the user selects 'quit'. This might be a tiny bit harder to implement, but you have to learn it anyway. This is better, mostly because it is more logical to read: your program will stop at the end of the code, and not with some return-statement somewhere in the middle. This might seem unimportant now, and for now it is, but try to keep clean code. It's so much easier to keep going, then.

    One other remark, about the if's:
    Don't be scary to use them. It may look as if it is an incredibly nested structure, but C manages to handle those very well. The trouble with switch-statements is that you'll have to use 'breaks' to prevent fall-trough... And that's not something vey nice. In case you want fall-through, use switch, otherwise, stick to if.
    Well that's my view about it anyway... Some might find the switch/break stuff very good working, i don't... Actually make your own choice .

    I'm feeling as if I know a damn lot about coding now... Keep asking questions, it makes me feel good .

    -Amarth
    #
    Marevix 19 years ago
    Ehh.. I know what you're talking about, but how do you set the boolean variable to 'true' in the first place(or is it already there, like integers automatically at 0)?
    #
    void 19 years ago
    The reason I suggest using a switch statment instead of nesting is because it is easier to read by you and the people who look at your code. If statements are great but for the program you've created I'd use a switch statement. When you write your code you'll see the difference it makes.

    When I suggested using a loop I was thinking:


    do
    {



    }while ( choose != 5);


    For those that arn't sure saying != is the same as saying not equal to

    This is assuming that when you enter 5 you want the program to exit. What Amarth suggested would work fine aswell but like he saiid it could get a little needlessly complicated.

    EDIT: Nice work!
    #
    Anonymous1157 19 years ago
    Uh... That calculator code scares me, i'll stick to Java and Batch files.

    My calculator (mega-simple):

    function add(); {
    A = (document.form1.field1);
    B = (document.form1.field2);

    alert (A+B); }
    #
    Amarth 19 years ago
    "Marevix" said:
    Ehh.. I know what you're talking about, but how do you set the boolean variable to 'true' in the first place(or is it already there, like integers automatically at 0)?

    bool name = true;
    In front of your loop .
    Although I'm not really sure true is the way to go here... I do think so but it could be the constant 'true' is not declared...
    Nevermind, try it out. If it doesn't work, ask again here. It's a long time ago I coded C anyways.

    -Amarth
    #
    void 19 years ago

    function add(); {
    A = (document.form1.field1);
    B = (document.form1.field2);

    alert (A+B); }


    I don't understand The code you just wrote. C/C++ is no harder then java. You just have to learn what a few key words like cout and endl mean to make it less scary.

    If you want to use bool do something like this:


    bool exit = false;

    do
    {

    case 5:
    exit = true
    break;

    }while (exit = false);


    bool is just a data type like int, float, char, or anything else.
    #
    Marevix 19 years ago
    Heheheh.. Code I learned in a week is scaring Anonymous . Kind of odd how I now know basic C++ but not basic Notrium modding. Either way, I'm done with the calculator project. If I could remember html I could go and add a C++ project section to my retired website. My next project is figuring out how structures and arrays work, but so far all I can figure out about structures is that you can store variables in them and store variables in those variables. Anyways, what do debuggers do? I have one, but can't quite figure out what to do with them.
    #
    void 19 years ago
    Not sure, I've never used a debugger. Arrays are boring and structures arn't much fun either. If you can't I'll try and think up a challenge for you.
    #
    Anonymous1157 19 years ago
    My code is problably bad, I havent used Java for a while. But I know one programming language nobody can forget once they use it a lot: Command line type thingy! BTW what's its correct name? Basic DOS?

    title TEST FILE BY ANONYMOUS1157
    @echo off
    cls
    echo Copyright Anonymous1157
    echo ===================
    echo;
    echo Test file. If you can read this, i'm not an idiot.
    pause
    if not exist ../Companion.txt goto hell

    :OK
    cls
    echo The companion file exists. Press any key AGAIN to exit.
    pause
    goto exit

    :hell
    cls
    echo A companion file dosen't exist. Press any key AGAIN to exit.
    Echo;
    echo look for the companion file, Companion.txt or just make it up.
    pause

    :exit
    exit

    Copy that into a text file and rename it into a BAT file, then run it.

    I put a joke in there; get it, goto hell?
    #
    void 19 years ago
    goto hell
    :hell


    hehe

    nice. Make sure you enter the game making comp even if it's just a batch file that calls everyone carrots.........
    #
    Marevix 19 years ago
    Gah.. Can't figure out how to make it into a batch, renaming doesn't work. Makes it into name.bat.txt.
    #
    void 19 years ago
    Just copy and paste into notepad then go save as.. then give it a name with .bat at the end and it should work.
    #
    Marevix 19 years ago
    That worked. I'm not quite sure what it's meant to do though.

    I found a DOS battleship game, very simple. 29,000 lines of code .
    #
    Forum » Learning C++
  • « previous
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • » next
  • Post Reply


    Your email:
    Your name: