Page 8 of 21 FirstFirst ... 34567891011121318 ... LastLast
Results 106 to 120 of 307

Thread: Coder's Hang-out

  1. #106
    Join Date
    Aug 2004
    Posts
    6,802
    Thanks
    30
    Thanked 96 Times in 48 Posts
    EP Points
    195

    Default

    Quote Originally Posted by pkt-zer0
    Getchar would be better in case of a menu, since it's not buffered, by default. With scanf that would require flushing the buffer on erroneous input. Your call, anyway, but unless you need to read more than one character at a time (like, inputting a name, a date, or something similar), use getchar.
    well its just for the start of my morse code program, I want the user to select a bunch of options for example

    Morse code conversion program

    write text to be converted: 1
    select a text file to be converted: 2

    and so on and so on, and any other option would simply yield a the same prompt if you know what I mean.

  2. #107
    Join Date
    Feb 2005
    Location
    Limbo
    Posts
    3,706
    Thanks
    0
    Thanked 2 Times in 2 Posts
    EP Points
    10

    Default

    That's what the code I put there does. Just add a few more cases, one for every menu option you need, and add a function that does that, and launch it there. I was unclear again, I'm afraid, so here's a part of a small, simplified database-management proggy.
    Download Links:
    Links are hidden from guests. Please register to be able to view these links. The individual functions you'll have to write yourself, though.

  3. #108
    Join Date
    Aug 2004
    Posts
    6,802
    Thanks
    30
    Thanked 96 Times in 48 Posts
    EP Points
    195

    Default

    Quote Originally Posted by pkt-zer0
    That's what the code I put there does. Just add a few more cases, one for every menu option you need, and add a function that does that, and launch it there. I was unclear again, I'm afraid, so here's a part of a small, simplified database-management proggy.
    Download Links:
    Links are hidden from guests. Please register to be able to view these links. The individual functions you'll have to write yourself, though.
    Thanks. I'm not trying to get you to do my assessment for me, I'm just trying to understand a few functions that I think I can't understand. Here is my one if I can remember correctly

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. hmm, I'll have to look a bit more at my notes, I might show you my binary conversion program just to get a few critisms from you

  4. #109
    Join Date
    Feb 2005
    Location
    Limbo
    Posts
    3,706
    Thanks
    0
    Thanked 2 Times in 2 Posts
    EP Points
    10

    Default

    Just a few corrections.

    int main(int argc, char* argv[])
    The latter is an array of strings, used to pass arguments to the program from the command line. You won't be using it that much, however.

    getchar()

    As I've pointed out, getchar is a function, returning the last input character, so you'll have to store its return value in a variable. Correct usage would be c = getchar();

    if(getchar == 1)

    Very, very wrong. getchar is a function. Therefore, it's just an address of some code in memory. By adding the () to its end do you actually make a function call. But still, that would definitely not be what you're wanting here, since you've already read the input (into the c variable), you just need to process it now. That's what the switch statement is for, you might not have known about it, though. It's basically a bunch of if statements, checking if the given value equals a set of constants, and jumping to the proper location. Similar to a 'case' statement in some other programming languages.

  5. #110
    Join Date
    Dec 2004
    Location
    Fantasy Dreamland
    Posts
    1,655
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    Hmm. Don't want to interrupt, but how about this...

    //removed the code
    Last edited by MegaVolt; 22nd-December-2005 at 19:13.


  6. #111
    Join Date
    Aug 2004
    Posts
    6,802
    Thanks
    30
    Thanked 96 Times in 48 Posts
    EP Points
    195

    Default

    Hmm okies, thanks Pkt and megavolt, I'll note these.

  7. #112
    Join Date
    Dec 2004
    Location
    Fantasy Dreamland
    Posts
    1,655
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    Sorry I couldn't wrote it earlier because of sudden OS crash,
    but although my code works...somehow...almost like it should
    I think you'll do better if you stick
    with what pkt-zer0 wrote...well yeah it's the best way.


  8. #113
    Join Date
    Aug 2004
    Posts
    6,802
    Thanks
    30
    Thanked 96 Times in 48 Posts
    EP Points
    195

    Default

    I got it working now. the program has a basic selection thingy that selects 4 options (based on 1,2,3,4 being pressed) I included a thing that said

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. After I finish the rest of the program, I'm going to try and fix that so it will merely prompt it again, now I need to understand how to get arrays and pointers working I have a basic idea, but theory and practice are two different things

  9. #114
    Join Date
    Apr 2005
    Location
    Pennsylvania, USA
    Posts
    520
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That they are, and learning is a concept that comes from combing both. In other words, try it and see what happens. Also, (not reading back yet) if you're using C/C++ your printf will give you.... Alot of problems....

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Format, being a const char array, is easily fixed with "SOMETHING HERE" and you use "%[flag]" for where you want your variables, then place them in appearance order. Where variabes are, you seperate them with a comma.

    FLAGS(the "percent codes"):

    i = int
    c = char
    s = string
    wc = wide char
    ws = wide string

    I forget what the others are, but you'd probably only ever need float other than that.

    ADDITION:

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Download Links:
    Links are hidden from guests. Please register to be able to view these links. Same as above, only you need to make things into longs or shorts, there is a hard way to do this, and there is an easy way to do this. The hard way is probably what you're guessing as reading this. Easy way is to put the L infront of a constant array.

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Now there are ways of getting input. (In wide, i don't know what non wide equivalent of _getws is so i'll use wide examples, i stress on wide anyway, because international characters are slower but less biased!)

    scanf (wide: wscanf) is just like printf (wprintf) only that for arrays you must include widths... now, i forget how to do that so bear with me... lol the alternative route is the fallowing...

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. variable, being an array, it's just like getline only you don't have to specify size and stuff. And i'm not about to go into file streams, go to msdn if you want that. lol

    As for arrays! Well, won't take you long to get used to them... Pointers... on the other hand are annoying... I don't like pointers. Sometimes pointers can be unpredictable if you want to use them to store data. [u]never use them to store large amounts of data.[/b] Sometimes when i use pointers for dynamic allocation, i find the program crashing... Don't know if that's my fault, or if it's the fact that pointers without referances are unpredictable.

    And a trick:

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. And yes, that means like const ints are Download Links:
    Links are hidden from guests. Please register to be able to view these links. or somthing like that. const chars are Download Links:
    Links are hidden from guests. Please register to be able to view these links. and const char array is Download Links:
    Links are hidden from guests. Please register to be able to view these links. .
    Last edited by kohlrak; 24th-December-2005 at 19:56.

  10. #115
    Join Date
    Aug 2004
    Posts
    6,802
    Thanks
    30
    Thanked 96 Times in 48 Posts
    EP Points
    195

    Default

    hmm, I see, I'll have to get figure something out for that when I get to it, my next problem is reading arrays, at the moment I have 2 arrays, the alphabet, and the morsecode all laid out in order, what I need to do is to get it so that for every letter typed, 4 characters of morsecode is printed. so I'll probably spend tomorrow figuring that out, I have a decent idea of how to go about it, but I've had this day off because I really can't be arsed with it today

  11. #116
    Join Date
    Apr 2005
    Location
    Pennsylvania, USA
    Posts
    520
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i made an edit while you posted, just read up on it.

    As for your problem... Might help to use string...

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Then do all the cases... And for each letter, you make the morse code letter appear in the string with something like Download Links:
    Links are hidden from guests. Please register to be able to view these links. Might be an easier way to go at that, but that's how i'd take that one on. lol Now getting the string into a char array, ask PKT or someone for that. I always had trouble with that. Just take all the values and put them in a dynamically made array like below....

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Note strings dont' directly convert to char array or chars. So.... Like i said, ask for that one, but don't ask me. lol

  12. #117
    Join Date
    Aug 2004
    Posts
    6,802
    Thanks
    30
    Thanked 96 Times in 48 Posts
    EP Points
    195

    Default

    Quote Originally Posted by kohlrak
    i made an edit while you posted, just read up on it.

    As for your problem... Might help to use string...

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Then do all the cases... And for each letter, you make the morse code letter appear in the string with something like Download Links:
    Links are hidden from guests. Please register to be able to view these links. Might be an easier way to go at that, but that's how i'd take that one on. lol Now getting the string into a char array, ask PKT or someone for that. I always had trouble with that. Just take all the values and put them in a dynamically made array like below....

    Download Links:
    Links are hidden from guests. Please register to be able to view these links. Note strings dont' directly convert to char array or chars. So.... Like i said, ask for that one, but don't ask me. lol
    hmm. You see I think they want me to use a strcpy fucntion, and they don't want it done that I put 1 value for 1 value if you know what I mean, I think they want me to be able to take a value from a array and point it to another value from another value, I'll try and post it later

  13. #118
    Join Date
    Feb 2005
    Location
    Limbo
    Posts
    3,706
    Thanks
    0
    Thanked 2 Times in 2 Posts
    EP Points
    10

    Default

    Kohlrak, you're giving terribly wrong advice in some places, so let me elaborate.

    First, the declaration of printf is incorrect, because it uses a variable length argument list. Doesn't matter much, though.

    printf(a); // Prints a 1 on the screen (or should)
    As you yourself have written, the first argument is a pointer to a character constant. 'a' having the value of 1, you'd be printing out garbage data.
    A short explanation of how data is handled in computers.
    You've got a little amount of space in the CPU itself for variable storage, so you won't be able to process larger amounts of data simply by whacking it in the registers. That's why you have... RAM! That little rascal simply does one thing: you tell it where the piece of data you want is located, and it tells you what it is, in return.
    So, usually data you'll be using will be referred to by an 'offset' or 'memory address'.
    In C, this goes the following way: when you declare a variable, (int a), the compiler allocates memory for it, and remembers its memory address. Afterwards, you'll be able to refer to this data in memory simply by the variable name. This is clear so far, at least I hope so. There are a few exceptions to this that you should note, namely arrays. Arrays are stored in the same way, except that the variable name (char array[80]) is the memory address where the data starts. Actually, the array variable isn't really variable, since you can't change its value. However, you can change the data at the memory address its pointing to. This is done by the dereference operator. (the *)
    In short: you'll be working with memory addresses, that are automatically derefered, or in case of bigger data structures, you'll have to handle that as well.

    Arrays in C are simply pointers that have a fixed value, and point to a contiguous area of memory. (The term 'pointer' is equivalent to a variable holding a memory address, in most cases)

    printf("%c", a); // It's an int, but it's set as c so it's like using a cast, it'll be displayed as a char.

    Casting is an entirely different matter. This works this way simple due to how variable length argument lists are handled in C. They supplement NO information on the type of the argument, only its value. So, whatever might stand there, is interpreted as a character, as the format string ("%c") indicates. In C++, there's typeof, and operator overloading, getting around this problem.

    File streams: You've already gone into them, whether you noticed it, or not.
    The thing is, that functions like scanf, printf, and the like use the so called 'standard input/output', AKA stdin and stdout. Guess what, these are handled as files, too. You have functions like fscanf and fprintf, that do the same as their simple cousins, only that they let you specify the file you want to use, and not defaulting to stdin/stdout.

    Sometimes pointers can be unpredictable if you want to use them to store data. never use them to store large amounts of data. Sometimes when i use pointers for dynamic allocation, i find the program crashing... Don't know if that's my fault, or if it's the fact that pointers without referances are unpredictable.


    There is no random factor in programming, get used to it. Programs don't crash without a reason. What I'm getting to is that for basically any kind of program, you'll need to use pointers. For the simple reason that they let you handle data in a fast and efficient way, and also allow for neat data constructs like linked lists or binary trees. If you're going to have a database, you'll be needing them. Also, pointers are the only way to manage large amounts of data. Misuse of them are likely to provoke crashes, though. Proper usage can circumvent most possibly errors, like, including error checking.

    b="333"; // too small, compilation error.
    Compiles correctly.

    Also, I believe that Theo's currently learning C, so including operator overloading would be pretty freaky.

    Morse coding could be most easily implemented with a binary tree, by the way, but an associative array could work as well.
    The former is a dynamic data structure that should be familiar to you (I believe Java already has that premade), the latter is simply the concept of having a 256 element array, each containing a morse string, and ASCII codes would designate the proper string in the array. So that morse['c'] would point to the string that contains the morse code of 'c'.
    Last edited by pkt-zer0; 24th-December-2005 at 20:49.

  14. #119
    Join Date
    Apr 2005
    Location
    Pennsylvania, USA
    Posts
    520
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by pkt-zer0
    Kohlrak, you're giving terribly wrong advice in some places, so let me elaborate.

    First, the declaration of printf is incorrect, because it uses a variable length argument list. Doesn't matter much, though.

    printf(a); // Prints a 1 on the screen (or should)
    As you yourself have written, the first argument is a pointer to a character constant. 'a' having the value of 1, you'd be printing out garbage data.
    A short explanation of how data is handled in computers.
    You've got a little amount of space in the CPU itself for variable storage, so you won't be able to process larger amounts of data simply by whacking it in the registers. That's why you have... RAM! That little rascal simply does one thing: you tell it where the piece of data you want is located, and it tells you what it is, in return.
    So, usually data you'll be using will be referred to by an 'offset' or 'memory address'.
    In C, this goes the following way: when you declare a variable, (int a), the compiler allocates memory for it, and remembers its memory address. Afterwards, you'll be able to refer to this data in memory simply by the variable name. This is clear so far, at least I hope so. There are a few exceptions to this that you should note, namely arrays. Arrays are stored in the same way, except that the variable name (char array[80]) is the memory address where the data starts. Actually, the array variable isn't really variable, since you can't change its value. However, you can change the data at the memory address its pointing to. This is done by the dereference operator. (the *)
    In short: you'll be working with memory addresses, that are automatically derefered, or in case of bigger data structures, you'll have to handle that as well.
    Well yea.. But things crash when i use pointers as variables. It won't auto allocate... But wait... Are you saying i could read byte 0 in the ram by declaring a pointer that equals 1? (please do elaborate on this cause if so this would solve a few problems i've had in the past.)
    Arrays in C are simply pointers that have a fixed value, and point to a contiguous area of memory. (The term 'pointer' is equivalent to a variable holding a memory address, in most cases)

    printf("%c", a); // It's an int, but it's set as c so it's like using a cast, it'll be displayed as a char.

    Casting is an entirely different matter. This works this way simple due to how variable length argument lists are handled in C. They supplement NO information on the type of the argument, only its value. So, whatever might stand there, is interpreted as a character, as the format string ("%c") indicates. In C++, there's typeof, and operator overloading, getting around this problem.
    Keyword: Like, but thanks for pointing that out.

    File streams: You've already gone into them, whether you noticed it, or not.
    The thing is, that functions like scanf, printf, and the like use the so called 'standard input/output', AKA stdin and stdout. Guess what, these are handled as files, too. You have functions like fscanf and fprintf, that do the same as their simple cousins, only that they let you specify the file you want to use, and not defaulting to stdin/stdout.
    But do people really ever reffer to them as file streams? Even though they are... When you use them you save them to the screen's cache (or somthing along those lines) while STDIN saves it to your RAM as an unnamed file but each value has it's own location in ram. (Like the sectors on the hard drive.)

    Sometimes pointers can be unpredictable if you want to use them to store data. never use them to store large amounts of data. Sometimes when i use pointers for dynamic allocation, i find the program crashing... Don't know if that's my fault, or if it's the fact that pointers without referances are unpredictable.


    There is no random factor in programming, get used to it. Programs don't crash without a reason. What I'm getting to is that for basically any kind of program, you'll need to use pointers. For the simple reason that they let you handle data in a fast and efficient way, and also allow for neat data constructs like linked lists or binary trees. If you're going to have a database, you'll be needing them. Also, pointers are the only way to manage large amounts of data. Misuse of them are likely to provoke crashes, though. Proper usage can circumvent most possibly errors, like, including error checking.
    I mean as stand alone variables to store large amounts of data. And there is a random factor, the human. which consists of everything run before and after the program.


    b="333"; // too small, compilation error.
    Compiles correctly.
    Maybe in c... But not C++. Try that one again. lol

    Also, I believe that Theo's currently learning C, so including operator overloading would be pretty freaky.
    Wait, that's theo? Daaaang. lol He keeps changing his name can't tell it's him. Woulda helped to know that before then i woulda revised some of this ahead of time.

    Morse coding could be most easily implemented with a binary tree, by the way, but an associative array could work as well.
    The former is a dynamic data structure that should be familiar to you (I believe Java already has that premade), the latter is simply the concept of having a 256 element array, each containing a morse string, and ASCII codes would designate the proper string in the array. So that morse['c'] would point to the string that contains the morse code of 'c'.
    Meh... Big problem is though, that you can't add more than one char to 1 char. Therefore, you'd have to directly output to the stream you want to use, but for quick revising, you could print it to variables first rather than editing alot of printfs you could edit just one... But... If they want direct stream printing, better do that. lol

    On a lighter (yet more complicated note), how exactly does a processor get the values of a string to compare? Like in C++ you can compare variable "33A" and "33A" and it'd return true while any unequal would return false. Does it just put them all together and read their binary as one big number? If so, then why can't we make an int or somthing big enough to handle that value? (would make quite the amusement project, don't you think? lol) Then each possible string would have it's own value. Then instead of something like "0x6A, 0x66,0x44" it'd be 0x6A6644 which (luckily, hex is close enough to binary to combine the numbers to make somthing like that, the equivalent in decimal would be much uglier to figure out. But it would be "106, 102, 68" verses "6972996". Which, if you sit and do the math, no other string could possibly have that value. lol)

    I autta come up with my own compression aligoritham thinger. lol i have been meaning to do it, and when i do it won't be much of compression. lol And unless you're doing a 1+ gig file or somthing, the compression wouldn't be worth it (accept with text files of course). Probably some one came up with my idea but i'd give me somthing to do. lol

  15. #120
    Join Date
    Feb 2005
    Location
    Limbo
    Posts
    3,706
    Thanks
    0
    Thanked 2 Times in 2 Posts
    EP Points
    10

    Default

    Quote Originally Posted by kohlrak
    Well yea.. But things crash when i use pointers as variables. It won't auto allocate... But wait... Are you saying i could read byte 0 in the ram by declaring a pointer that equals 1? (please do elaborate on this cause if so this would solve a few problems i've had in the past.)
    You couldn't read byte zero in the RAM under Windows that easily, since Windows manages memory. But by dereferencing a zero pointer, you're technically trying to read the program's code you're running, thereby causing an access violation. That's why your programs crash, most likely.
    And why would pointers auto-allocate?


    Quote Originally Posted by kohlrak
    But do people really ever reffer to them as file streams? Even though they are... When you use them you save them to the screen's cache (or somthing along those lines) while STDIN saves it to your RAM as an unnamed file but each value has it's own location in ram. (Like the sectors on the hard drive.)
    Yeah, people do. BTW, I believe direct access to any kind of memory is long gone since DOS, so they're absolutely no different from a file, except that they do not get saved to hard disk, but somewhere else. Due to this naming being a bit confusing, under C++ you have IOStream classes, which can just as convieniently be used for reading files or stdin.
    Anyhow, if you want to see how much of files they are, you can indeed reassign stdin and stdout, thereby making a program read 'keyboard input' from a file, and outputting it into another file.

    Quote Originally Posted by kohlrak
    I mean as stand alone variables to store large amounts of data. And there is a random factor, the human. which consists of everything run before and after the program.
    Yes, but that doesn't change what the program itself does.

    Quote Originally Posted by kohlrak
    Maybe in c... But not C++. Try that one again. lol
    What kind of compiler are you using? Works perfectly, compiled with g++. Static variables are zeroed out anyway, so you could just set its first three elements to '3', not bothering to include a terminating zero, and it would still work.

    Quote Originally Posted by kohlrak
    Meh... Big problem is though, that you can't add more than one char to 1 char. Therefore, you'd have to directly output to the stream you want to use, but for quick revising, you could print it to variables first rather than editing alot of printfs you could edit just one... But... If they want direct stream printing, better do that. lol
    Download Links:
    Links are hidden from guests. Please register to be able to view these links. First part reads a sample file, associating the characters with morse codes. Then it displays those codes appropriate for each keypress. No error checking, memory cleanup, and that sort of stuff, but you get the idea. And yes, kohlrak, it uses... pointers!

    Quote Originally Posted by kohlrak
    On a lighter (yet more complicated note), how exactly does a processor get the values of a string to compare? Like in C++ you can compare variable "33A" and "33A" and it'd return true while any unequal would return false. Does it just put them all together and read their binary as one big number? If so, then why can't we make an int or somthing big enough to handle that value? (would make quite the amusement project, don't you think? lol) Then each possible string would have it's own value. Then instead of something like "0x6A, 0x66,0x44" it'd be 0x6A6644 which (luckily, hex is close enough to binary to combine the numbers to make somthing like that, the equivalent in decimal would be much uglier to figure out. But it would be "106, 102, 68" verses "6972996". Which, if you sit and do the math, no other string could possibly have that value. lol)
    It depends on how it's implemented in the standard library. Most common would be to go character-by-character on the two strings, calculate the difference of the characters ASCII codes, and if it's not zero, return it, otherwise continue comparing.
    The reason you can't put a string in a single variable to handle it is that C uses zero terminated strings, unlike Pascal, where the first byte of a string was its length. You can have arbitarily long strings in C (as in: possibly a hundred gazillion letters long), but obviously you can't have arbitarily large variables (int, long int, and the like have an implementation defined size)

Similar Threads

  1. Visit The Hang Out!
    By Georgyw5k in forum Free 4 All
    Replies: 5
    Last Post: 28th-November-2003, 05:18
  2. Do You Hang Up On Advertisement Callers?
    By ((KvN)) in forum Free 4 All
    Replies: 52
    Last Post: 21st-July-2003, 11:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About Us

We are the oldest retro gaming forum on the internet. The goal of our community is the complete preservation of all retro video games. Started in 2001 as EmuParadise Forums, our community has grown over the past 18 years into one of the biggest gaming platforms on the internet.

Social