466 Posted Topics
There are occasionally posts asking how to remove white space from string of characters (either a C-style `char` array, or a C++ `std::string`). Using functions in standard C++ libraries, this is quite an easy thing to do: #include <string> #include <algorithm> #include <iostream> #include <cctype> int main() { /* Make … | |
Re: I think that [icode]new[/icode] and [icode]delete[/icode] try and initialise/clear-up the memory that they allocate, so that might take some time. Although for a [icode]char[/icode] it shouldn't have much overhead. You are allocating quite a few elements though. [icode]malloc[/icode] and [icode]free[/icode] just allocate the memory and leave it uninitialised. Hope that's … | |
Re: I think I'd use a [icode]std::set[/icode] instead of a vector and then sorting. If you do something like: [code] std::set< valueType > mySet; for( auto it_map = myMap.begin(); it_map != myMap.end(); ++it_map ) mySet.insert( it->second ); for( auto it_set = mySet.begin(); it_set != mySet.end(); ++it_set ) std::cout << *it_set << … | |
Re: > I find this confuseing as I'm associateing Student::name with "Bob" from Student student("Bob", "C++101") Teacher::clazz with "C++ 101" from Teacher teacher("Jim", C++ 101"); and Student::clazz with "C++ 101" from Student student("Bob", "C++ 101", I think your confusing a class with an instance of a class (an object). The class … | |
Re: There are many problems in your code. I would suggest starting again with a simpler set of functions that you want your class to perform. Also, if you include the header file and the main file, it would be easier for people to see what is going on. So, taking … | |
Re: [QUOTE=Red Goose;1470030]Clock measures in seconds, however you should have no trouble grabbing milliseconds.[/QUOTE] I think that [icode]clock()[/icode] actually returns the number of [i]clock cycles[/i] since the program started, not any actual unit of time. You can get an estimate of the time that passed by using [icode]CLOCKS_PER_SEC[/icode] You can read … | |
Re: I'm not sure, but [URL="http://stackoverflow.com/questions/4162135/simple-wostream-logging-class-with-custom-stream-manipulators"]this[/URL] looks like what you want to do. | |
Re: The array in [icode]SortData[/icode] [i]is[/i] potentially a dynamic array. It depends on what you do in the constructor. To me it looks like the constructor will look something like: [code] SortData::SortData(int max) : maxSize(max){ theData = new int[max]; } [/code] There are a number of problems with this constructor related … | |
Re: This won't do what you want because an iterator points at one of the [I]elements[/I] of the vector, not the vector itself. If you want to know the size of a [icode]std::vector[/icode] then just do [icode]x.size()[/icode]. If you're passing an iterator to a function, then you generally have to pass … | |
Re: Usually, a linked-list class would have a way to access at least one end of the list. For example, [icode]std::list[/icode] has [icode]begin()[/icode] and [icode]end()[/icode] methods. If you're using your own linked-list class, you will have to write in methods to access the beginning and end of the list. Alternatively, if … | |
Re: I think you have at least two problems: [list] [*]Your array is 100 (uninitialised) elements (numbered from 0 to 99), but you pass in [icode]left = 0[/icode] and [icode]right = 100[/icode], then on line 18, you do [icode]while(Array[right] > pivot)[/icode], which is guaranteed to try and access [icode]Array[100][/icode] on the … | |
Re: For simple equations of the form: [tex] \begin{equation*} 0 = \sum_{i = 0}^N A_i x_i \end{equation*} [/tex] I think you could solve for any of the [tex]x_i[/tex] with something like: [code] #include <iostream> #include <vector> /* I made these typedefs to tidy things up. The first element of the */ … | |
Re: You should try adding the following line to the .pro file: [code] QT += core gui [/code] Also, you should provide a name in the [icode]TARGET[/icode] field: [code] TARGET = myexecutable.exe [/code] | |
Re: This all looks OK (well not incorrect, anyway) as long as you have initialised [icode]aPoint[/icode] somewhere, so I'd have to say that the problem is in the [icode]Point::getX()[/icode] function. Could you post the code for the [icode]Point[/icode] class? There are some other points to mention about what you have posted … | |
Re: [QUOTE=sing1006;1529995]can anyone explain to me what is the definition for the both above.i had search on the internet but still cant got wat it is mean by.if can pls show some example to me.thanks[/QUOTE] Basically, call-by-value creates a local copy of the variable for use inside the function, whereas call-by-reference … | |
Re: I think you need: [code] class AbstractFilter{ public: /* Need to have at least one pure virtual function to make an abstract class */ virtual int doFilter( const std::ifstream& inFileStream, const std::ofstream& outFileStream ) = 0; }; [/code] | |
Re: Er, is this a C++ problem? Or, just a general maths problem? There are two ways in C++: [list=1] [*]Write your own functions to do it [*]Use an existing library [/list] I would recommend option 2. I've used the Gnu Scientific Library (GSL) in the past. It's a C library, … | |
Re: I think it says somewhere in the posting rules that you're not going to be able to get people to just do your homework for you! If you have a particular, specific problem within this assignment (such as "when I try compiling my code, I get error X", or "I … | |
Re: No, [icode]GoToCellDialog::GoToCellDialog(QWidget *parent):QDialog(parent)[/icode] is a constructor. The [icode]:[/icode] says that the stuff that follows is the [i]initialisation list[/i]. It's a way of Initialising the members of a class when the constructor is called. To simplify: [code] class A{ public: A( int iNum ); // Constructor that takes an argument void … | |
Re: Yup. As long as you include the header files for the C stuff that you want. In general C headers start with 'c', so to include [icode]math.h[/icode] in a C++ program, you'd do: [code] #include <cmath> [/code] | |
Re: I don't think there's anything wrong with the code. It compiles and runs fine using GCC. Which compiler are you using? As a side note, you could probably scale the number of comments back a bit - you can have too many :o) | |
Re: You need to specify the namespace for [icode]string[/icode], [icode]cout[/icode], etc. You do that like this: [code] #include <iostream> #include <string> int main() { std::string myString("Hellow world!"); std::cout << myString << std::endl; std::cout << "Enter a new string: "; std::cin >> myString; return 0; } [/code] Some people might say that … | |
Re: [QUOTE=Dexxta27;1534264]Can someone please explain these functions when creating a class please. Searching hasn't helped much. I need to know how they are used and examples or links to examples would be helpful. Any help is appreciated.[/QUOTE] "Get & Set" are kind of informal terms used to describe functions that allow … | |
Re: So you want to read in 50000x14 values and add a constant value to them? What do you want to do with them after that? I presume that you don't want to just print them to the console? Do you have any more detailed example of the actual code that … | |
Re: You still need to ask a question. What is it specifically that you don't know how to do? What have you tried already? | |
Re: template<> is right, you should think about the mathematics of the solution first. I think that you can use calculus to find the solution to this problem and just use the user's numbers in the resulting formula to generate the answer. It might be that the equations are complex and … | |
Re: The code posted by FloatingDivs still has the original problem that you had (on line 53). If you change that to [icode]dup = new int[MAX][/icode] then it should go away. Otherwise, you should try a new compiler. | |
Re: There's nothing to be scared of when it comes to constructors, they're actually very helpful. Your class declaration currently looks like this: [code] class GuessingGame { public: int secretRandom() return randomNumber; private: int randomNumber; int randomNumber(randomNumber <= 100, >= 1); } [/code] In this case, you haven't specified a constructor. … | |
Re: I'm not sure you can do the last two. The program gets some control over its own window (so you can remove the control buttons and the keyboard shortcuts etc), but I think that the taskbar is part of the window manager, so I'm not sure how much your program … | |
Re: What is the problem, exactly? | |
Re: Which part isn't working? Did you try debugging by looking at whether the correct node is found, or not? If the correct node isn't found, then what happens in [icode]find()[/icode]? If it is found, what goes wrong in [icode]deleteNode()[/icode]? | |
Re: Your problem is that you have redeclared [icode]size[/icode] inside the member function [icode]Boggle<T>::SizeBoard()[/icode]. If you delete the [icode]int size[/icode] on line 7 you'll be OK. You have to remember that, in C++, the [i]most local[/i] variable is the one that is used, unless you specifically do something about it. I … | |
Re: You could write a function that goes through the strings that are entered and checks that the letters all make sense. For this, you can use the functions in [icode]<cctype>[/icode], you can read more about these [URL="http://www.cplusplus.com/reference/clibrary/cctype/"]here[/URL]. You can, for example, use [icode]isalpha[/icode] to check if the character is an … | |
Re: [QUOTE=evstevemd;1479236]Check [URL="http://wiki.videolan.org/Libvlc"]Libvlc[/URL]. They have even examples there[/QUOTE] There's also libav, which is what ffmpeg uses. I think that ffmpeg might also be used by vlc. Beware though, it's not for the feint-hearted! ffmpeg is directed at processing video files, but as part of this it can encode and decode many … | |
Re: I think you've misunderstood how to make functions. The statements at the top of your code like this: [code] double addition = (d1 + d2); double subtraction = (d1 - d2); double multiplication = (d1 * d2); double division = (d1 / d2); [/code] don't do what I think that … | |
Re: Sounds like you're talking about [icode]std::map[/icode]. These are a kind of container that allows one to store "key-value pairs". There are a vast number of ways that they can be used. By way of example, one thing that you might use them for is enabling a kind of "switch-on-string" functionality. … | |
Re: [QUOTE=gerard4143;1475651]In C++ you generally create the variables where they are used...In C its considered improper programming style to mixed declarations and code but C++ its O.K.[/QUOTE] It's also very useful from the point of view of [i]controlling the scope[/i] of variables in a program. For example, in the C style, … | |
Re: To add new items to an empty vector, use the [icode]std::vector::push_back()[/icode] method: [code] #include <iostream> #include <vector> #include <string> int main(){ /* Make a vector of string to store the names */ std::vector< std::string > names; /* Read in an arbitrary number of names */ std::cout << "Tyep \"exit\" to … | |
Re: Yeah, I agree with the above. Post some C++ code and you might get more help. Also, as Clinton Portis said, you should clarify the "don't use arrays" clause with your professor. Does this mean just C-style arrays, or [i]all[/i] array-like structures (i.e. [icode]std::vector[/icode], [icode]std::map[/icode], etc)? If you can't use … | |
Re: Again, please use code tags. This is a strange program. You have a kind of half attempt at reading something from user input from `stdin`, then you have a random line that takes some things from `infile` and then you use a `while` loop to try and read the file. … | |
Re: It seems to me like you might have got a little confused about just [i]what[/i] gets copied when you pass by value. If you have a function, like your [icode]Rational::add()[/icode] function, and you declare it as: [code] void add(Rational f2, Rational f3); [/code] Then you are using pass-by-value. I think … | |
Re: Looks like you haven't included the cpp files in the project (or didn't actually implement the member functions of [icode]Pices[/icode]). Also, you know that a chess board is 8x8, right? Not 12x12. | |
Re: I use Qt Creator in Ubuntu (which uses gcc as its comipler by default, but all this will get installed when you select it from the repository). It's quite a nice place to work, the defaults are reasonable and I generally prefer qmake to automake. It's also (obviously) very good … | |
Re: I think that [code] int pieSort(int*, const &int); [/code] should be [code] int pieSort(int *, const int &); [/code] on line 15. | |
Re: If you can determine the number of characters in the largest number, then you can just use [icode]std::setw()[/icode] and [icode]std::right[/icode] to do this: [code] void printarray( int *arg, const unsigned maxWidth ) { for(unsigned i = 0; i < maxWidth; ++i) std::cout << std::setw(maxWidth) << std::right << arg[i]; std::cout << … | |
Re: Because [icode]arr[/icode] just an array of 5 [i]pointers to [icode]char[/icode][/i]. When you have [code] char *list[5][30] [/code] this makes 5 arrays of 30 [icode]char[/icode]. So doing [icode]list[i][/icode] returns a pointer to one of the 5 arrays of 30 [icode]char[/icode], which is OK, because [icode]arr[/icode] contains pointers to [icode]char[/icode]. If you … | |
Re: I don't know if you're going to end up doing this, but if you want to loop and read in a number of files, with systematic names, like: [CODE] data1.dat data2.dat data3.dat data4.dat ... data9.dat [/CODE] you can use a [icode]std::stringstream[/icode] to generate the names from the loop variable, like … | |
Re: First: use [color="Red"][code][/color] tags. Second: What [i]exactly[/i] is the problem? Are you getting an error from the compiler? If so, what is it? Is it a run-time error? | |
Re: Just a few things to consider: [list] [*]You should think about your use of [icode]while(fin)[/icode], I would go for something like [icode]while(fin.fail() == false)[/icode] instead. [*]What if there are two consecutive spaces (like people tend to do after a period? Or if, for some reason, the sentence begins with a … |
The End.