1,426 Posted Topics
Re: If I was writing a code to compute the factorial of a number I would do this. [code=c++] #include <iostream> using std::cout; using std::cin; int main() { int factorial = 1, number; cout << "Please enter a number to find its factorial: "; cin >> number; for (int i = … | |
Re: On line 50 you are using [icode]j[/icode] without initializing it. Same thing goes for every other time. | |
Re: I'm not sure what you are trying to do between lines 70-81. if you want to find the difference between the 2 times you do[icode]return total_min1 - total_min2;[/icode]. If you want to get to the absolute difference between the two times than you can use the abs function provided in … | |
| |
Re: well if you want to search for movies with a particular genre you could use a for loop. Assuming myData is a vector and genre is a string you could use this. [code=c++] string searchGenre; cout << "Please enter the genre to search: "; cin >> searchGenre vector<t_film> matches; // … | |
Re: if you want to only get an integer then you could use something like this [code=c++] cout << "Enter menu number: " cin >> number; while (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n') // need to #include<limits> for this cout << "Please enter a valid number: " cin >> number; } [/code] … | |
Re: First [icode]void main()[/icode] is not standard. Main should always return an int. Secondly if you want to use things from the standard library then you will either need to use a using directive or fully qualify the names. [code=c++] #include <string> int main() { string word; // error compiler does … | |
Re: Could you post the deceleration for your userInfo class? Also please use code tags. | |
Re: The problem is that after you get the correct input from the user you are not setting the found flag to true. Add a line to set found to true in your else statement. As a side note the best way to get input from the user that could be … | |
Re: What tools do you have at your disposal? If you can not use the STL then you will probably want to use a 2d char array. If you can use the STL then a multimap might do what you need. | |
Re: Im pretty sure the reason that you are not getting an exception is because even though you have called delete the pointer still points to the same address. This definitely is dangerous because you have freed the memory so you cant be sure you are not overwriting something else that … | |
Re: By default members of a class are private not public. If you want to make them public you need to use the public keyword. [code=c++] class foo { int bar; public: foo() : bar(10) {} int getBar() const {return bar; } } int main { foo temp; int testBar; testBar … | |
Re: Just use a vector of students in your main function. | |
Re: The stream class is made for built-in types like int and double it has no idea what a student is. If you want to output it to the stream using cout you will need to write a function that does so. To do that you need to overload the operator … | |
Re: If you just want to keep outputting a+b you could use an infinite loop. something along the lines of [code=c++] while(true) { // code here } [/code] I have to ask why you want to do this? | |
Re: I belive the flags you are using should be [icode]ios::in | ios::out | ios::app[/icode] | |
Re: What happenes if you do [code=c++] return to_bin(static_cast<unsigned int>(dec), size); [/code] on line 36? | |
Re: If you want to find the size of a char array and you dont want to use the strlen() function in the cstring header then you can use this. [code=c++] //... char sentence[80]; cout << "Enter a sentence to find the length: "; cin.getline(sentence, 80); int stringSize = 0; while(sentence[stringSize] … | |
Re: I think you need to describe more exactly what you want to do. Do you want to run through an array of items and display the stats of the items in the array? | |
Re: Run the following code to see how many digits your double is actually holding. [code=c++] #include <iostream> #include <ostream> #include <limits> int main() { typedef std::numeric_limits< double > dl; using namespace std; cout << "double:\n"; cout << "\tdigits (bits):\t\t" << dl::digits << endl; cout << "\tdigits (decimal):\t" << dl::digits10 << … | |
Re: Is the error from line 111 of you code? You are trying to write to inventory[10] which is illegal because you declared inventory as [icode]inventory[10][/icode]. This means that the valid range for the subscript is 0-9. | |
Re: On line 32 you are using mean without ever initializing it. | |
![]() | Re: you need to use the == operator in your if statements not the = operator. == checks if two things are equal and = is assignment. |
Re: Well first off you never reset you loop variable to true after you exit your while loop. Secondly I would change line 18 to [icode]while(getline (myFile,line))[/icode] and then you can delete line 20. As a side note how are you going to handle punctuation marks? | |
| |
Re: How are the numbers stored in the file? One per line? If that is the case you might want to use the getline function for your while loop control [code=c++] string number; // open file while(getline(inFile, number)) { nuber.resize(14); // gets rid of any extra letters for (size_t i = … | |
Re: Well you could do this [code=c++] size_t pos = 0; string sample = "The man went to the store."; while ((pos = sample.find_first_of("o", pos)) != string::npos) { if (sample.find_first_of("r", pos) == pos + 1) cout << "good"; } [/code] | |
Re: An array's size is only limited to the memory of the machine running the code. If you want to make an array larger you will need to read up on dynamic arrays. If you need storage that can get very large or you need it to grow I would suggest … | |
Re: Well you could store the data in a set and define a comparison function that sorts the data by the lowest price. After reading the contents into the set and the first element of the set should be the one with the lowest price. [code=c++] class comparePrice { bool operator()(const … | |
Re: To find if a letter is in the string then you can do [code=c++] string sample = "have a good day."; if (sample.find_firt_of("ha", 0) != string::npos) [/code] The above example will only check to see if there is an 'h' or an 'a' in the string. It does not check … ![]() | |
Re: Well you can use a counter variable and increment it until you find the word. [code=c++] //... int lineCount = 1; while ( myfile.good() ) { getline (myfile,line); if(line == word) { cout << "Walked was found on line " << lineCount; } else { lineCount++; cout << line << … | |
Re: Well you can do like the standard library does and say that a iterator a pointer to the data of a class is only good until the next time you use the object. | |
Hey all, Three times today on three different computers when I went on to this site, specifically the c++ forum, I have received a anti-virus report warning me about an attack site. Has anyone else had this problem? I am attaching a screen shot of what Norton reported. Nathan Oliver | |
Re: one way to convert an int is to use the a string streams. Another way is to use the function atoi(). [code=c++] #include <sstream> // for stringstream #include <iostream> #include <string> #include <cstdlib> // for atoi() using namespace std; int main() { int a, b; string number = "12345"; stringstream … | |
Re: The first parameter of substr() is where you want to start so you don't want to start at the comma. you want to start at zero and go untill there is a comma. To do that you can do this. [code=c++] string str1 = "hello,friend"; string str2 = str1.substr(0, str1.find(",")); … | |
Re: line 2 in your in the cpp file should be [code=c++] fraction fraction::addFractions(fraction two) [/code] The reason it was not working they way you had it was because you did not have the return type in the deceleration. | |
Re: Now when you say any sequence of A-F do you mean you are only checking 6 characters sized strings like ABCDEF, ACBdEF, ACDBEF, .... If that is the case then something like this should do the trick. [code=c++] string sequence = "ABCDEF" string random; // has a random mix of … | |
Re: if you want to get a positive number from the user try this out. [code=c++] int number = -1; while(number < 0) { cout << "please enter a positive integer: "; cin >> number; } [/code] | |
Re: More than likely ShellExecute take a const char array. If that is the case try [code=c++] ShellExecute(NULL, NULL, run.c_str(), NULL, NULL, SW_SHOWNORMAL); [/code] | |
Re: To pass a vector by reference to a function like your add function you can do this. [code=c++] //prototype vector<int> & AddElement(vector<int> & container, int foo); // definition vector<int> & AddElement(vector<int> & container, int foo) { container.push_back(foo); return container; } [/code] | |
Re: So whats seems to be the problem? What code do you have so far? | |
Re: what is the value of num1 before you try the division? Use a cout statement to see what it is. | |
Re: The problem is you defined you function as using ofstream and ifstream. These are the objects used for handling files. If you want to use you class with cin and cout you need to use istream and ostream. | |
Re: I have to go with MSVC++ 2010. I really love the debugger and if you mistype a variable name it will get a red underline letting you know it doesn't know what it is. | |
Re: You don't need to install Linux. You can install code::blocks and then link it to a gcc compiler. Check [URL="http://www.daniweb.com/software-development/cpp/threads/370828"]this[/URL] out. | |
Re: If you don't have to reinvent the wheel I would suggest using the std::list. For your code that you provided though line 37 looks wrong. what happens if you change it to [code=c++] current->next = new teachersList(inTeacherName, inTeacherID); [/code] |
The End.