2,384 Posted Topics

Member Avatar for Dave Sinkula

How might I write an implementation in C of the standard library function [inlinecode]strcpy[/inlinecode]? Here's how I might.

0
192
Member Avatar for Dave Sinkula

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]?

0
164
Member Avatar for Dave Sinkula

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]?

Member Avatar for c_coder
0
2K
Member Avatar for Dave Sinkula

How might I write an implementation in C of the standard library function [inlinecode]strstr[/inlinecode]? Here's how I might.

0
133
Member Avatar for Dave Sinkula

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 …

Member Avatar for kedhar
0
327
Member Avatar for Dave Sinkula

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]. …

0
1K
Member Avatar for Dave Sinkula

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.

0
130
Member Avatar for Dave Sinkula

Some issues with date/time calculations. [Perhaps I ought to edit this one day and better describe it.]

0
132
Member Avatar for Dave Sinkula

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.

0
357
Member Avatar for Dave Sinkula

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].

0
615
Member Avatar for Dave Sinkula

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.

Member Avatar for Sealteam56
0
246
Member Avatar for Dave Sinkula

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 …

0
146
Member Avatar for Dave Sinkula

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 …

0
405
Member Avatar for Dave Sinkula

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 …

0
378
Member Avatar for Dave Sinkula

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), …

Member Avatar for al3x748769
0
542
Member Avatar for selsium

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 …

Member Avatar for Dave Sinkula
-1
136
Member Avatar for Nathan Campos
Member Avatar for Jennifer84

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 …

Member Avatar for Dave Sinkula
0
3K
Member Avatar for MaestroRage

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]

Member Avatar for MaestroRage
0
97
Member Avatar for Cosa

[url]http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12[/url] and subsequent FAQs?

Member Avatar for Dave Sinkula
0
119
Member Avatar for bynaridoom

[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]

Member Avatar for bynaridoom
0
101
Member Avatar for daviddoria

[code]namespace [B][COLOR="Red"]N[/COLOR][/B]yNamespace[/code] [code]namespace MyNamespace[/code]

Member Avatar for daviddoria
0
232
Member Avatar for nateuni

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,", …

Member Avatar for nateuni
0
367
Member Avatar for dumbncool

[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 …

Member Avatar for Aia
0
150
Member Avatar for gretty

[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]

Member Avatar for mrnutty
0
123
Member Avatar for chathu12

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.

Member Avatar for yellowSnow
0
103
Member Avatar for DavidDan

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 …

Member Avatar for tux4life
1
226
Member Avatar for Dsiembab

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 …

Member Avatar for GrimJack
0
199
Member Avatar for chathu12
Member Avatar for MaestroRage
Member Avatar for acidnynex

[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 …

Member Avatar for acidnynex
0
102
Member Avatar for Democles

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]

Member Avatar for Dave Sinkula
0
120
Member Avatar for aflneto

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 = …

Member Avatar for aflneto
0
69
Member Avatar for Lukezzz

[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]

Member Avatar for Lukezzz
0
98
Member Avatar for checkforc

*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.

Member Avatar for Dave Sinkula
0
277
Member Avatar for esesili

[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]

Member Avatar for mrnutty
0
131
Member Avatar for Nogat21

Code for playing along at home? [url]http://www.daniweb.com/forums/post954890-8.html[/url]

Member Avatar for Nogat21
0
110
Member Avatar for D.JOHN

[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]

Member Avatar for D.JOHN
0
157
Member Avatar for JameB

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 …

Member Avatar for JameB
0
5K
Member Avatar for missty

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]

Member Avatar for Dave Sinkula
0
3K
Member Avatar for Nathan Campos

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, …

Member Avatar for Dave Sinkula
0
103
Member Avatar for acplus

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 …

Member Avatar for acplus
0
149
Member Avatar for NeoFryBoy

[URL="http://www.daniweb.com/forums/showthread.php?p=155265#post155265"]Avoid Loop Control Using eof[/URL]

Member Avatar for VernonDozier
0
132
Member Avatar for missty
Member Avatar for dgr231

[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].

Member Avatar for mrnutty
0
154
Member Avatar for cmsc21_scared

I don't know that you've posted enough code to debug. How about a compilable snippet that demonstrates the problem?

Member Avatar for Dave Sinkula
0
290
Member Avatar for jeezcak3++

[url]http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5[/url]

Member Avatar for mrnutty
0
93
Member Avatar for no1zson

[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]?

Member Avatar for no1zson
0
249
Member Avatar for KungFuTze

[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 …

Member Avatar for KungFuTze
0
706
Member Avatar for VernonDozier

[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]

Member Avatar for Dave Sinkula
1
202

The End.