• ### Announcements

#### Archived

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

# can someone please optimize this chunk of code?

## 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 #include 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?! 

Go to windows.

##### Share on other sites
"cin.flush()" is probably what you''re looking for. I don''t use the C++ i/o stuff, but it''s something like that.

http://druidgames.cjb.net/ (If my website isn''t down, it''s a miracle!)

##### Share on other sites
Don''y use cin. Use something like scanf() or gets(), getchar(), etc.

##### 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 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 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 on other sites
Huh? It compiles fine, its just slow! I''m using VC++ 6.0

##### 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 on other sites
The original post has to be a joke.

##### Share on other sites
I think he is just goading everyone on. The program doesn''t compile and will NOT compile as written. Unless you have defined some really funky macros this code is not valid C++;

This post reeks of a post meant to waste our time.

Dire Wolf
www.digitalfiends.com

##### Share on other sites
Hm, the struct reminds me of intel syntax x86 assembly.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627653
• Total Posts
2978433

• 10
• 12
• 22
• 13
• 33