145 Posted Topics
Re: Your first problem description had almost no relationship to your second. The %x format specifier (for scanf() and related functions) reads a hex value (with the 0x prefix) and stores it in an int. If that integer is within the range of values that can be stored in a char, … | |
Re: Your problem has nothing to do with use of namespaces. DarkObject::Object::imageID is a non-static member of DarkObject::Object, so it would usually be initialised within a constructor of DarkObject::Object. Your are attempting to initialise it as if it is a [u]static[/u] member of DarkObject::Object. | |
Re: Your basic problem is that some of your code can encounter errors, but you're not testing for them. What do you expect a sequence of calls to content.get() (with different argument types) to do if one of the previous calls encounters an error? And, if an error occurs, what do … | |
Re: Your problem is actually the first four lines of the main() function. I've added comments to the code that are related to problems on each line. [code] void main() // main should return int, not void { sLink* pChainLink; // uninitialised pointer sLink ChainLinkNo1 (pChainLink, 1); // undefined behaviour: accessing … | |
Re: The C++ standard explicitly allows compilers to eliminate temporary objects if the only way of detecting if those temporary objects exist is to track constructor and destructor calls. A special case of this is the Return Value Optimisation (RVO), which your compiler is implementing. This optimisation basically means the compiler … | |
Re: mayabo's original code (other than the line [icode]cout << (A)DUDE;[/icode]) should actually work. The line [icode]name="bob";[/icode] in B's constructor is not required (and A::name can be made private). The compiler is able to do the conversion "derived class to public base" (i.e. B to A) implicitly. If you're getting errors, … | |
Re: Your main.cpp needs to have visibility of the definition of the conversion (or cast) operator. In other words, the implementation of the operator needs to be inlined into the vector.h: otherwise the compiler cannot instantiate the template. Unrelated to your problem: it is usually a good idea for copy constructors … | |
Re: Tip: If a and b are integral values then [icode]a % b[/icode] gives the remainder that is left over when dividing a by b. | |
Re: You are making the mistake of assuming that integer division yields a floating point result - it doesn't: it yields an integer result and integers can't represent fractions. In your code i/2231 is an integer division, and yields an integer result by rounding towards zero (hence always yields zero if … | |
Re: Try [code] void func(int (*)[2]); // declaration void func(int (*x)[2]) { // whatever } [/code] In this exmaple, func accepts an argument of type "pointer to array of two ints". The initialisation of A in your example should be something like; [code] int A[2][2]={{1,2},{3,4}}; [/code] | |
Re: Don't use %d to print out pointers. Other than that, try explaining what [u]you[/u] expect the code to do, and then compare that with what the code does. | |
Re: Saving data in an executable means a self-modifying executable. And self-modifying executables are most often the basis for mutating viruses. While there are courses on computer security that teach techniques to counter malicious code, no reputable course would have an exam question that involves writing self-modifying executables. | |
Re: Putting it into the executable would be no more secure than a text file: all a prospective password hunter would need to do is search for data known to be near the strings you store and ..... Access control, running your program with specific privileges, and encryption are more commonly … | |
Re: [QUOTE=jbennet;727052]shit lol i got 7 and im from the UK![/QUOTE] So you're now a man without a country? | |
Re: [QUOTE=Freaky_Chris;726197]Just to note, this sort of thing is better done with a macro [CODE=cplusplus]#define SIZE 101[/CODE] Chris[/QUOTE] Disagree. This sort of thing is sometimes done with help of a macro. That doesn't make it better. Macros are not type safe, cannot be checked by the compiler, disrespect scope, can be … | |
Re: [QUOTE=ddanbe;725026]You can also use a [B]switch case [/B]statement instead of multiple [B]if / else if[/B] [/quote] Only if the behaviour sought is comparing an integral variable against a fixed set of values. It is not possible to use a switch statement with non-integral types, or have a case that is … | |
Re: A post-test loop is a generic name for a loop of the form do { ... } while (condition), as opposed to a while(condition) {} loop. The solution, if you have to do that, is to have the printf() statement reporting the error [u]within[/u] the loop condition. To do that … | |
Re: Don't use the first option. The whole point of a Base class is capturing functionality that is common to [u]all[/u] derived classes. As to the second option, there are many ways. Such as.... Provide a protected virtual function named UpdatePoints() in your base class. Implement it to do nothing (i.e. … | |
Re: [QUOTE=Sci@phy;720918]Don't know if it helps. I myself am not sure if it's ok to write it, but compiler doesn't complain: [CODE=cplusplus] void func(int n){ int a[n]; for (int i = 1; i <= n; i++) a[i+1] = i*i; for (int i = 1; i <= n; i++) std::cout<<a[i+1]<<std::endl; return; }[/CODE][/QUOTE] … | |
Re: 1) People here are not free homework generators; at best, you'll get help if you show evidence you've tried and encountered a particular problem. 2) This homework exercise does not require writing a program. | |
Re: You're missing a closing brace at the end of main(), so the compiler thinks you're trying to implement a function inside the body of a function - which is illegal. | |
Re: (1) strtok() assumes both its arguments are strings that are terminated with a 0 character. Behaviour is undefined if that is not true. 2) strtok also returns NULL if it cannot find a token being searched for. strcmp() yields undefined behaviour if either argument is NULL. 3) A segmentation fault … | |
Re: getline() approach also allows handling of cases where someone enters a partial name (eg first name only vs first and last name). However, it is necessary to parse the line input to determine whether it contains one or two (or more) fields. | |
Re: [QUOTE=Denniz;720350]I don't know about others but to me putting function declarations inside main is just like a potential time-bomb, even if at this moment your functions do not call each other.[/QUOTE]Oh, rubbish! Your description is excessive. The worst thing that can happen is that another function needs to call the … | |
Re: It's usually called a compiler! Quite a few mainstream compilers have an option to emit assembler code (for the target system) rather than compiled object files. You need to read the documentation for your compiler to find the relevant command line option or (with an IDE) configuration setting. Couple that … | |
Re: The problem is your coding errors. [QUOTE=Trekker182;717312] [CODE]#ifndef SHIP_H #define SHIP_H #include<string> #include<iostream> using namespace std; class Ship { // stuff snipped in interest of brevity virtual void printInfo() { cout<<"The name of the ship is "<<name<<endl; endl; cout<<"The year the ship was built is "<<year<<endl; }; #endif [/code] [/quote] … | |
Re: There are plenty of open source C/C++ compilers. They are fairly complex bits of code, so the notion of "debugged easily" depends on your notion of what is "easy". However, the information on how they work is well documented. If you want to debug compilers, and you have skills with … | |
Re: 10000000 is an integer constant, and is probably too large to be represented in the int type supported by your compiler. [That presumably means you are using 16 bit compiler]. Also, as someone said in your other thread, "if ( 4000000 < ammwater < 10000000 )" does not test if … | |
Re: [QUOTE=panpanf;716493]when we use int *p=new int; we know p point to int. But i just wanna know how the compiler know p's type? [/quote] Because you've told it so. int *p tells the compiler that p is a pointer to int. The compiler also knows that the expression "new int" … | |
Re: [QUOTE=Clockowl;715128]Only plain wrong if you're not using a compiler that has defined it. Given that that's not the case most of the time: check your compiler's documentation and see if you can use it, then, bare in mind that this code is not 100% portable and SHOULD input not work … | |
Re: [QUOTE=Alex Edwards;715906]There may be a way to fool around with memory and store the information somewhere more permanent, but I don't know how to do that, nor would I recommend it.[/QUOTE] Yeah, it's usually called "copying contents of memory to a disk file in some format" :lol: There are some … | |
Re: All the results it gives are as if tax is zero, right? The reason is that you're doing everything with integer arithmetic. To look at an example; [code] else if ( 15000 < income <= 44000) { tax = (income - 15000) * (20/100); [/code] With integer arithmetic (20 and … | |
Re: [QUOTE=Ellisande;709861] My code goes something like this: [/QUOTE] Try giving a [u]small but complete[/u] sample of [u]actual[/u] code that illustrates your problem, rather than "something like this" which may actually be different. Include a sample input file as well. At the moment, we can't guess the cause of your problem … | |
Re: Your function highestTest() does not initialise the variable "highest", but the first operation involves comparing its value with elements of the array. That means the value it ends up with in that function will probably be junk. The printHighest() function declares its own local variable named highest (again uninitialised so … | |
Re: The short answer: wherever the compiler and/or system choose to store them. A longer answer follows. Older systems used to make a distinction between "stack" and "heap" (e.g older IBM compatible PCs used different memory chips for "stack" and "heap", and needed special drivers to access heap). Global, static, and … | |
Re: First tell us how you are measuring its memory usage, and the basis on which you think that usage needs to be reduced. It would help if you used a recent compiler/library and used standard functions/etc. There is no <iostream.h> (except with old compilers) and <conio.h> is system-specific. Memory usage … | |
Re: Yes. Look up the C standard header stdarg.h or (in C++) the standard header <cstdarg>. Those headers contain macros and types that support writing functions with variable argument lists. | |
Re: This is off-topic in a C++ forum. However, I'll take pity and offer suggestions. No guarantees though ..... 1) Check if you have a hardware problem: for example a loose memory module, a loose connector, a card that's loose in its slot. Loose bits of hardware can cause all sorts … | |
Re: [QUOTE=rati;712144]It is because you are assigning the value to argv[1] every time in your if condition. Use == instead of =[/QUOTE] Wrong. As Dave said, comparison of char arrays (aka "C style strings") should usually use strcmp() rather than ==. | |
Re: The simpler way (less error prone) is to use a typedef helper. [code] typedef float ((*YourFunction)(int, int); YourFunction YourArray[3]; [/code] or, if you wish code that is terse but easier to get wrong (typos very easy), use this; [code] float (*YourArrayAlternative[3])(int, int); [/code] to declare an array named YourArrayAlternative. Whichever … | |
Re: As a matter of fact, no. The name of an argument in a function declaration (ie. "myFloat" in [icode]void foo(float myFloat);[/icode]) is generally ignored by the compiler. It is generally used simply to give information to the programmer (eg by providing a descriptive name, rather than adding an additional comment). … | |
Re: Apart from that, it often takes a lot more effort to write assembly code to achieve X than it does to do X in a higher level language like C. C also has a library .... which means it is not necessary to code everything from scratch. As an exercise, … | |
Re: 1) Comparison of test with 1 has two equals signs, not 1 [icode]if (test =[color="RED"]=[/color] 1)[/icode]. Having one equals sign assigns test to 1. 2) main() returns int, not void. Wash your mouth out with soap for returning void. | |
Re: I'd say you've got Buckley's chance of finding someone here who will do your homework for you. | |
Re: x is not initialised to point at anything and you are dereferencing *(x+i*nc+j) in your loops. Before your loops, you need to make x point at something valid, such as the first element of an array with nr*nc elements. | |
Re: The short answer is "yes and no". The C++ standard specifies no direct means of accessing hardware. However, with the help of non-standard techniques (which are operating system and hardware specific) it is possible to control hardware. Technically, those techniques are beyond scope of the C++ standard but, in practice, … | |
Re: You seem to have solved the looping problem, except that in most tests a grade of zero is possible and your (grade > 0) condition excludes that. An aside: I knew a teacher who came up with a test that, she said, nobody could earn a zero grade .... because … | |
Re: Salem is right; there is no way to #define a macro that expands to a preprocessor directive. There is also the incidental concern that #warning is not a standard preprocessor directive. Any C++ compiler will generate an error on an attempt to call a function that has not previously been … | |
Re: There is also the incidental concern that your function is recursive, which does not meet the requirement to be non-recursive. | |
Re: [QUOTE=jeevsmyd;706311]cin waits till the user enters the answer... but is there any input method which wont wait for the user?[/QUOTE] Neither standard C not C++ support such a capability (over-simplistically, because C and C++ are designed to work on platforms without a keyboard, and such capabilities generally amount to reading … |
The End.