Archived

This topic is now archived and is closed to further replies.

can someone please optimize this chunk of code?

This topic is 6033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is a bit of code that runs a little slow from my text-engine, please optimize it to run faster, thx
  
#include <ifstream.h>
#include <iostream.h>

struct rpg() 
{
   .age=20;
   .mana=5;
   char name[32]=NULL;
} rpg MAN;

void charinfo(MAN tempguy) 
{
  cout << "Enter your age here: ";
  cin >> tempguy.age[];
  cout << "\n\n\n\nEnter your mana amount here: ";
  cin >> tempguy.mana;
  cout << "\n\nEnter your name here: ";
  char name[32]=NULL;
  cin.getline(name[32],32);
}

void main()
{  
  cout << "Beta version LINK-RPG Text Game" << endl << " " << endl;
  cout << "Press a key to continue!" << cin(getline,"\n");
  cout << "-------------------------------" << endl;
  charinfo();
}

it takes a while for the cin''s  to register in the allocated memory space, so how can i speed it up?!  

Share this post


Link to post
Share on other sites
Aww...come on. Although he might gain a very slight speed increase by using stdio functions instead of streams, that wouldn''t even be noticable for a small program like this. And he would lose all the benefits of type safety, extensibility etc. I think Null And Void has the right idea - its probably related to buffering.
(BTW: Is noone going to comment on the fact that his code will obviously not compile? No? Sure? Ok then, then I won''t either. )

Share this post


Link to post
Share on other sites
quote:

Aww...come on. Although he might gain a very slight speed increase by using stdio functions instead of streams, that wouldn''t even be noticable for a small program like this. And he would lose all the benefits of type safety, extensibility etc. I think Null And Void has the right idea - its probably related to buffering.
(BTW: Is noone going to comment on the fact that his code will obviously not compile? No? Sure? Ok then, then I won''t either. )



You''re right, it doesn''t compile. What compiler are you using? And if its a text-engine, why does it have to optimized. How fast are these people typing?

If you really want a speed increase, pass your structs by reference or by pointer.

Share this post


Link to post
Share on other sites
Ok what are the () after struct rpg there for? I have NEVER seen anything like that in any c or c++ program. The memebers age and mana do not have types and have a period before them. Ive never seen anything like that before so what does that mean? Also after } there is rpg MAN. That space would cause a compile problem too. Oh and assigning char name[32] inside the struct NULL is an error. If these things are working in his compiler, i must say, that is one F**KED up compiler.

Welp, i just spotted something else. After the first cin he has cin >> tempguy.age[]; Im pretty sure that [] shouldnt be there. The cin.getline at the end of the function is not right either. it should be cin.getline( name, 32 ); The line just above that dont seem quite right either.

Ok thats enough.

-SirKnight

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster

Don''y use cin. Use something like scanf() or gets(), getchar(), etc.



You shouldn''t use scanf or gets ever. Replace them with a call to fgets. Why? They aren''t very ''safe''. gets has the risk of overflowing the buffer, and scanf just screws up if you enter something that it isn''t expecting(try scanf("%d", &x) and enter "Hello").

Thus, for inputting(is there such a word?) strings, you should use fgets. For inputting numbers (floats, ints, etc), use fgets and atoi/atof.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hm, the struct reminds me of intel syntax x86 assembly.

Share this post


Link to post
Share on other sites