1,359 Posted Topics
Re: Crystalll: As an aside, in the future, please put CODE tags around your code samples. The forum software does not retain formatting by default, so your code loses it's indentation unless it is in code tags. | |
Re: You may want to look at the answers given [url=http://www.daniweb.com/software-development/cpp/threads/395461]here[/url], as many (if not most) of the same issues were addressed there, for the exact same problem set. | |
Re: As it is, all that you are checking is whether the region between the source and the destination is clear; you never check to see if the destination is actually on that diagonal. [code]bool validBishop(char board[][8],int inputs[]) { int sr=inputs[0] , sc=inputs[1] , dr=inputs[2] , dc=inputs[3]; int i, j; bool … | |
Re: What you want is a [url=http://en.wikipedia.org/wiki/Sorting_algorithm][i]sorting[/i] algorithm[/url], from the way you describe it. You can find descriptions of [url=http://en.wikipedia.org/wiki/Category:Sorting_algorithms]various sorting methods on Wikipedia[/url]; I personally suggest the [url=http://en.wikipedia.org/wiki/Gnome_sort]Gnome Sort[/url], as it is very simple and easy to debug, though not particularly efficient. | |
Re: For line 12, you declare [ICODE]counter[/ICODE] as a reference to [ICODE]int[/ICODE]. References are similar to pointers in that they do not hold a value in themselves, but instead refer to another variable. Thus, [ICODE]counter[/ICODE] won't work unless you assign a reference to it. I suspect that for [ICODE]counter[/ICODE] (and [ICODE]overall[/ICODE]) … | |
Re: To start with, you need to be more careful about capitalization. C++ is a case-sensitive language; [icode]MyList[/icode] is not the same as [icode]MyLIST[/icode] or [icode]myList[/icode]. This alone could be the source of some of the errors. Secondly, you don't want to [i]rename[/i] an object to something else; you want to … | |
Re: One thing strikes me most of all, that you did not, in fact, close and re-open the file as the instructions said would be necessary. This is probably the main reason it isn't working, and the data you are getting is probably garbage that was already in the allocated memory. | |
Re: I strongly recommend that you first simplify the program thusly: [code]/* File: Rainfall.cpp Description: Write a program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program … | |
Re: One thing I noticed (aside from your indentation, which needs improving) is that the project said to set the file to [url=http://www.cplusplus.com/doc/tutorial/files/]append mode[/url]. Thus, you should change the open() call to: [code] file.open("output.txt", ios::out | ios:app);[/code] This ensures that the data is added to the end of the file, rather … | |
Re: You need to move the final word count out of the while() loop. You also need to check for in.fail() in the loop itself, in case there's a problem with the input that did not appear when it was opened. [b]EDIT:[/b] I think I know where the problem lies: you … | |
Re: Your description of the problem you're having wasn't very clear; you say you want to search from an input file, but right now, all you're doing is reading in data from the console. Can you explain the problem better? If you don't mind me asking, why do you have the … | |
Re: I am assuming you meant Visual C++, not Visual Basic. Otherwise, you've gotten very confused about the language you are using :?: The error indicates that the function [ICODE]_tmain()[/ICODE] is missing. Now, in UNICODE enabled Windows programs, [ICODE]_tmain()[/ICODE] is the equivalent of the [ICODE]main()[/ICODE] function in regular C++. That you … | |
Re: To begin with, please use [[i][/i]code] tags around all code samples in the future, especially ones as large as this. This is very important, as the code tags force the software to retain the indentation; without them, the code is almost impossible to read. [code] #include <iostream> #include <fstream> #include … | |
Re: You never allocate the memory for the [ICODE]name[/ICODE] and[ICODE] branch[/ICODE] variables, so when you copy the strings to them, they end up writing to random locations in memory. Add the following to the [ICODE]new[/ICODE] and [ICODE]delete[/ICODE] operators and it should fix the issue: [code]void *student::operator new(size_t size) { void *p; … | |
Re: I have noticed that there are some problems with the indented code posted by Captain Jake, specifically that some braces appear to be missing. I've used AStyle to automatically re-indent the code, which I hope will help make the main bug clearer: [code] { int quantityamount1[3]; for(int i=0; i<3; i++) … | |
Re: You would do better with a stack of strings rather than chars, I think. You then could tokenize the input into strings, and push the strings onto the stack. A better approach might be to use a discriminant union (that is, a structure with a type marker and a union … | |
Re: Could you please tell us what is it doing? What [i]should[/i] it do that it is not? | |
Re: I agree with Pseudorandom21 on both of these points, especially the latter: when I tested the code myself (using GCC 4.4.1 running through Code::Blocks 10.5 for Windows), I found that the program would hang and crash until I changed that particular loop to test for end-of-file. As for the box … | |
Re: Oops, I missed that. You need to check whether [ICODE]line_counter[/ICODE] is zero in your loop conditional, otherwise it will try to read an non-existent line, causing the segfault. [code]while(choice != 'q' && line_counter > 0)[/code] | |
Re: If you put the final [ICODE]while()[/ICODE] conditional at the beginning of the [ICODE]main()[/ICODE] function, and put braces around the rest of the function, it should loop around the whole function: [code]#include <iostream> #include <fstream> #include <windows.h> #include <string> using namespace std; #define DEBUG 0 int Password = -1; int Option … | |
Re: Aside from some extremely inconsistent formatting, I can see a number of issues with this: [list] [*] You don't appear to be testing whether the destination (destiny?) is still in the same column as the original position. I realize that you aren't addressing horizontal movement yet, but you should at … | |
Re: I am not familiar with a pseudo-stack, myself. Stacks are common enough, yes, but not pseudo-stacks, at least not in the usual terminology. Can you tell us the context in which you've encountered this term? | |
Re: Actually, I think that thines01 misunderstood the project requirements; what you describe is what is called a 'shell' in the Unix/Linux world. You won't necessarily be getting command-line arguments for this program, but will be reading in a command line and executing it, then reading another command line and executing … | |
Re: The specific problem you mention comes from a missing closing brace in the [icode]RationalNumber()[/icode] c'tor. However, it is not by any means the only problem in the code. For example, the assignment operator should be something like this: [code]RationalNumber& RationalNumber::operator= (const RationalNumber &right) { numerator = right.numerator; denominator = right.denominator; … | |
Re: You need to actually instantiate a [ICODE]TestScores[/ICODE] object using the non-default c'tor, which is the only place you actually throw the exception in question. | |
Re: [QUOTE=fmasroor;1702614]2. Why use pointers when the array can be declared as global?[/QUOTE] I assume you're talking about passing arrays to functions, in which case the answer is, because global variables in general are a Bad Thing, or at least enough of a bad thing that you will want to use … | |
Re: About half of these are warnings rather than errors (mostly ones relating to standard functions Microsoft considers 'deprecated', which in this case means, "Don't use the standard version, use this spiffy safer new version that happens to lock you into our compiler"). The main reason they appear at all is … | |
Re: The problem is that, unless you define them yourself, there are no comparison operators for the [ICODE]Date[/ICODE] type. The compiler does not know how to compare one [ICODE]Date[/ICODE] value with another. | |
Re: The error messages are fairly straightforward: most of the member functions you are calling do not actually exist. You need to define and implement a constructor for [icode]Node[/icode] which accepts a string as an argument, plus all of the member functions which you are using ([icode]SetNorth()[/icode], etc). | |
Re: To address the last (non-)question first, you could use the predicates from [url=http://www.cplusplus.com/reference/clibrary/cctype/][ICODE]<cctype>[/ICODE][/url] to check the characters if you really needed to. You'd probably have to check against a few different things to get exactly the character set you were looking for. On a side note, with a modern C++ … | |
Re: How are the boxes defined in the program? Or is that part of what you need help with? | |
Re: As a side issue, why didn't you define the border functions using a [icode]for()[/icode] loop? It would be a lot easier to read and change, if you needed to: [code] const int LINE_LENGTH = 75; // ... void borderDown() { const char o = 209; for (int i = 0; … | |
Re: Certainly, you can write functions that work recursively, yes. Here is your function written in just such a fashion: [code]#include <iostream> #include <cctype> using namespace std; void repeat(); int main() { repeat(); } void repeat() { char s3='n'; cout << "Would you like to perform another operation? (Y/N)" << endl; … | |
Re: The linker isn't losing track of things; the problem lies in the header, and more specifically, the function prototypes for those two functions. The prototypes are declared as: [code]void clearBoard(int, char); void printBoard (char, int);[/code] Whereas the functions themselves are actually: [code]void clearBoard(int, char*[]); void printBoard (char*[], int);[/code] This is … | |
Re: I believe that at least one part of the problem can be fixed by changing the function prototype such that [ICODE]nbrCells[/ICODE] is a [ICODE]const long[/ICODE] reference rather than just a [ICODE]long[/ICODE]: [code]template <class T> int getVectort(ifstream & s, const long & nbrCells, typename vector<T>::iterator iter)[/code] This will allow for either … | |
Re: Actually, there is one more means of repetition, called recursion, but most C++ programmers consider recursive functions to be both too confusing and too inefficient to be used regularly. Being a Lisp programmer, I find this attitude silly, but there it is. :P Recursion consists of writing a function that … | |
Re: This may be a bit advanced of a suggestion, depending on just how the professor is teaching the course, but: one way to improve the readability of the code would be to use an enumeration for the three different states rather than using numbers for them. Combining this with the … | |
Re: Surprisingly, it is actually valid C, or rather, it [i]was[/i] valid about twenty-five years ago. The function argument declarations are of a form that was used in 1st edition K&R C, but which most modern compilers wouldn't accept as far as I am aware (though interestingly, GCC accepts it without … | |
Re: While I am not certain why it appears to be bypassing the input, I can tell you why it is going into an infinite loop: at no point do you change or test [icode]tries[/icode], which means that if the input is not a valid number in the right range, it … | |
Re: While it is not the only problem with the code, the main issue is that you are using an output file for input. You will want to change the program thusly: [code]#include <iostream> #include <fstream> #include <string> const int LinesToPrint = 10; using namespace std; int main() { ifstream inputfile; … | |
Re: Getting back to the original question, the code below fixes most of the syntactic errors in the code, though I did not attempt to correct the logical flaws without a clearer idea of the intended behavior: [CODE]#include <iostream> #include <iomanip> #include <string> #include <cctype> using namespace std; const int ARRAY_SIZE … | |
Re: I gather your intention is to have a ring buffer queue, correct? I think that this solves most of the problems you are having: [code]#include <iostream> const int SIZE = 6; class queue { private: int a[SIZE + 1]; int front; int rear; public: queue(); ~queue(); bool insert(int i); int … | |
Re: The first thing I recommend is that, in order to simplify reasoning about the Simpson's Rule function, you should move the part that gathers the elevation data from the function proper, storing the data in an array which you can then pass to the function. You'll need to allocate the … | |
Re: There are a number of issues with this code that I can see, with the foremost being with the way you are getting the time. As it is, you are reading in the time as an 8-character array, using [icode]gets()[/icode] (which is deprecated as being unsafe, but that that's a … | |
Re: From the thread title, I assume you are a Java or C# programmer. The [ICODE]main()[/ICODE] in C++ works very differently from the equivalent in Java, being a bare function not associated with a class. You would write a [ICODE]main()[/ICODE] function something like this: [code]int main() { // code goes here … | |
Re: I'm not sure about [icode]e[/icode], but for [icode]d[/icode], you will want to use the [url=http://www.cprogramming.com/tutorial/modulus.html]modulo operator[/url], [icode]%[/icode]. For [icode]e[/icode], as I said I'm not sure, but I think it has to do with integer division by 5. | |
Re: You can use the combination of [url=http://www.cplusplus.com/reference/iostream/manipulators/setw/][icode]setw()[/icode][/url] and [url=http://www.cplusplus.com/reference/iostream/manipulators/setfill/][icode]setfill('0')[/icode][/url]. [code] cout << setw(2) << setfill('0') << day;[/code] | |
Re: The issue is not with the header files, [I]per se[/I], but with the object files that the compiler produces before linking. You need to have the compiled object files linked together in the final version of the program, which (depending on how you are doing things) means having them together … | |
Re: What do you need help with? What have you been able to do so far? Do you know the syntax for declaring classes, for inheritance, for invoking a method, etc? | |
Re: In the definition of the [icode]Weapon[/icode] structure, the first letter of the [icode]Name[/icode] member is capitalized. In the code referring to it, the first letter is lowercase ([icode]name[/icode] instead of [icode]Name[/icode]). It's the sort of mistake anyone could make, and would have been easy to find except that the compiler … |
The End.