Results 1 to 10 of 10

Thread: C++

  1. #1

    Default C++

    I'm looking for a person who would like to help me with my IT project I have a few questions about C#. Unfortunately google is not helpful Anyone is willing?

    Thank you
    Last edited by caffelatte; 05-Jan-2013 at 20:59.

  2. #2

    Default



    Quote Originally Posted by caffelatte View Post
    I'm looking for a person who would like to help me with my IT project I have a few questions about C++. Unfortunately google is not helpful Anyone is willing?
    Your best bet would be to post the questions here, so anyone who is able to answer them could help.

    You might also find the programming group here useful: https://www.adisc.org/forum/groups/106-programmers.html

  3. #3

    Default

    I need to write a program which is a translator. Lets say ENG<->GER
    Assumptions: I have 2 *.txt files (one with ENG words & one GER). The biggest problem is that I have no idea about using memory in C language (I was looking in Google, but without success). I need to load data to memory and then do a translation.. Anybody can help me?

  4. #4

    Default

    you mean you need to open a file?

    ifstream - C++ Reference

    There's the docs for one of the easier to use libraries for getting input from a text file.

    Depending on the exact way the file is set up, you can extract data from here pretty easily. If each file consists of each word on a separate line then you can use the getline() function which will copy the contents of that line into an array of characters. Do you know how to manipulate strings and use char*?

  5. #5

    Default

    Thank you
    But have u ever heard about malloc function? Probably I will have to use this..

    Please remember that it has to be C (not C++) language

  6. #6

    Default

    Whoops! I didn't see that there, Hogan's mixed me up and I thought you were working in C++.

    For C you need to use stdio.h to extract characters from a file.

    fscanf - C++ Reference That's more or less what you've got to grab stuff from the file.

    What that function does is copy letter by letter the contents of a file into character arrays you have constructed. You will need to allocate your own memory to store the data somewhere.

    a char represents a single character. An array of characters can be used to form a word. Note how in the example on the site I linked, they make a char str[80], which means that str has the memory allocated to store 80 characters at a time. This will work to get, say, a single line from the file and store it in that character array, but what you are trying to do is have multiple entries in your dictionary, meaning that you need an array of words in your dictionary, and since words are arrays of characters, you need an array of arrays of characters.

    One way to do this is to statically declare an array of arrays of characters, (like char arrayOfWords[100000][80]) which has space for 100000 words which have no more than 80 characters in them. As long as your dictionary never had more than 100000 words in it and no word had more than 80 characters you would be fine, you could always increase the static dimensions to be safe, but the problem there is that you become rather wasteful of memory.

    What you'd like to do is figure out exactly how many characters you'll need to store all the letters of all the words in each dictionary, then get an array that is exactly that size and fill it up. To do this you'll need to dynamically allocate memory.

    Dynamic allocation of memory is tricky, and there are some ground rules first. When you ask for an array or an int as I did up there, the program knows ahead of time how much memory to set aside for your array, and it is well defined when you are using that memory, and when you are no longer using the memory (when the program goes outside of the curly braces the variable is declared in, you no longer need that memory). With dynamic memory, the computer doesn't know how much memory it will need beforehand, and it can't make assumptions about when you no longer are using that memory.

    The first thing about malloc is that whenever you use a malloc, you must use a free() somewhere later on in your program. malloc() says "I need this much memory to fill out my array right now" and free() says "okay I'm not using this memory anymore, go ahead and overwrite it". This is important, because the computer will do whatever you say and if you malloc some memory and never free it then the computer will not be able to use that memory again until your program terminates. This is wasteful because if you aren't using that memory anymore then you should let the computer know that it can use that memory for other stuff. On the flip side, if you call free() on some memory, then write to your old malloced array after the free(), that is valid but the computer may have given your memory away to something else to use. This is really bad, because now you have potentially two places writing to the same spot in memory, which is like taping over someone else's VHS tape half way through, where you get some stuff that you wanted and then potentially something else written where you thought your stuff would be.

    TLDR: If you use a malloc, always use a free.

    The double problem you face is that you don't just want a dynamically allocated array, you want a dynamically allocated array of arrays which has some really yucky syntax for creating and cleaning everything up in C. On your first try I'd probably take the memory hit and create a static 2D array that has waaaay more space than you need, and get the program working first, then you can try using malloc to make it more memory efficient. If you get the 2D array version working, then come back and we can work on the malloc part.

  7. #7

    Default

    Thank you so much This kind of explanation is everything I need

  8. #8

    Default



    Quote Originally Posted by caffelatte View Post
    I need to write a program which is a translator. Lets say ENG<->GER
    Assumptions: I have 2 *.txt files (one with ENG words & one GER). The biggest problem is that I have no idea about using memory in C language (I was looking in Google, but without success). I need to load data to memory and then do a translation.. Anybody can help me?
    LBcub explained how to use memory in C better than I could, but I thought I'd point out that it is confusing how you've been interchanging C, C++, and C# in this thread.

    C++ does have backwards compatibility with most of C, so most C programs will compile with a C++ compiler. Otherwise, the languages and libraries are distinct. Although the basic syntax of loops, if statements, etc. is very similar, what works in one of the languages won't necessarily in another one of the languages, so it's important to be clear which language is which when looking for help.

  9. #9

    Default

    clarification: C is a subset of C++. Anything you do in C will work and run the same way in a C++ compiler ( or SHOULD), all libraries that work in C work in C++. The same is not true going the other way. Stuff that was compiled in C++ will not work with strict C code. Stupid programmer in-joke: In C and C++, the "++" operator takes the value of a number, increments it by 1, and assigns it back to the variable (so var++ is equivalent to var += 1 or var = var + 1) c++ therefore is the language C, incremented by 1 :P C# is also a play on this, only the # is a musical reference.

  10. #10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
ADISC.org - the Adult Baby / Diaper Lover / Incontinence Support Community.
ADISC.org is designed to be viewed in Firefox, with a resolution of at least 1280 x 1024.