Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Program Clean-Up and Tips [C++]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hacksign23
Master Cheater
Reputation: 0

Joined: 26 Nov 2006
Posts: 404

PostPosted: Mon Feb 02, 2009 10:31 pm    Post subject: Program Clean-Up and Tips [C++] Reply with quote

please,

i made a program that converts numbers to roman numerals. It took me a couple of hours because i'm not a high level programmer. I personally think that this wasn't that good. Can anyone look into this and see what i should/shouldn't do in this? thanks

oh, and it makes a
*****
* a *
*****
weird name thingy

umm...
it was made with C++ in Visual Studio Team Edition '08. I used console win32... yup :'D

thanks!

http://www.mediafire.com/download.php?2tyzmoyzmyb yays!

(whole thing + source code)

EDIT: src:

Code:

// learning.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;

string start();
string loop();
string end(string out);
void extra(string character, string past);
int co;
string out;


int _tmain(int argc, _TCHAR* argv[])
{


cout << "enter your name!" << endl;

string name;
string top;
string middle = "* ";
string bottom;
int namesize;
int i;
getline(cin, name);
namesize = name.length();
for(i=0;i!=(namesize+4);i++){
   top+= '*';
}
middle += name + " *";
bottom = top;
cout << top << newline << middle << newline << bottom <<newline;
cout << start() << endl;;
system("PAUSE");
}


string start() {
   string s;
   int i;
   getline(cin,s);
   for (i = 0; i!=  s.size() ; i++) {
      if (isdigit(s[i]) == 0){
         cout << "DIS INCORRECTS! (not a number)"<<endl;
         return NULL;
      }
   }
      co = atoi(s.c_str());
      s = loop();
   return(s);
   
}


string loop() {

   if (co >= 1000) {
      out.insert(out.size(),"M");
      co-= 1000;
      loop();
   }
   if (co >= 500) {
      if (co >= 900){
         co -= 900;
         extra("M","C");
      }else{
      out.insert(out.size(),"D");
      co -= 500;   
      loop();

      }
   }

   if (co >= 100) {
      if (co >= 400){
         co -= 400;
         extra("D","C");
      }else{
      out.insert(out.size(),"C");
      co -=100;
      loop();
   }
   }
   if (co >= 50) {
      if (co >= 90){
         co -= 90;
         extra("C","X");
      }else{
      out.insert(out.size(),"L");
      co -=50;
      loop();
   }
   }
   if (co >= 10){
      if (co >= 40){
         co -= 40;
         extra("L","X");
      }else{
      out.insert(out.size(),"X");
      co-=10;
      loop();
   }
   }
   if (co >= 5) {
      if (co >= 9) {
         co -= 9;
         extra ("X","I");
      }else{
      out.insert(out.size(),"V");
      co -=5;
      loop();
   }
   }
   if (co >= 1) {
      out.insert(out.size(),"I");
      co -=1;
      loop();
   }

return out;
}
void extra(string character, string past){
   out.insert(out.size(), past);
   out.insert(out.size(),character);
   loop();
   return;
}



my "stdafx.h" has all the declarations. kkthx

_________________


Last edited by hacksign23 on Tue Feb 03, 2009 5:47 pm; edited 1 time in total
Back to top
View user's profile Send private message AIM Address
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Tue Feb 03, 2009 1:51 am    Post subject: Reply with quote

you should just post the source here for faster help
_________________
Back to top
View user's profile Send private message
LolSalad
Grandmaster Cheater
Reputation: 1

Joined: 26 Aug 2007
Posts: 988
Location: Australia

PostPosted: Tue Feb 03, 2009 2:12 am    Post subject: Reply with quote

I quite liked the Roman Numeral idea, but it doesn't provide accurate results with some numbers (from my limited knowledge of Roman Numerals, and in comparison to this).

I decided to make the same thing... I don't know how much better than yours mine is in general C++ terms because I am fairly new to C++ as well. Here is my code. (Edit: Made it work like Flytes... mine still sucks in comparison regardless :<)

Also, my Roman Numeral generator appears to, from the few tests I did, provide the same result as what the generator here does.

_________________


Last edited by LolSalad on Tue Feb 03, 2009 3:14 am; edited 2 times in total
Back to top
View user's profile Send private message MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Feb 03, 2009 2:36 am    Post subject: Reply with quote

I programmed this like two hours ago... then I went and got food and forgot about it. Here it is.

Code:
#include <iostream>
#include <string>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;

void inline NameThing() throw()
{
   string name;
   int len;

   cout << "Enter your name: ";
   getline(cin, name);
   len = name.length();

   for(int i = 0; i <= (len+4); i++) {
      name.insert(0, (!i ? "\n* " : "*"));
      name.insert(name.length(), (!i ? " *\n" : "*"));
   }

   cout << name << endl;
}

