jephthah 1,888 Posting Maven

okay, i'm a new user who just logged in to make my very first post ever, because i found some old thread on google, and i'm prepared to dazzle the world with my "solution" on how to best "gets()" input from the terminal.

i'm already ignoring the bold text on the message input that says the same thing. why would a popup be any different?

just lock the stale threads down. random posts 3 years after the fact don't contribute anything, they just make this site look like its full of retards to anyone who finds it afterwards.

jephthah 1,888 Posting Maven

think ardav meant that other people's references to the old username will remain within the body of the text.

like i change my name to "joe schmoe" and all my old posts are now listed as being "joe schmoe" but other people's posts will still be addressing me as "jephthah".

it will cause consternation when internet archaeologists of the 22nd century attempt to reconstruct our "original intent"

there will probably be a schism between scholars over the Daniweb Apocrypha

WaltP commented: Good description... +0
jephthah 1,888 Posting Maven

for one thing you're getting characters into 'c' and parsing results based on something called 'answer'

jephthah 1,888 Posting Maven

most other sites have the courtesy to close their stale threads. they leave them available for archive purposes, but not allow them to become magnets for spammers, trolls, and other assorted retards.

allowing flotsam and jetsam from the internets to collect around old posts only serves to make the Daniweb site look incompetent . Passers-by from google links see our popular "Solved" threads with ridiculous "answers" and/or insipid commentary as final posts, sometimes pages worth.

if that's the kind of image you want to project, that of "inmates running the asylum", then keeping the threads open forever is the way to go.

jephthah 1,888 Posting Maven

for (;;) just means "forever". typically used for a loop that will execute an indeterminate number of times, exiting with a break; command once the exit condition is found.

the corresponding while would be while(1) or while(TRUE) ... doesnt make any sense to use '\n', even though technically '\n' is non-zero and it will work. it just looks really weird and implies that the programmer doesn't know what they're doing.

the second for loop is a bad example. this will be a source of problems. i can't just convert this to a while loop, because the while loop will then be faulty.

think about what that for loop is doing.... what will it execute if the DEBUG is defined? what will the for loop execute if the DEBUG is not defined? see the problem?

in any event, you'll want to get the character 'c' first, then evaluate it if it is '\n' or EOF to decide whether to conditionally execute the loop or exit.. how you structure your loop may also depend on what you're doing with 'c'

.

jephthah 1,888 Posting Maven

You could be right there. Replace Google search with DaniWeb search in that equation. Maybe this is something Dani could take a look at when the current site update stuff is out of the way.

or maybe she could "take a look" at auto-locking old threads, that can only be re-opened upon legitimate request to a moderator.

would be easy to implement, will quit appending bullshit answers to solved threads, quit wasting moderators time locking and deleting the swarms of one-off "me toos" and "gimmetehcodez" posters.

it would also save the rest of us from crafting detailed responses to the OP, thinking it's a current thread, only to find out afterwards that the damn thing is five years old. i don't know how many times i've done that, it's really aggravating.

diafol commented: here here +0
Ancient Dragon commented: Agree +0
jephthah 1,888 Posting Maven

copying byte by bye using loop...uh...sounds like a complected process...:|

uh... not so much :)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   FILE *fp_source, *fp_dest;
   char oneByte;

   /* WARNING:  this does ZERO error checking!! */

   fp_source = fopen(argv[1], "rb"));
   fp_dest   = fopen(argv[2], "wb"));

   while(!feof(fp_source)) {
      oneByte = fgetc(fp_source);
      fputc(oneByte, fp_dest);
   }

   fclose(fp_source);
   fclose(fp_dest);

   return 0;
}

.

jephthah 1,888 Posting Maven

you can copy any file, whatever the type or extension.

just open the source file as a binary file and use a while loop to copy each byte one at a time to the destination, until the EOF charater is found.

see fopen(), fgetc(), fputc(), and feof() in the standard C library <stdio.h>. that's all the functions you'll need, though you'll probably want to include error checking with ferror().


.

jephthah 1,888 Posting Maven

in the ... link they are saying, user should definitely NOT [use fflush(stdout) to clear the output buffer.]

