Thread: AAAAAAAAAAA C++ sucks

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    52

    AAAAAAAAAAA C++ sucks

    C is so much better.

    But what i came here to do:

    I wrote this base conversion program in C. I was going to use it for encryption.... (not just base conversion, but that's a part of the algorithm). Today i tried porting it to C++. I have absolutely no clue what's wrong with it, but any help would be appreciated.

    Sorry for the lack of comments, if you want an explanation about a certain part just post here and i'll explain.

    Compile with 'g++ -o btob btob.cpp' (or whatever compiler you use / whatever file you choose to save it as).
    It compiles fine, screws up running........ no error message. and gdb isn't helping me this time.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    char toch(int w) {
      if (w > 9) return (w + 55);
      else return (w + 48);
    }
    
    int toi(char w) {
      if (w > 64) return (w - 55);
      else return (w - 48);
    }
    
    string revstr(string str) {
      char tmp;
      int l = str.length(), l2 = l - 1, ong;
      for (ong = 0; ong < (l / 2); ong ++) {
        tmp = str[ong];
        str[ong] = str[l2 - ong];
        str[l2 - ong] = tmp;
      }
      return str;
    }
    
    string itob(int n, int r) {
      string blah;
      int m;
      char tmp;
      int l, l2, ong;
      for (m = 0; (n % (int)pow((float)r, (float)m)) != n; m++) {
        blah[m] = toch((n % (int)pow((float)r, (float)(m + 1))) / (int)pow((float)r, (float)m));
        n -= n % (int)pow((float)r, (float)(m + 1));
      }
      blah[m + 1] = toch((n % (int)pow((float)r, (float)(m + 1))) / (int)pow((float)r, (float)m));
      return revstr(blah);
    }
    
    int btoi(string num, int r) {
      int l = num.length(), ans = 0, ong;
      for (ong = 1; l - ong >= 0; ong++)
        ans += toi(num[l - ong]) * (int)pow((float)r, (float)(ong - 1));
      return ans;
    }
    
    int main() {
      int b1, b2;
      string num;
      cout << "FIRST BASE? ";
      cin >> b1;
      cout << "SECOND BASE? ";
      cin >> b2;
      cout << "NUMBER? ";
      cin >> num;
      cout << itob(btoi(num, b1), b2) << endl;
      return 0;
    }
    And here's the WORKING version in C:
    (compile with 'gcc -lm -o btob btob.c'.... replace names as needed)


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    char toch(int w);
    int leng(char* str);
    void revstr(char* str);
    char* itob(int n, int r);
    int btoi(char* num, int r);
    
    char toch(int w) {
    	if (w > 9) return (w + 55);
    	else return (w + 48);
    }
    
    int toi(char w) {
    	if (w > 64) return (w - 55);
    	else return (w - 48);
    }
    
    int leng(char* str) {
      int x;
      for (x = 0; str[x] != 0; x++) {}
      return x;
    }
    
    void revstr(char* str) {
      char tmp;
      int l = leng(str), l2 = l - 1, ong;
      for (ong = 0; ong < (l / 2); ong++) {
        tmp = str[ong];
        str[ong] = str[l2 - ong];
        str[l2 - ong] = tmp;
      }
    }
    
    char* itob(int n, int r) {
    	char* tmpstr = malloc(sizeof(char) * 1024);
    	int m;
    	for (m = 0; (n % (int)pow(r, m)) != n; m++) {
    		tmpstr[m] = toch((n % (int)pow(r, m + 1)) / (int)pow(r, m));
    		n -= n % (int)pow(r, m + 1);
    	}
    	tmpstr[m+1] = toch((n % (int)pow(r, m+1)) / (int)pow(r, m));
    	revstr(tmpstr);
    	return tmpstr;
    }
    
    int btoi(char* num, int r) {
    	int l = leng(num), ans = 0, ong;
    	for (ong = 1; l - ong >= 0; ong++) {
    		ans += toi(num[l - ong]) * (int)pow(r, ong - 1);
    	}
    	return ans;
    }
    
    int main() {
      char num1[20];
      int b1, b2;
      printf("FIRST BASE? ");
      scanf("%d", &b1);
      printf("FIRST NUM? ");
      scanf("%s", num1);
      printf("SECOND BASE? ");
      scanf("%d", &b2);
      printf("%s\n", itob(btoi(num1, b1), b2));
      return 0;
    }
    Sorry for the incredibly long-ish post.

    P.S. if you decidce you want to use my base conversion algorithm for some reason, and copy it instead of writing your own.... please give me a little credit. I know this is a pretty insignificant program but it was hard for me to write.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    That is about the worst program style I've ever seen. Change all your variable names to something meaningful, and whatever you do, don't ever use variable names like "l" that look like the numeral 1.
    Last edited by 7stud; 01-28-2006 at 07:55 PM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    yeah, i was being lazy.
    l was for length.
    ong was random... i was looking for something to indicate continuation of a loop, ong = short for ongoing.

    any idea how to fix the problem though?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What was "blah" short for I wonder. Make it more readable, then people will be more interested in helping you.
    Sent from my iPad®

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    blah was just there.
    since nobody wants to read through my horrible code, let me ask:
    about strings in C++..... when you pass a string to a function is it just giving you a pointer to the first element?

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    It will pass the entire string. It's an object, so of course it will pass like that.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    hmm i was stepping through it with gdb..... and when i found there was a problem (I think) in itob (Integer TO Base) with "string blah;" i tried doing "string blah = new string;" and got this compilation error:

    btob.cpp: In function ‘std::string itob(int, int)’:
    btob.cpp:27: error: conversion from ‘std::string*’ to non-scalar type ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ requested

    i think that's it, so umm... yeah. what should i do to fix that error?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You don't allocate memory into data, you allocate into pointers.

    Code:
    string *blah = new string;
    ...and I don't think that's your problem.
    Sent from my iPad®

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    hmm....
    this is really confusing.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You realize your C version leaks memory, do you?

    What you don't realize is that std::string has a fixed size at any single point, although it can grow. What this means is that you can't use [] to access a non-existent index. This is exactly what you try in itob, though. You need to resize the string first.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    hmm....
    this is really confusing.
    new returns an address in memory. In C++, addresses in memory are stored in variables that are pointers. Therefore, when you use new on the right of an assignment statement, there has to be an appropriate pointer type on the left side of the assignment.
    Last edited by 7stud; 01-28-2006 at 11:13 PM.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    how does my C version 'leak memory' and how can i prevent it?

    also, how do i resize a string?

    thanks for the help

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by w00tw00tkab00t
    how does my C version 'leak memory' and how can i prevent it?
    You allocate memory here:
    Code:
    char* tmpstr = malloc(sizeof(char) * 1024);
    but never free it. (You also allocate far more than can ever be needed - the longest possible string to be created by itob is sizeof(int)*CHAR_BIT characters long, which on no platform I ever heard of is greater than 40, so 41 is the limit you need to allocate.) To prevent, the typical way is to pass a buffer into itob instead of returning an allocated one. This way, it's clear who is responsible for the memory. In your case, you'd have to free the pointer returned by itob eventually. The trick here lies in knowing (or rather, remembering) that itob actually allocates memory.

    C++ takes these issues away. I can't help but wonder at your topic title and your first line.

    also, how do i resize a string?
    The easiest way is the += operator. It resizes the string as necessary.
    Code:
    string str = "Hello";
    str += ", World!";
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    hmm, i would help but i have no idea what any of thsi means

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    well C is just so much more.... compatible and simpler. even though it's occasionally more complicated, and takes a bit more work (lack of OOP / libraries for certain functinos) i love it.

    anyways..... how would i do it the not-easiest way? i'll use the += op for now though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. this REALLY sucks
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-31-2004, 12:46 AM
  2. How Cool is Java
    By dukemarlon in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 11-28-2002, 05:24 PM
  3. Why europe sucks
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 04-08-2002, 07:02 PM
  4. Why the DX8.1 SDK sucks....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-06-2002, 12:07 AM
  5. Microsoft Sucks
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 110
    Last Post: 11-08-2001, 04:30 PM