bool inline ToRN(double number, string *ret) throw()
{
   string num[] = { "I", "V", "X", "L", "C", "D", "M" };
   string out;

   if(number <= 0) return false;

   for(int i = (int)ceil(log10(number)) + 1; i >= 0; i--) {
      if(number >= pow((double)10, i)) {
         int x;
         for(x = 0; x < floor(number/pow((double)10, i)); x++) {
            out += (i >= 3) ? num[6] : num[i*2];
         }
         number -= x*pow((double)10, i);      
      }
      if(number >= pow((double)10, i)*0.9 && i && i <= 3) {
         out += (i > 3) ? "" : (num[(i*2)-2] + num[i*2]);
         number -= pow((double)10, i)*0.9;
      }
      if(number >= pow((double)10, i)/2 && i && i <= 3) {
         out += (i > 3) ? "" : (num[(i*2)-1]);
         number -= pow((double)10, i)/2;
      }
      if(number >= pow((double)10, i)*2/5 && i && i <= 3) {
         out += (i > 3) ? "" : (num[(i*2)-2] + num[(i*2)-1]);
         number -= pow((double)10, i)*2/5;
      }
   }
   *ret = out;
   return true;
}

int main(void) throw()
{
   double number;
   string thing;

   NameThing();

   for(;;) {
      cout << "Enter a number (0 to quit): ";
      cin >> number;
      if(ToRN(number, &thing)) {
         cout << thing << endl;
      } else {
         break;
      }
   }
   cout << "Press \"Enter\" to continue...";
   cin.sync();
   cin.ignore();
}
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Feb 03, 2009 3:33 am    Post subject: Reply with quote

Both of you fail when it comes to long numbers and faulty input, eg. Flyte:
Code:
bug>CEF_Flyte_RomanNumerals.exe
Enter your name: This one is stupid
**********************
* This one is stupid *
**********************
Enter a number (0 to quit): 111111111111111
MMMMMMMMMMMMCXI
Enter a number (0 to quit): 11111111111111111
MMMMMMMMMMMMMMCX
Enter a number (0 to quit): 1111111111111111111
MMMMMMMMMMMMMMMMCLXVIII
Enter a number (0 to quit): 1wewre
Enter a number (0 to quit): I
Enter a number (0 to quit): I
Enter a number (0 to quit): I
Enter a number (0 to quit): I
Enter a number (0 to quit): I
Enter a number (0 to quit): I
Enter a number (0 to quit): I
..and keeps going on..

And the original posters one doesn't say anything.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Feb 03, 2009 2:06 pm    Post subject: Reply with quote

Jani wrote:
Both of you fail when it comes to long numbers and faulty input, eg. Flyte:


Of course you could argue that it didn't handle faulty input. I could also argue that you should stop acting like a 5 year old twat who clearly can't follow instructions. It says, "enter a number", for a reason.

As for the "long numbers", it was a small mistake in the for loop; I forgot to make sure that the log was never higher than 3. Either way, I'd say it was pretty good for a 15 minute hack job, and frankly I really don't care.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Feb 03, 2009 2:50 pm    Post subject: Reply with quote

Flyte wrote:
Of course you could argue that it didn't handle faulty input.
He asked for tips, etc... Your example just had a little more sense what did I mean with faulty input. Also the code being posted here, not packed, etc, was easier to pick up for testing.

Flyte wrote:
I could also argue that you should stop acting like a 5 year old twat who clearly can't follow instructions. It says, "enter a number", for a reason.
Well.. Maybe I'm five year old. :) But actually, for the first time test I did input a letter by mistake.

Flyte wrote:
As for the "long numbers", it was a small mistake in the for loop; I forgot to make sure that the log was never higher than 3. Either way, I'd say it was pretty good for a 15 minute hack job, and frankly I really don't care.
Yeah, you did good.
Back to top
View user's profile Send private message
hacksign23
Master Cheater
Reputation: 0

Joined: 26 Nov 2006
Posts: 404

PostPosted: Tue Feb 03, 2009 7:11 pm    Post subject: Reply with quote

okay guise put up src.
_________________
Back to top
View user's profile Send private message AIM Address
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Sun Feb 08, 2009 10:35 pm    Post subject: Reply with quote

I think this should work and hopefully compile.
Pretty much what you shuld do for your code is to make it go through an array of numbers rather than hardcoding all of them. and your flow is a bit odd when you check if it's a combined (ie iv) number in the check for the number under it.
Code:

#include <iostream.h>
#include <windows>
#include <math.h>
using namepsace std;
int main(){
int numnums = 6;//numer of numerals
char [numnums] lets = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};//numerals
int [numnums] digs = {1000, 500, 100, 50, 10, 5, 1};//numbers of each numeral
int input;
cin >> input;
int diff;
for (int c=0c<numnums;c++){//for each numeral
   while(input >= digs[c]){//while input > the number of the numeral
      input -= digs[c];//subtract the number
      cout << lets[c];}//and print the numeral
   diff = input - digs[c];//check for stuff like 4, 9, etc
if(diff != 5 && diff != 50 && diff != 500)//EDIT: it should show vx rather than v without this.
   for(int x = c+1; x< numnums;x++)//x is the next smallest numeral to the smallest
      if(diff == digs[x]){
         input = 0;
         cout << digs[x] << digs[c];}
}


Fuck, I can't follow Flyte's algorithm at all.

_________________
Back to top
View user's profile Send private message
hacksign23
Master Cheater
Reputation: 0

Joined: 26 Nov 2006
Posts: 404

PostPosted: Mon Feb 09, 2009 11:04 pm    Post subject: Reply with quote

i see . okiedokie thanks
_________________
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites