2,384 Posted Topics
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 … | |
Re: [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] ![]() | |
Re: Avoid using [INLINECODE]scanf[/INLINECODE]. [url]http://www.daniweb.com/tutorials/tutorial45806.html[/url] | |
Re: [QUOTE=cocojim]I was trying to implement "strcat" myself[/QUOTE][url=http://www.daniweb.com/code/snippet406.html]Strings: Concatenation[/url] | |
Re: [INLINECODE]#include <stdio.h>[/INLINECODE]? Or post the code in question. | |
Re: [code] std::cout << [COLOR="Blue"]std::[/COLOR]endl;[/code] | |
![]() | Re: 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 … ![]() |
Re: This is most definitely wrong.[code]while( fscanf(stream, str) != '\n')[/code]Look up [INLINECODE]fscanf[/INLINECODE] in your Help/man pages. ![]() | |
Re: 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 … | |
Re: Same for code tags. (Sometimes?) [code]fprintf(fp,"%s\n",strp);[/code] [From [post=222982]here[/post].] | |
Re: 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. | |
Re: 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 … ![]() | |
Re: 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]. | |
Re: [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 … | |
Re: [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. | |
Re: 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 … | |
Re: [url]http://cboard.cprogramming.com/showthread.php?p=564434#post564434[/url] | |
Re: Enter text containing whitespace. (And do you want to leave whitespace, including a newline, in the input buffer?) Oh, and always check return values. | |
Re: [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.) | |
Re: [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. | |
Re: [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. | |
Re: [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++ … | |
Re: [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. | |
Re: 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 … | |
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. ![]() | |
Re: 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", … | |
Re: [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. | |
Re: [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 … | |
Re: [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] | |
Re: [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] | |
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. | |
Re: I'd recommend reading this first: [thread]40450[/thread] | |
Re: Did you have a C or C++ question? | |
Re: [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] | |
Re: 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 … ![]() | |
Re: [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 … | |
Re: 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. | |
Re: C is not VB.[code]_snprintf(user_message,MESSAGE_LINE_SIZE, "MESSAGE [COLOR="Blue"]\"[/COLOR]%s[COLOR="Blue"]\"[/COLOR]\r",s);[/code] | |
Re: 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. | |
Re: 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? | |
| |
Re: 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 … | |
Re: [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 … | |
Re: Let sleeping threads lie. | |
Re: [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; … | |
Re: 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] | |
Re: Keywords: bold. Library types: normal. And [INLINECODE]std::string[/INLINECODE] is from [INLINECODE]<string>[/INLINECODE], not [INLINECODE]<string.h>[/INLINECODE]. |
The End.