2,384 Posted Topics
Re: [QUOTE=ravinamita;1068283]I am new to XML and have a very sketchy knowledge of C. I am writing a C program in which I need to read strings (each on a separate line) in a text file, match that string with a value enclosed in a specific tag inside an XML file, … ![]() | |
Re: [QUOTE=Snapster5;1069302]is the max array size for this question 5?[/QUOTE]In this example, yes. But that makes no difference for the functions you are supposed to write. | |
Re: >any ideas? Maybe [ICODE]break;[/ICODE] the input loop on the sentinel value? | |
Re: [QUOTE=jephthah;1068614]any loop can that can be done with a for statement, can also be done with a while statement. and vice versa. the details are different but the result will be the same.[/QUOTE]That reminds me of [URL="http://www.daniweb.com/forums/post94143.html#post94143"]an old post[/URL]. But I seem to have left out recursion. :P (And it … | |
Re: One way: [CODE]#include <stdio.h> int main() { const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if ( file ) { char crossword[10][10]; int i, j; for ( i = 0; i < sizeof crossword / sizeof *crossword; ++i ) { for ( j = 0; j < sizeof … | |
Re: Very minor, but... [CODE] cout << " p[COLOR="Red"]\([/COLOR]" << x << ") = " << p << endl; [COLOR="Red"]/* warning: unknown escape sequence '\(' */[/COLOR][/CODE] So how about: [code] cout << " p(" << x << ") = " << p << endl; cout << " p'(" << x << … | |
Re: [url]http://c-faq.com/aryptr/dynmuldimary.html[/url] | |
Re: You've got nested loop issues. After you get the line, the line is got. Reading "the rest of the line" will be problematic. You should check for success when you attempt to read from the file. Avoid using eof() as a loop control. | |
Re: How about something along this line? [code]nclude <iostream> #include <fstream> using namespace std; int main() { ifstream iptilvar("ip.txt"); //open filestram string line, ip; while ( getline(iptilvar, line) ) { ip = line; } string::size_type loc = ip.find_last_of(' '); if ( loc != string::npos ) { cout << ip.substr(loc + 1) … | |
Re: I really don't care to make up the #defines and the rest. Do you suppose you could just post a small, but complete and compilable snippet? kthxbye [edit]But the nested loops don't look like a happy thing to do. | |
Re: Are necessary headers #included prior to the lines in question? The functions calls shouldn't have the [] part. Do your headings have the curly-brace part? Your code may be long, but you've edited out important details. It's generally best not to paraphrase code. | |
Re: [code][color=#000000][color=red][b]#[/b][/color]include <stdio.h> [color=red]int[/color] main() { int i=2;[/color] [color=#000000] switch ( i ) { case 1[b][color=red]:[/color][/b] printf("i am in case 1\n"); break; case 2[b][color=#ff0000]:[/color][/b] printf("i am in case 2\n"); break; case 3[b][color=#ff0000]:[/color][/b] printf("i am in case 3\n"); break; default[b][color=#ff0000]:[/color][/b] printf("i am in default\n");[/color] [color=#000000] }[/color] [color=#000000] [color=red]return 0;[/color] }[/color][/code] | |
Re: [QUOTE]baseentity.cpp In file included from baseentity.cpp:3: baseentity.h:8: warning: `class BaseEntity' has virtual functions but non-virtual destructor main.cpp In file included from main.cpp:3: baseentity.h:8: warning: `class BaseEntity' has virtual functions but non-virtual destructor main.cpp: In function `void split_string(const std::string&, std::vector<std::string, std::allocator<std::string> >&, char)': main.cpp:9: warning: comparison between signed and unsigned integer … | |
Re: getc returns an int; EOF is an int. [edit]Also, you'll need to rewind fp the way you're doing things.. [code]int countlines(FILE* fp) { int c, lines = 0; while ( (c = getc(fp)) != EOF ) { if ( c == '\n' ) lines++; } [COLOR="Red"]rewind(fp);[/COLOR] return lines; }[/code] | |
Re: [code]int main(int O,char**I){int _[]={23,10,7,25,9,26,13,-1};if(I){O=0;}if(_[O]>-1){main(++O,0);putchar('|'-_[--O]);}return 0;}[/code](Lame effort of mine, MS barks, gcc is ok.) | |
Re: [QUOTE=halluc1nati0n;1059989]Why'd they use char str[80] in the example??? What types can be used?[/QUOTE] It sounds like you may be confused as to how an array/C-style string is passed to a function in C and C++. In the example, the array for a C-style string [ICODE][COLOR="Green"]str[/COLOR][/ICODE] is declared. [code]/* strcat example … | |
Re: [QUOTE=PDB1982;1058551]But, I don't know where the -858993460 is coming from...!?! 2). Where should I go from here?[/QUOTE] Read input, [I]then[/I] print it? | |
Re: Keep track of the index of the highest value while taking input. Initialize to the first element: [code] int hi = 0;[/code] When you find a new highest value, capture the index: [code] if ( votes[i] > votes[hi] ) { hi = i; }[/code]The winner is [ICODE]candidate[hi][/ICODE]. | |
Re: The [URL="http://web.archive.org/web/20050207005628/http://dev.unicals.com/papers/c89-draft.html#4.12.2.2"]difftime[/URL] function returns the difference expressed in seconds as a double. | |
Re: Your first example may not be working the way you think it is. The #include directive is like a copy-and-paste that happens at compile time. You can't do it at runtime. | |
Re: The compiler warns because generally it isn't what someone wants. But multiplying by two is the same as adding self to self. [edit]Heh. But that won't work with pointers. Probably also for good reason. | |
Re: Try printing the bytes in the buffer rather than printing the buffer as a string (especially when you are not treating buffer as a string). | |
Re: Expected input and output would be helpful. Providing a small snippet of a simple test, minimal but complete and compilable, would too. "This code doesn't do what I want (and I'm not going to tell you what that is)" isn't very helpful. I have no idea how this compares to … | |
Re: [QUOTE=bufospro;1051624]I want to make a program that reads a string. But I would like to read only characters, neither numbers nor special characters(@,#$%^&*()!). Also, no spaces between characters.[/QUOTE]Use system-specific APIs and write a fair amount of character-handling code. Or use simple standard library functions and reject unwanted input. Which is … | |
Re: You seem to be using a lousy code editor and have "fancy quotes" instead of the plain variety. After that you'll get more syntax errors to clean up, like this [code]int calc_bonus(int sales){[/code] You need to learn how to declare, define, and call functions. So maybe take another look at … | |
| |
Re: [QUOTE=Ancient Dragon;1047685]sizeof( any pointer ) is always the same[/QUOTE]That happens to be true for the garden variety PC of today, but it is not true as a generic statement. | |
Re: Are any of the suggestions here usable? [url]http://c-faq.com/varargs/nargs.html[/url] | |
Re: [CODE]#include <iostream> #include <fstream> #include <cstdlib> int main() { std::ifstream file("reg.txt"); char text[80]; while ( file.getline(text, sizeof text) ) { unsigned long value = strtoul(text, NULL, 2); std::cout << "text[] = \"" << text << "\", value = " << value << "\n"; } return 0; } /* my output … | |
| |
Re: [code]#include <iostream> #include <string> int main() { std::string input; std::cout << "Enter a string: "; if ( getline(std::cin, input) ) { std::cout << "You entered " << input << std::endl; } return 0; }[/code] | |
Re: I believe your expectation used to be the default behavior. The new default was an "upgrade" a while back. The compromise you see is the result of some complainin'. (If I remember things correctly.) | |
Re: [QUOTE=hacker9801;643172]What's the ^ for anyway?[/QUOTE] A [URL="http://en.wikipedia.org/wiki/C%2B%2B/CLI"]C++/CLI[/URL] [URL="http://msdn.microsoft.com/en-us/library/yk97tc08.aspx"]Handle to Object on Managed Heap[/URL] I believe. | |
Re: [QUOTE=Ancient Dragon;1043921]Yup. floats are always converted to doubles when used as function parameters.[/QUOTE][url]http://groups.google.com/group/comp.lang.c/browse_thread/thread/2aaf5360b08c89a9/1000b1f7fb33ea53?ie=UTF-8&q=float+promoted+double+function+group%3Acomp.lang.c&pli=1[/url] [QUOTE]No, that's only true if (1) you call the function without a prototype in scope, or (2) it's a variable argument to a variadic function like 'printf'.[/QUOTE] | |
Re: [CODE]for (int i=1;i<=8;i++)[/CODE]Don't you mean this?[CODE]for (int i=0;i<8;i++)[/CODE] | |
Re: You'll need to make a lot of changes to your functions given this sort of pattern... From (extraneous type needs to be removed, multiplication is done using [ICODE]*[/ICODE], feed the [ICODE]pow[/ICODE] function the correct number of parameters):[CODE]double height_moon (float speed, double radians, double distance_moon(float speed, double radians)) { double height_moon, … | |
Re: Are you planning to point to memory you can write to before you write to it? [code] [COLOR="Red"]char *str[/COLOR], *tmp; char linein[81]; linenum = arrpos = 0; while(fgets(linein, 80, inf) != NULL) { printf("2\n");fflush(stdout);/* POINT 2 */ linenum++; sprintf([COLOR="Red"]str[/COLOR], "%c", linein[0]); [/code] | |
Re: [QUOTE=crazybitez]Does anyone know how i could get a list of all files in a given directory given the directory's path using c++?[/QUOTE][url="http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608"]http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608[/url] | |
Re: I don't think you even want the array, but instead are after this? [CODE] inFile.open(file);[/CODE] | |
Re: [CODE]#include <iostream> #include <cmath> using namespace std; int main() { cout << "RAND_MAX = " << RAND_MAX << "\n"; return 0; } [/CODE] | |
Re: Match your prototypes with function signatures. | |
Re: [QUOTE=cscgal;1042090]Therefore, my policy is to ensure 100% backwards compatibility[/QUOTE] :icon_lol: | |
Re: Do you know why you're trying to use an array of pointers to char instead of an array of char? [CODE]main(){ const int max_chars =100; int length= 0; [COLOR="Red"]char* letters[max_chars + 1];[/COLOR] String* string; string = new String(); cout << "Enter string: " ; cin.getline([COLOR="Red"]*letters[/COLOR], max_chars + 1, '\n'); string->stringLength([COLOR="Red"]*letters[/COLOR]); … | |
Re: Have the code mirror what you're doing: [CODE]char *VAR1([COLOR="Red"]const [/COLOR]char *VAR2)[/CODE] Also, make sure you have enough space to write into. [edit]If he wants the warning... [QUOTE=gerard4143;1041035]Try this[/QUOTE] why would a solution be to silence the warning with the cast? | |
Re: Lose the semicolon: [CODE]#define CAPACITY 128[COLOR="Red"];[/COLOR][/CODE] [edit]Avoid expressions like this: [CODE][COLOR="Red"]i=(i++)[/COLOR]%CAPACITY[/CODE] [url]http://c-faq.com/expr/ieqiplusplus.html[/url] | |
Re: Another way of reading "7 times per line" is "output a newline every 7 times". You could do this with a separate counter (in the same loop though!) that counts to 7, outputs a newline and then is reset. Or you might use the % operator with the current loop … | |
Re: Another way would be to write this as a function. If any character does not match, you can break the loop early and return a value indicating the "strings" do not match. If all characters do compare the same, then return a value indicating the "strings" do match. [code]#include <iostream> … |
The End.