2,384 Posted Topics

Member Avatar for Dave Sinkula

One thing I find terribly annoying is how the box in which code is contained always has a scroll bar with one line missing from view.[code]#include <cstdlib> #include <iostream> using namespace std; int main() { int random_integer = rand(); cout << random_integer << endl; }[/code]Is there any way to get …

Member Avatar for Comatose
0
141
Member Avatar for CStallion

[QUOTE=iamthwee]What's wrong with your example?[/QUOTE]Try it and see. [QUOTE=CStallion]Thanks! now is there a way that I can make a shortcut for that? say...if I wanted to make a function [inlinecode]arraycount(array)[/inlinecode] how would I do that so I don't have to type tha long stuff?[/QUOTE]Consider a macro.[code]#define arraycount(x) (sizeof(x)/sizeof(*(x)))[/code]

Member Avatar for iamthwee
0
143
Member Avatar for isaacmacdonald

Avoid using [INLINECODE]scanf[/INLINECODE]. [url]http://www.daniweb.com/tutorials/tutorial45806.html[/url]

Member Avatar for WaltP
0
106
Member Avatar for cocojim

[QUOTE=cocojim]I was trying to implement "strcat" myself[/QUOTE][url=http://www.daniweb.com/code/snippet406.html]Strings: Concatenation[/url]

Member Avatar for cocojim
0
237
Member Avatar for startspark9
Member Avatar for John A
0
154
Member Avatar for gorgonzola357
Member Avatar for gorgonzola357
0
82
Member Avatar for iamthwee

How about a vector of strings and the std::sort?[code]#include <iostream> #include <string> #include <vector> #include <algorithm> #include <iterator> int bylen(const std::string &s, const std::string &t) { return s.length() < t.length(); } int main() { static const char *init[] = {"yo","momma","iz","phat"}; std::vector<std::string> word(init, init + sizeof init / sizeof *init); std::cout …

Member Avatar for iamthwee
0
234
Member Avatar for gampalu

This is most definitely wrong.[code]while( fscanf(stream, str) != '\n')[/code]Look up [INLINECODE]fscanf[/INLINECODE] in your Help/man pages.

Member Avatar for iamthwee
0
522
Member Avatar for mikki2

Quickie:[code]class Dog { private: int age; int weight; public: Dog();牋牋牋//Constructor ~Dog();牋牋//Destructor void setAge(int age); int getAge(); void setWeight(int weight); int getWeight(); void speak(); };[/code][code]#include <iostream> using namespace std; Dog::Dog() { age = 0; weight = 0; cout << "Dog Constructor Called" << endl; } Dog::~Dog() { cout << "Dog Destructor …

Member Avatar for mikki2
0
321
Member Avatar for tayspen

Same for code tags. (Sometimes?) [code]fprintf(fp,"%s\n",strp);[/code] [From [post=222982]here[/post].]

Member Avatar for 'Stein
0
284
Member Avatar for gampalu
Member Avatar for Dave Sinkula
0
411
Member Avatar for gampalu

Read the file line by line. Ignore empty lines and those beginning with #. Attempt to parse other lines into [string][whitespace][value]. Attempt to match a particular string to associate with a variable. Assign the value to the variable if the string matches.

Member Avatar for gampalu
0
223
Member Avatar for l0g0rrhea

One way might be to use whitespace-delimited input functions. [code]#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream file("file.txt"); string word; while ( file >> word ) { cout << word << '\n'; } return 0; } /* file.txt All work and no play makes Jack …

Member Avatar for iamthwee
0
217
Member Avatar for Pacer

Avoid using EOF for loop control. Consider standard functions such as [INLINECODE]isprint[/INLINECODE]. [url=http://www.daniweb.com/code/snippet488.html]Here is a similar snippet I wrote once[/url].

Member Avatar for Pacer
0
200
Member Avatar for molayos

[QUOTE=molayos]I couldn't change and compile uday chitragar c++ code.[/QUOTE]As Googled and downloaded and Windows-modified, neither could I. I would venture to say that this line is buggy: [code] [COLOR="Magenta"]int [/COLOR]request ("www.somesite.com", "/post_url.pl", "search=hello&date=todat", [COLOR="Magenta"]string&[/COLOR] message);[/code]I changed it to this and was able to compile. Whether it does anything useful, I …

Member Avatar for molayos
0
265
Member Avatar for grautu

[QUOTE=grautu]Could anybody please tell me something useful about how to overcome the mentioned linker error? [/QUOTE]Usually this means that much to your chagrin, you have not actually told the toolchain to build the file into your project.

Member Avatar for sabuj
0
181
Member Avatar for codergem

Post a small but complete snippet of code that demonstrates the issue. Pepper it with some debugging output to make things clear to us and you what it is you are trying to do. You [I]can[/I] make it easier for people to answer you know. Expecting folks to recreate what …

Member Avatar for Lerner
0
174
Member Avatar for Buggaya
Member Avatar for Dave Sinkula
0
104
Member Avatar for aeinstein

Enter text containing whitespace. (And do you want to leave whitespace, including a newline, in the input buffer?) Oh, and always check return values.

Member Avatar for Dave Sinkula
0
384
Member Avatar for jaden403

[QUOTE=jaden403]I get the following error: `array' undeclared (first use this function) Any help would be greatly appreciated.[/QUOTE]As the message says, how and where do you define [I]array[/I]? (The one inside the class does not count.)

Member Avatar for server_crash
0
1K
Member Avatar for tlly

[QUOTE=tlly]I've got a 2 tricky questions for u. Can anyone write:[/QUOTE] Yes. They are so commonly asked that googling them ought to spoonfeed you enough results to choke a donkey.

Member Avatar for Dave Sinkula
0
214
Member Avatar for mikki2

[QUOTE=mikki2]but i have changed a few things in it just for practice but it keeps saying "switch quantity not". i dont know what that means...any help?[code][COLOR="Magenta"]double [/COLOR]operand; // ... switch ([COLOR="Magenta"]operand[/COLOR]) { case [COLOR="Magenta"]"C"[/COLOR]: // ...[/code][/QUOTE]"switch quantity not [COLOR="Blue"]an integer[/COLOR]"? You can't use a switch with strings, either.

Member Avatar for Dave Sinkula
0
187
Member Avatar for pench

[QUOTE=dubeyprateek]new actually calls malloc internally[/QUOTE]Maybe. [quote]18.4.1 Storage allocation and deallocation [lib.new.delete] 18.4.1.1 Single-object forms [lib.new.delete.single] [INDENT][INLINECODE]void* operator new(std::size_t size) throw(std::bad_alloc);[/INLINECODE][/INDENT] 1 [B]Effects:[/B] The [I]allocation function[/I] (3.7.3.1) called by a [I]new-expression[/I] (5.3.4) to allocate [I]size[/I] bytes of storage suitably aligned to represent any object of that size. 2 [B]Replaceable:[/B] a C++ …

Member Avatar for SpS
0
624
Member Avatar for papermaker
Member Avatar for yuzhang
Member Avatar for Dave Sinkula
0
97
Member Avatar for yuzhang

[QUOTE=Ancient Dragon]there is if you use vc++ 2005 -- strlen() returns size_t (unsigned int) with that compiler.[/QUOTE]Which is what [INLINECODE]malloc[/INLINECODE] expects. Not much reason to explicitly cast a [INLINECODE]size_t[/INLINECODE] to an [INLINECODE]int[/INLINECODE] to have it implicitly cast to a [INLINECODE]size_t[/INLINECODE], I wouldn't think.

Member Avatar for Ancient Dragon
0
167
Member Avatar for vissor3

A nudge for finding factors:[code]#include <stdio.h> void factor(int value) { int i; printf("factor(%d): ", value); for ( i = 1; i <= value; ++i ) { if ( value % i == 0 ) { printf("%d ", i); } } putchar('\n'); } int main(void) { factor(6); factor(28); factor(121); factor(5); return …

Member Avatar for WolfPack
0
257
Member Avatar for Dave Sinkula

I just ambled across this [url=http://www.daniweb.com/code/snippet491.html]whopper[/url], and it seems to be truncated. Not that that is necessarily a bad thing, I expect snippets to be, well, smaller snippets.

Member Avatar for iamthwee
0
315
Member Avatar for CStallion

I might go with something like this. [code] static const string data[][20] = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }, { "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", …

Member Avatar for SpS
0
448
Member Avatar for yuzhang

[QUOTE=yuzhang]Just thinking: how does the first malloc() function know how much memory it needs to allocate for the struct before knowing the length of the string?[/QUOTE] Because the struct does not contain a string, it contains a pointer, and the compiler knows how big a pointer is.

Member Avatar for Dave Sinkula
0
136
Member Avatar for mhm_ra

[QUOTE=mhm_ra]i need to r write binary(hexadecimal) values into a text file then read this data and manipulate it as a number[/QUOTE]So is it binary or is it text? Do you mean that you want to write the binary text representation of a value and read this text back as its …

Member Avatar for Dave Sinkula
0
269
Member Avatar for type-r

[QUOTE=type-r]if u can help email me at <<email snipped>> and ill emailu the question.[/QUOTE][url=http://www.daniweb.com/techtalkforums/faq.php?faq=rules#faq_keep_it_on_the_site]Keep It On The Site[/url]

Member Avatar for Dave Sinkula
0
58
Member Avatar for ontrix

[QUOTE=ontrix]Im using windows xp home and visual C++[/QUOTE]Your compiler's documentation is the best place to look for compiler-specific functions. It takes me forever to attempt to navigate the MSDN, so here's a start. [url]http://msdn.microsoft.com/visualc/reference/default.aspx[/url]

Member Avatar for Ancient Dragon
0
398
Member Avatar for Dave Sinkula

Clicking the [I]Posts Since Last Visit[/I] gives me a table with the last(?) column chopped (see attached picture). I'm using FireFox 1.5.0.3 on Win2k.

Member Avatar for Dani
0
163
Member Avatar for gampalu
Member Avatar for Dave Sinkula
0
131
Member Avatar for Joncamp
Member Avatar for lchamarthi

[QUOTE=ivanCeras]bit scope: signed(-32767) = 11111111 11111111 unsigned(655535) = 11111111 11111111[/QUOTE]The C languages support one's complement, two's complement, and sign-and-magnitude representations. [url]http://en.wikipedia.org/wiki/Signed_number_representations[/url]

Member Avatar for Ancient Dragon
0
205
Member Avatar for jazzz

I'd use [INLINECODE]qsort[/INLINECODE]. [code]#include <stdio.h> #include <stdlib.h> struct type { int code, subcode, branch; }; int compare(const void *a, const void *b) { const struct type *x = a, *y = b; if ( x->code < y->code ) { return -1; } if ( x->code > y->code ) { return …

Member Avatar for iamthwee
0
2K
Member Avatar for gladiator1919

[CODE]main(){[/CODE] Wake up and smell the current millenium, or even the '90s. [CODE]gets(string);[/CODE] I know of no forums that will treat you any better than a first-day newb for posting code contiaining the most well-known do-not-use-ever function in C. [CODE]scanf("%s",&change);[/CODE] [url]http://www.daniweb.com/tutorials/tutorial45806.html[/url] [CODE]getch();[/CODE] The nonportable code tends to get panned if …

Member Avatar for Dave Sinkula
0
219
Member Avatar for yuzhang

alpha [25] is off the end of the array. Arrays are indexed from 0 to N-1, which means an array declared as[code]int alpha[25];[/code]may be indexed from 0 to 24.

Member Avatar for ivanCeras
0
99
Member Avatar for yuzhang

C is not VB.[code]_snprintf(user_message,MESSAGE_LINE_SIZE, "MESSAGE [COLOR="Blue"]\"[/COLOR]%s[COLOR="Blue"]\"[/COLOR]\r",s);[/code]

Member Avatar for yuzhang
0
275
Member Avatar for CStallion

They are both in the same scope, so yes there is conflict. For block scope, enclose code in { and }. [edit]And I was going to mention the array thing, but alas I was too slow.

Member Avatar for CStallion
0
96
Member Avatar for drock9975

I didn't look too closely, but this sticks out.[code]problem.write(fileName, [COLOR="Magenta"]sizeof(fileName)[/COLOR]);[/code]Wouldn't you want the length of the string rather than the size of a pointer?

Member Avatar for drock9975
0
190
Member Avatar for MrLew
Member Avatar for Dave Sinkula
0
260
Member Avatar for sania kohli

First I'd recommend learning how to indent your code, and add some whitespace, and use full bracing, and better variable names. Then throw out non-standard junk, and add in the necessary headers. And put in some error checking. And discard the malloc casts (if you are compiling C with a …

Member Avatar for SpS
0
142
Member Avatar for yuzhang

[QUOTE=sunnypalsingh]Or [code] char *z = "Foobar"; char *t = "Foobar";[/code][/QUOTE]Note: [url]http://c-faq.com/decl/strlitinit.html[/url] [edit]Just mentioning this because it's a pet peeve of mine.[/edit] [QUOTE=yuzhang] I found the solution: memcmp(). The key is the third parameter of this memcmp() funciton.[/QUOTE]To compare C-style strings, use [INLINECODE]strcmp[/INLINECODE] as previously mentioned. [code]#include <stdio.h> #include <string.h> int …

Member Avatar for SpS
0
258
Member Avatar for badchick
Member Avatar for Pacer

[code]#include <stdio.h> #include <stdlib.h> typedef struct link { char data; struct link * next; struct link * pre; }list; main() { list * head=NULL; /* Define the head node */ list * linklist; /* Define the link list */ list * p1,* p2; /* Define tempopary node */ char flag; …

Member Avatar for Pacer
0
207
Member Avatar for sadronmeldir

This is passing several functions to glVertex3f:[code]glVertex3f(pA.giveX, pA.giveY, pA.giveZ);[/code]This is calling several functions and passing the return values to glVertex3f: [code]glVertex3f(pA.giveX[COLOR="Blue"]()[/COLOR], pA.giveY[COLOR="Blue"]()[/COLOR], pA.giveZ[COLOR="Blue"]()[/COLOR]);[/code]

Member Avatar for WaltP
0
314
Member Avatar for newgurl

Keywords: bold. Library types: normal. And [INLINECODE]std::string[/INLINECODE] is from [INLINECODE]<string>[/INLINECODE], not [INLINECODE]<string.h>[/INLINECODE].

Member Avatar for WaltP
0
272

The End.