Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My second program
#1
This my second program it is pretty useless. Im learning c++ and Im just doing random exercises. It is a statistics generator, so far not many statistics just the basics lol. I dont really like the if structure I think there could of been a better way to do it. Anyway let me know what you guys think of the code. And if there would be anything to add to the statistics of two numbers.

Code:
#include <iostream>
#include <Windows.h>
#include <math.h>
void stat(float,float);

int main()
{
    float x,y;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
    std::cout << "Enter a number: ";
    std::cin >> x;
    std:: cout << "Enter another number: ";
    std::cin >> y;
    std::cout << std::endl;
    stat(x,y);
    std::cin.ignore();
    std::cin.get();
    return 0;
}

void stat(float x, float y)
{
    
    float result [5];
    result[0] = x+y;
    result[1] = x-y;
    result[2] = x*y;
    result[3] = x/y;
    result[4] = pow(x,y);

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
    std::cout << "Printing statistics......" << std::endl << std::endl;
    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    std::cout << x << " + " << y << " = " << result[0] << std::endl;
    std::cout << x << " - " << y << " = " << result[1] << std::endl;
    std::cout << x << " * " << y << " = " << result[2] << std::endl;
    std::cout << x << " / " << y << " = " << result[3] << std::endl;
    std::cout << x << " ^ " << y << " = " << result[4] << std::endl << std::endl;
    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    if (x<y) std::cout << x << " < " << y << " = " << "true\n";
    else std::cout << x << " < " << y << " = " << "false\n";
    if (x>y) std::cout << x << " > " << y << " = " << "true\n";
    else std::cout << x << " > " << y << " = " << "false\n";
    if (x==y) std::cout << x << " == " << y << " = " << "true\n";
    else std::cout << x << " == " << y << " = " << "false\n";

}

Thanks,
Algorithm
~ Digital-Punk
Reply
#2
Good jobThumbsup! You should try out allegro to make some neat games.
Reply
#3
(12-22-2010, 10:21 PM)Scorch Wrote: Good jobThumbsup! You should try out allegro to make some neat games.

Yeah I will, what is it like some kind of gaming app?
~ Digital-Punk
Reply
#4
(12-22-2010, 10:25 PM)algorithm Wrote: Yeah I will, what is it like some kind of gaming app?

It's a 2d Graphics library. Here's an example i've just started.
Code:
#include <allegro.h>


BITMAP * ship;
BITMAP * background;
BITMAP * buffer;
SAMPLE * music;

int x = 300;
int y = 400;



int main(){
    allegro_init();
    install_keyboard();
    install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "A");
    music = load_sample("music.wav");
    play_sample(music,255,128,1000,-1);
    
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

    ship = load_bitmap("ship.bmp", NULL);
    background = load_bitmap("background.bmp", NULL);  
    
    buffer = create_bitmap(640, 480);

    while(!key[KEY_ESC])
    {
    
     clear_keybuf();
     acquire_screen();
    
     draw_sprite( buffer, background, 0, 0);
     draw_sprite( buffer, ship, x, y);              
     blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
     textout_ex(screen, font, "Game by Scorch", 50, 10, makecol(0, 0, 255), makecol(0, 0, 0));
     textout_ex(screen, font, "Stratovarius - Fight", 50, 30, makecol(255, 0, 0), makecol(0, 0, 0));
     if(key[KEY_LEFT])
     {
       x--;
     }
     if(key[KEY_RIGHT])
     {
       x++;
     }
     if(key[KEY_UP])
     {
       y--;
     }
     if(key[KEY_DOWN])
     {
       y++;
     }
     else if(key[KEY_Z])
     {
     clear(screen);
     draw_sprite( buffer, background, 0, 0);
     draw_sprite( buffer, ship, x, y);              
     blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
     }    
     release_screen();
     readkey();
     rest(2);
    
    }
  clear(screen);
  destroy_bitmap(ship);
  destroy_bitmap(background);
  destroy_bitmap(buffer);
   destroy_sample(music);
  return 0;
    
}  
END_OF_MAIN();
Reply
#5
Nice dude Smile
Keep it up, just practice a practice and someday you will get good!
Reply
#6
Grats on this program. I want to see you making any great game, even a Console/Batch based one! Keep it up, pal.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)