that webpage is a joke. a bad joke. they actually instruct you to use gets() as an example of "safe code". this right there tells me they're talking out their ass.

for example here jephthah gave a solution where he used fflush after printf and mentioned // printf did not have newline, need to flush

if you don't print a newline, but want the line to display to the terminal, you should use fflush(stdout);. Your system may not need this, but some systems will not print text to the output buffer until a newline is found or the buffer is otherwise flushed.

Narue has explained it better than i can. i'm only commenting here because my name was dropped :icon_wink:

jephthah 1,888 Posting Maven

"isPrime" is just a flag that gets set or cleared to indicate whether or not the current number being tested is prime.

if isPrime = 0, then it is not prime.
if isPrime = 1, then it is prime.

here is the process by which it works:

the outer for loop for (number=lower;number<=upper;number++) tests each number within the range desired. before we start testing each number, we set isPrime = 1; which is to say that we will assume each number is prime until we later find otherwise.

so then we go into the second (inner) loop and test each of the appropriate divisors against the number. IF the number is found to be divisible by any one of the divisors, the number is "not prime" and so we clear isPrime = 0 and break; out of the inner loop -- there's no point in testing any more divisors, since the number is not prime.

once we're out of the second (inner) loop -- whether it's because we (a) found a divisor that evenly divided the number and broke out early, or (b) because we checked all the divisors and none of them evenly divided the number -- the determination of the number being prime or not will be reflected by the value of "isPrime".... if "isPrime" is 1, then the number is prime. if "isPrime" is 0, then the number is not prime.

we then index the outer loop to the next …

jephthah 1,888 Posting Maven

look i'm just going to give this to you, because it's obvious you're trying, and you really need to get some better coding practices.

study how i've modified your code, noting a few key points:

-- the second (inner) for loop should not be testing divisors across the entire range, but only up to a number less than the number being divided. in fact, this "maximum divisor" should not be more than half the number being tested, because it's meaningless to test divisors larger than half of the number being tested, and will just add unnecessary computations.

-- you need to test *each* of the appropriate divisors, and *none* of those divisors should be evenly divisible into the number in order for that number to be considered prime.

-- name your variables with meaningful names

-- comment your code heavily, explaining each fundamental block.

-- quit using <conio> functions like "clrscr" and "getche"

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int lower, upper, number, divisor, maxDivisor, remainder, isPrime;

   printf("\nEnter Lower Number of Range: ");
   fflush(stdout);  // in case lack of newline blocks buffer
   scanf ("%d",&lower);

   printf("Enter Upper Number of Range: ");
   fflush(stdout);  // in case lack of newline blocks buffer
   scanf ("%d",&upper);

   printf("\nPrimes Numbers in this Range are:\n\n");

   // cycle through each number within the range of values requested

   for (number = lower ; number <= upper; number++)
   {
      isPrime = 1;  // assume each is a prime unless found otherwise

      // for …
Xufyan commented: ummaah... +0
jephthah 1,888 Posting Maven

it wasnt working to begin with. try to run your original code with the same range.

AD just fixed some of the blatant loop error to keep it from repeat printing. the problem is your choice of variables in which to base your loops, and the fact that you print the number as a prime on the first instance of it not being divisible by some number. just because 9 is not divisible by 2, doesnt mean that it's prime. obviously, it's divisible by 3. yet, as soon as it's found to not be divisible by 2, you print it as being prime.

your problem is also the way you name your variables. a, b, c, d, e, f... could you be any more cryptic? this is bad practice that lends itself to unintelligible code that is difficult to debug. and it will be full of bugs, because the programmer himself cant understand what is going on.

.

jephthah 1,888 Posting Maven

this doesnt work. try to find primes from 2 to 30:

2
30
3 5 7 9 11 13 15 17 19 21 23 25 27 29

Process returned 0 (0x0)   execution time : 5.015 s
Press any key to continue.
jephthah 1,888 Posting Maven

because we sure do need all these new users posting half-assed, me too, or completely wrong "solutions" to multi-year-old threads that had already long since been resolved.

like this
and this
and this
and this
and this
and this

I could go on, because that's just a sample from the past 10 days or so in C. i havent even started on other forums.

Salem commented: Careful, you might trigger the non-existant link-spam detection s/w when you post too many links ;) +0
jephthah 1,888 Posting Maven

