2,384 Posted Topics
How might I write an implementation in C of the standard library function [inlinecode]strcpy[/inlinecode]? Here's how I might. | |
How might I write an implementation in C of the standard library function [inlinecode]strstr[/inlinecode]? Here's how I might. [url=http://www.daniweb.com/code/snippet313.html]What if I need it to be case-[b]in[/b]sensitive[/url]? | |
How might I write an implementation in C a case-insensitive version of the standard library function [inlinecode]strstr[/inlinecode]? Here's how I might. [url=http://www.daniweb.com/code/showsnippet.php?codeid=314]But I need it to be case-sensitive[/url]? | |
How might I write an implementation in C of the standard library function [inlinecode]strstr[/inlinecode]? Here's how I might. | |
Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than [inlinecode]gets(text)[/inlinecode] or [inlinecode]scanf("%s", text)[/inlinecode], but not bulletproof. It is meant to be a simple but relatively safe demonstration. The function [inlinecode]mygetline[/inlinecode] reads user input from the [inlinecode]stdin[/inlinecode] into a string … | |
Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than [inlinecode]scanf("%d", &n)[/inlinecode], but not bulletproof. It is meant to be a simple but relatively safe demonstration. The function [inlinecode]mygeti[/inlinecode] reads user input from the stdin into a string using [inlinecode]fgets[/inlinecode]. … | |
This snippet shows how to use the functions [inlinecode]time[/inlinecode], [inlinecode]localtime[/inlinecode], and [inlinecode]strftimetime[/inlinecode] to pick off only the time parts for display. | |
Some issues with date/time calculations. [Perhaps I ought to edit this one day and better describe it.] | |
To check for integer overflow, you can't simply check the [i]outcome[/i] of an operation, you need to check [i]before[/i] the operation. So how do you do that? This example checks for overflow with multiplication. | |
This is yet another example of reversing a string. This version modifies the original, reversing the string in place. [I]See also [url=http://www.daniweb.com/code/snippet522.html]Strings: Reversal, Part 2[/url][/I]. | |
This problem is a typical homework question. The code presented here may contain some elements that may be a little "ahead of the game" in coursework, but it presents some of the basic elements that might be used in such an exercise. | |
In [url=http://www.daniweb.com/code/snippet149.html]Part 2[/url], you may have noticed that the bits representing the [i]value[/i] of an [font=courier new]unsigned int[/font] did not appear to match the bits representing the [i]object[/i]. This is because of the [i][url=http://en.wikipedia.org/wiki/Endianness]endianness[/url][/i] of my implementation. When it comes to displaying the bits of objects that take more space … | |
In [url=http://www.daniweb.com/code/snippet148.html]Part 1[/url], I chose [font=courier new]unsigned int[/font] as the type to use for displaying the bits of a value. The same general procedure can be done with other types as well. The biggest issue really is the expression which generates the most significant bit, for which there are a … | |
It is often instructive to display the bits of a value. (This is similar to 'converting' an integer to binary.) The way I have done this in this snippet is to start at the most significant bit and work your way through all the rest of the bits. (Continued in … | |
This is a very common task for a beginner programmer. Unfortunately many beginners do not seem to understand the question. First, know that binary, decimal, octal, hexadecimal, and others are simply [i]representations[/i] of values. The value of 123 (decimal) still has the same [i]value[/i] if it represented in hexadecimal (7B), … | |
Re: Not getting an error for the undefined _CARTERR should indicate that WIN32 is not defined. So CARTERR is never filled with a string. So you try to open a file given by an undefined filename. This likely fails, but you don't check for success. So you probably try to fprintf … | |
Re: Standard C and C++ have [icode]isalnum[/icode]. [edit]Sometimes the is* functions are implemented as lookup tables. Consider that type of approach (lookup table) yourself instead of the inner loop if speed is paramount. [edit=2]Geez I need a nap: [QUOTE=VernonDozier;964423]cctype comes in handy here. [url]http://www.cplusplus.com/reference/clibrary/cctype/[/url] Everything is done by single characters, but … | |
Re: A 2D array of chars is a 1D array of strings, so to speak. So if you want a 2D array of modifiable strings, you'll need a 3D array of chars. [code]char a[][3][6] = {{"item1", "1", "1"}, {"item2", "1", "2"}};[/code] | |
Re: [url]http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12[/url] and subsequent FAQs? | |
Re: [QUOTE=bynaridoom;963525]I guess what I need to know first, is how to read each line in sequence.[/QUOTE] [code=c++] std::ifstream file("file.txt"); std::string line; while ( getline(file,line) ) { // parse line }[/code] | |
Re: [code]namespace [B][COLOR="Red"]N[/COLOR][/B]yNamespace[/code] [code]namespace MyNamespace[/code] | |
Re: One way: [code]/** Comma separated, for example. */ void record_write(FILE *file, const clientData *cd) { fprintf(file, "%s,", cd->firstName); fprintf(file, "%s,", cd->surname); fprintf(file, "%d,", cd->age); fprintf(file, "%f,", cd->height); fprintf(file, "%f,", cd->weight); fprintf(file, "%f,", cd->bmi); fprintf(file, "%f,", cd->bfpercentage); fprintf(file, "%c,", cd->significantInjury); fprintf(file, "%c,", cd->unableToExercise); fprintf(file, "%d,", cd->seekMedicalApproval); fprintf(file, "%d,", cd->hadDrApproval); fprintf(file, "%d,", … | |
Re: [QUOTE=dumbncool;961177]Hi, CAn anyone tell me what does the following statement do in C. [icode]*p++ = val[/icode][/QUOTE][ICODE]*p[/ICODE] is assigned the value of [ICODE]val[/ICODE] and then [ICODE]p[/ICODE] is incremented. [QUOTE=dumbncool;961177]My understanding is that because ++ and * have right-associativity[/QUOTE]Associativity? I don't even think you got that right if you were trying to … | |
Re: [code]float pur[53][25]; // ... pur[count] = x; // error occurs here[/code] If pur[count] is an array of 25 floats, and x isn't, and you get an error, isn't that enough of a hint? You seem to know how to work it here: [code]pur[count][count2]= x;[/code] | |
Re: You return a variable that ceases to exist after the function all (a variable local to the called function). Generally speaking, pass a pointer (to the start of the array) and then modify it in the called function. | |
Re: I'd just go with a few standard functions. Then the math of subtracting years is pretty simple. [code]#include <iostream> #include <ctime> using namespace std; void foo(int age) { time_t now = time(0); if ( now != (time_t)-1 ) { struct tm *local = localtime(&now); if ( local ) { [COLOR="Red"]local->tm_year … | |
Re: I don't ask too many questions, in part because I try to RTFM. Often my first place to search is comp.lang.c or comp.lang.c++: [URL="http://groups.google.com/groups?q=qsort group%3Acomp.lang.c"]for example[/URL]; sometimes I even add an author to the search: [URL="http://groups.google.com/groups?q=qsort group%3Acomp.lang.c author%3Atorek"]Torek[/URL] is a favorite. I also check out the man pages for particular … | |
Re: Use a forward slash; #include necessary headers. | |
Re: [QUOTE=acidnynex;958311]I'm having a ton of trouble reading the text from a file into an integer array, the file contains 3 lines of 1,000 integers from 0 to 1.[/QUOTE] I don't suppose you could attach the input file? And what do you mean by "integers from 0 to 1"? Just a … | |
Re: Might you be doing the old 'OR when I mean AND' thing? [code]si < title.end() || title_len == 0[/code] I haven't looked very closely, but...? [code]si != title.end() && title_len > 0[/code] | |
Re: If you want to change the value of a pointer in a called function, pass a pointer to a pointer. [code]#include <stdio.h> #include <stdlib.h> #define MAXLINE 100 void foo(char **p,int x, int y) { p = malloc(x * sizeof(*p)); } void bar(char ***p, int x, int y) { *p = … | |
Re: [code=c++]#include <iostream> #include <string> int main() { std::string text("This string has \"embedded quotes\" in it."); std::cout << text << "\n"; } /* my output This string has "embedded quotes" in it. */[/code] | |
Re: *sigh* Post actual compilable code that demonstrates the problem. [edit]If you want to change [icode]p[/icode] in a called function, pass a pointer to it; i.e. a pointer to pointer to struct. Or else return a pointer from the function rather than returning void. | |
Re: [code]double avg_file(ifstream& source_file) { double number_in_file; double total = 0; int count = 0; [COLOR="Red"]while (source_file >> number_in_file)[/COLOR] { total = number_in_file + total; count ++; } //average of numbers in file return (count); }[/code] [url]http://www.daniweb.com/forums/post155265.html#post155265[/url] | |
Re: Code for playing along at home? [url]http://www.daniweb.com/forums/post954890-8.html[/url] | |
Re: [QUOTE=D.JOHN;950788][code]int student,subject; student=5; subject=3; int marks[student][subject][/code] If the it is not possible, can your please mind tell me the reason?[/QUOTE]Your compiler might try to explain it like mine does: [quote]error: ISO C++ forbids variable-size array `marks'[/quote] | |
Re: Or something like this? [code=c++]#include <iostream> #include <string> int main() { std::string text("foobarbaz"), sub("bar"); std::cout << "text = " << text << "\n"; std::string::size_type loc = text.find(sub); if ( loc != std::string::npos ) { text.erase(loc, sub.length()); std::cout << "text = " << text << "\n"; } } /* my output … | |
Re: Another possibility? [code=c++]#include <iostream> #include <fstream> int file_isempty(const char *filename) { std::ifstream file(filename); return !file || file.get() == EOF; } int main() { const char filename[] = "file.txt"; std::cout << filename << (file_isempty(filename) ? " " : " non") << "empty\n"; }[/code] | |
Re: Contrary to popular belief, moderator duties really have little to do with being knowledgeable about a particular subject. Moderating has more to do being online and keeping threads in order: [LIST] [*]Deleting spam. [*]Splitting threads that have been replied to after being dead for months or years. [*]Combining duplicate threads, … | |
Re: Watch your indentation. [code]//Specification: Count the letter is a Text file #include <iostream> #include <fstream> #include <string> using namespace std; const char FileName[] = "c:\\TestCount.txt"; int main() { string lineBuffer; ifstream inMyStream (FileName); //open my file stream [COLOR="Red"]if ( inMyStream.is_open() ) {[/COLOR] //create an array to hold the letter counts … | |
![]() | Re: [URL="http://www.daniweb.com/forums/showthread.php?p=155265#post155265"]Avoid Loop Control Using eof[/URL] |
Re: Is the context for the writing of the blank line in the code you didn't post? | |
Re: [QUOTE=dgr231;954919]So, apparently, this works if I change the (1/2) to .5. Is there any reason why this should make it work?[/QUOTE] To the compiler, [ICODE]1[/ICODE] and [ICODE]2[/ICODE] look like ints. Integer division truncates, so [ICODE]1/2[/ICODE] is zero. Try [ICODE]1.0/2[/ICODE], or [ICODE]1/2.0[/ICODE]. | |
Re: I don't know that you've posted enough code to debug. How about a compilable snippet that demonstrates the problem? | |
Re: [url]http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5[/url] | |
Re: [URL="http://www.daniweb.com/tutorials/tutorial45806.html"]My preference[/URL] is to read a line of input with [ICODE]fgets[/ICODE] and then use [ICODE][COLOR="Red"]s[/COLOR]scanf[/ICODE] to obtain numeric data. It's very much like what you're doing, with fewer headaches. Then maybe after you've got things going good that way take a pass at redoing it with [ICODE]scanf[/ICODE]? | |
Re: [QUOTE=KungFuTze;953221]Hello guys I have a problem with my code: I want to print an error message every time the user enters a number lower than -273.15 . My code runs well and does the desired mathematical operation. It even loops back to the input message, but I'm having huge headaches … | |
Re: [url]http://cboard.cprogramming.com/c-programming/37784-how-handle-integer-overflow-c.html#post265113[/url] ? [edit]So...? [code] if ( a > LONG_LONG_MAX - b ) { puts("overflow"); } else { a += b; }[/code] |
The End.