So database management would be suitable? How about unix? what do they do there? and isn't unix used for managing too? Is database management a good edge on job applications?

oh, yes. absolutely.

and when i speak of managment, i mean you should consider getting an MBA.

tell your potential employers your 5-year goal is to start an MBA program.

you'll do just fine.

jephthah 1,888 Posting Maven

change printf to fprintf and write to a .csv file

FILE * fp = fopen("mydata.csv", "w");  // opens file to write ("w"), using the FILE pointer "fp"

//...

printf("16 Bit:\n\n");
  for (n=0;n<numberOfValues;n++){
    fprintf(fp, "%.4x\n", rand16());   // prints to the file, using the file pointer "fp"

//...

fclose(fp);  // close the file after you're done

}

oggiemc commented: straight to the point once again, very helpful poster +1
jephthah 1,888 Posting Maven

The only way that I am able to do well in programming is because I am sort of a hard worker. ... I like routines and do menial tasks very well

sounds like a career in databases is for you.

I am really not that smart.

then it will be best if you get on a management track as soon as possible.

jephthah 1,888 Posting Maven

Here is a good example of why threads should NOT be closed

there's nothing redeeming about that thread. at this point, the only thing that can be added are warnings and disclaimers.

if we think such an effort is a good exercise with instructive value, then there's a lot of horrible code snippets out there that still need to be decisively shot and buried.


.

jephthah 1,888 Posting Maven

using the libcurl library is independent of whether or not you use the MinGW compiler. it's a library, you compile it to be used on your system. the library is then invoked to build an executable that can be distributed to any machine running a similar OS.

you should focus on getting libcurl working. i believe there was a recent thread about how to use the libcurl library to do what you're asking.

jephthah 1,888 Posting Maven

yeah, that's most people's reaction. even my wife.

jephthah 1,888 Posting Maven

although in retrospect, it's also probably better if i dont post after drinking.

Nick Evan commented: I agree :) +0
jephthah 1,888 Posting Maven

oops, i had a brainfart at the last minute and edited the code wrong. disregard the code i gave in #4, above. you had the basic form right the first time.

using your style (the style traditionally seen in many texts) it would look like:

x = malloc (T * sizeof(float*));
      for (i=0; i < T; i++)
         x[i] = malloc (S * sizeof(float));

using Salem's style it would look like:

x = malloc (T * sizeof(*x));
      for (i=0; i < T; i++)
         x[i] = malloc (S * sizeof(**x));

.

jephthah 1,888 Posting Maven

(1) means to not cast the return value of malloc. Simply write x=malloc (T*sizeof(float*)); instead of x=(float**) malloc (T*sizeof(float*)); . the cast is not required by the C standard and is redundant. this is at best "bad practice" and at worst can hide a serious bug if you didn't include the <stdlib.h> library.

(3) always include the <stdlib.h> library, otherwise malloc will assume the return of a type int regardless of your attempt to cast its return value. This can completely destroy the stack in some cases. It may not be the case for you, but is still in the "very bad practice" area.


(2) if you want to use the same code for C++ as for C, you invite more complexity that will require conditional compilation statements using #ifdef / #elsif / #endif methods.

Furthermore, the C language can certainly handle complex variables and perform FFT calculations, so this is not a reason for using C++. The <complex.h> library is standard to C99. You should be using this standard if you don't have a compelling reason not to. If you are forced to use C89, complex math can be handled using a third-party library.

(4) this is a style point, and diverges from the original problem. since you've already typed "x" as a type float**, Salem suggests you use sizeof(*x) instead of sizeof(float*) , and sizeof(**x) instead of sizeof(float) .

but a problem is you're not fully allocating your memory …

dr_michali commented: very helpful, thanks! +0
jephthah 1,888 Posting Maven

Haha.

this guy, "cool_jatish", wanted his name changed probably because someone had previously caught him and called him out for being a Plagiarizing Loser.

i caught him and called him out just now begging for rep for his plagiarized posts in C forum.

so now he's either using his alias or got his buddy "vigasdeep" to downvote all of my posts.

LOL

what a loser.


.

jephthah 1,888 Posting Maven

jesus christ already, i was being sarcastic.

cant you people read my facial expression?

:icon_frown: :P

jephthah 1,888 Posting Maven

I've never used libcurl. I'd love to help you, but all i can do is read the libcurl example that does exactly what you're looking for, and repeat what it says, but that would be kind of dumb and a waste of everyone's time, since you can just go read it yourself.

Aia commented: Best help ever. Just do it yourself! +8
jephthah 1,888 Posting Maven

crikey, eh?

jephthah 1,888 Posting Maven

and yet you bumped an 11-day dead thread to say that.

now see, this is why we can't have nice things. :icon_frown:

jephthah 1,888 Posting Maven

if he'd bother to pay attention in the first thread he made on this, he'd understand this already.

jephthah 1,888 Posting Maven

Xufan your codes don't work because they are wrong. Narue has given you code that works.

now do the following:

(1) read Narues post above and fully understand the second example with the if/else statements.

(2) take the first statement with teh ? : operators, and insert parentheses around them so that:
(a) the order of operations remain the same, and
(b) it is easier for you to follow the logic

you should get no more help here than this. we're not here to type your homework up and submit it for you.

.

jephthah 1,888 Posting Maven

it's not correct to test the return value of fscanf as a NULL character. fscanf returns an int. specifically:

Upon successful completion, these functions return the number of successfully matched and assigned input items; this number can be 0 in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF is returned. If a read error occurs the error indicator for the stream is set, EOF is returned, and errno is set to indicate the error.

--opengroup.org

and EOF is a macro that is represented as a negative value.

so change your line to

while (fscanf(data, "%c%d\n",&let, &prior) > 0)

this will process as long as characters are read.

jephthah 1,888 Posting Maven

doesn't have time to teach about <stdlib.h>? yet, will teach about <conio.h>?? i pity your class.

anyhow, what i don't have time for is looking at code that's been posted without code tags

jephthah 1,888 Posting Maven

point.

speaking of "classic errors" :$

.

jephthah 1,888 Posting Maven

point. so i mean, does it do it print that end message twice, from the parent and the child? or just once? (or none?)

i don't have the answer, i'm just trying to narrow down possibilities.

jephthah 1,888 Posting Maven

I have a sneaky suspicion that, after a posting or two, everyone would know it was you ;-)

aw, that's the nicest thing anyone's ever said!

:$

jephthah 1,888 Posting Maven

does it print "program done" and return 0?

jephthah 1,888 Posting Maven

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/index.html
http://www.cplusplus.com/reference/clibrary/


as you can see, atof() is part of the <stdlib.h>.

in C++ this library is called <cstdlib>


This is easily found with a minimum amount googling. its not that we dont want to help, but there's a sort of a minumum level of competence assumed that people can do basic things on their own.


.

jephthah 1,888 Posting Maven

ok i'll use another way to program it even if he did not discuss it like the stdlib.h

<stdio.h> which you already use, is the STanDard Input and Output library header. it's a "standard", you see....

<stdlib.h> is the STanDard LIBrary. it's also "standard". Its the default library for darn near every C program in the world. So don't be scared. the fact that your professor is withholding it from you like it's some big secret, is even more evidence suggesting his incompetence.

<conio.h> is CONsole Input and Output. It's not standard, it's obsolete. you avoid it like the plague. or don't, and get the plague. doesn't matter to me, i won't ever have to work with you.

is there any site u recommend to download Turbo C that is safe, without any virus? please.
thanks. God bless the wise!

no, i refuse to do so on the grounds that it will violate my principles. and god forbid you go find it yourself.

jephthah 1,888 Posting Maven

unix/linux represents a newline character ('\n') as 0x0A, called a LF (line feed) and windows represents a newline character ('\n') as 0x0D 0x0A called a CR LF (carriage return line feed). your professor's file is the unix case.

if, when writing programs in unix, you want your newlines to look like windows newlines, you can write them as '\r\n' to give the CR LF. be careful running this on windows, becasue it will give you CR CR LF (0x0D 0x0D 0x0A)..... and let's not even get started on Macs.

this whole incompatibility issue is historic, and there's a huge explanation about the whys and wherefores if you care to read about it. most text editors can deal with the various formats. Windows Notepad can not. So just be aware of this fact. I avoid using Notepad.

be advised, this whole issue can be a pain in the ass if you let it. just understand and accept the way it is, and don't stress about it.


.

jephthah 1,888 Posting Maven

Second, what teacher is going to go to a forum to hear random people whine and flame?

because.... they have so much free time on their hands, since they're not researching new teaching methods or current industry trends?

eh, yeah ... okay ... i got nothing.

jephthah 1,888 Posting Maven

this is a classic error. the macro just replaces the code in the program with your #define'd value verbatim. therefore, it's calculating according to the order of operation: 225 / 15 * 15 , which is 225.

change your #define to

#define SQR(x)     (x * x)

.

jephthah 1,888 Posting Maven

We used Turbo C in our programming and we need to do our project using Turbo C only.

That's a lie. Your instructor is an incompetent dolt. If he says otherwise, send him here.

Do everyone a favor, especially yourself, and use a modern development environment such as code::blocks with the MinGW compiler.

anything you build using a modern compiler that complies with the C standard (such as the link above) , will work on your instructor's retarded choice of a 20-year-old dried turd of a compiler. the reverse is NOT true, however, so don't waste your time building garbage on a decrepit toy.

do it right.


.

Aia commented: For a neverending anti-turbo-c campaign. +8
jonsca commented: "Beautiful" imagery +3
jephthah 1,888 Posting Maven

there's no reason why you should be afraid of <stdlib.h>. it won't bite you. you should not use <conio.h> that library has been obsolete for 20 years. most modern compilers do not use it.

as to "how to randomize", here's the simple answer: if you want a random number from 1 to 'n',

int number;
number = rand() % n + 1;

there some are problems with this method, but you don't need to worry about them for now.

one thing, this will generate the same sequence of "random" numbers each time you run the program. if you want a different sequence of random numbers each time the program is run, you'll need to "seed" the number generator, using "srand"

jephthah 1,888 Posting Maven

wow, i could change my name and my picture and i would be like a completely new person that no one would know who i am!

jephthah 1,888 Posting Maven

can i change my name, too?

jephthah 1,888 Posting Maven

please just one more time help me

naw... you'll be back :)

jephthah 1,888 Posting Maven

because now it's a pointer so you need to dereference it when you want to make an assignment to it. line 3 becomes:

if(( *TempFile = fopen(FileName, Mode) ) == NULL)
jephthah 1,888 Posting Maven

FILE * is a pointer. if you want to pass it by reference, make it a pointer to a pointer, FILE **

jephthah 1,888 Posting Maven

when you declare a prototype, like in line 4, you declare the function type, function name, and argument types, like you did: void printtable(int x); , this must match the definition of the function, which you have down on line 46.

but when you *call* the function in the program, like at line 9, do not include the function type or argument types, so you just call it like so: printtable(10); .... where the number "10" (for example) could be any "int" value and will be passed to the function.

when you get to the function definition, starting at line 46:

void printtable(int x)          /* Function definition */
{
    for (x=0; x<10; x++) 
    {
        printf("%d",x);
        printf("n");
    }
}

you see that whatever value passed in from the function call (line 9) will come into this function via the argument as "int x"... the purpose of this would be to have a value that could then be used for whatever purpose you intended within the function.

HOWEVER, what you are doing is not really correct, or at least is not what is intended. once you get to the function, you reassign "x=0" in your FOR loop, thereby losing whatever value was passed in.

it seems to me that you really intend to have "x" be the upper limit of your for loop, so that it counts out "x" numbers. change your printtable function to something like this:

void printtable(int x)          /* Function definition */
{ …
jephthah 1,888 Posting Maven

I'm not convinced that's correct. i'm not even convinced the indentions that have appeared since Walt added the code-tags are correct. they're pretty much a mess too.

i'm half suspecting the print() and printtable() are really the same function that was just misnamed in one spot.

we won't really know anything until the OP returns to clean up his code.