1,608 Posted Topics
Re: Do yourself a favor, never write a whole program and then try to debug it. You'll just drive yourself crazy. Start like this: [code] #include <iostream> // includes iostream #include <fstream> // ellows the program to write and reed files #include <string> using namespace std; int main (); { return … | |
Re: To my knowledge, you can't do that in C/C++, at least not in standard C/C++. I'll be happy to learn along side you if I'm wrong. | |
Re: [code] //basic main function int main() int height, width height = Get_Number(1); width = Get_Number(2); //Get_Number prototype int Get_Number(int x) sentinnel variable input variable use loop until user get's it right ask user for height if x == 1 else ask user for width accept input if input less than … | |
Re: The char type in C/C++ can be viewed as an int so this: char ch; cin >> ch; switch(ch) { case '~': //do something; break; } is the equivalent to this: char ch; cin >> ch; if(ch == '~') { //do something; } Each additional case in the switch statement … | |
Re: >>As for the c_str() function, what exactly does it do? It returns a const char * which contains the same char in the same order as the original STL string. const char* isn't the same as char *, so you can't assign one to the other. You can however cast … | |
Re: There's a saying about C/C++ that goes something like: "There is no such thing as a bad string." That usually is trumpeted when input is tried as a numerical value and you want to validate it. That is, you should accept the numerical information input as a string and then … | |
Re: One solution would be to add an else if and if start == -1 then change pump to something other than 1. Nothing should happen except the user prompts showing up until user enters a value for start at the cin >> start level. In line 18 the values of … | |
Re: A menu drive program frequently has a list of options for the user to choose from, typically by having the user enter a letter or a number to correspond to the activity in the list they want to do. The user's input is then frequently used in a switch statement … | |
Re: To give you an idea of the reason you got the responses you've gotten here's a brief synopsis of the basic tools/concepts I'd use to try to do this. I'd start with a two dimensional array declared on the heap using dynamic memory and initalized to a default value. Then … | |
Re: Line 14 is an else statement. Typically anything you want associated with an if/else if/else statement or a for loop body or a while loop body should be enclosed in {}, like in the snippet for the if and else if. If there isn't a set of {} associated with … | |
Re: Functions can have have a return type of void and they can have void, meaning no parameters passed, but I've never heard of a void function before. Please be more specific about what you want/need. Any function can call any other publically accessable function, irrespective of what the return type … | |
Re: for(int i=It1;i<=v1.end();i++) //can I affect the value of It1 to i ? I wouldn't equate It1 with an int. I think you may want something like this: vector<photon>::iterator It1=v1.begin(); for(int i = 0; It1 != v1.end(); It1++, i++) or maybe this is easier to understand: vector<photon>::iterator It1=v1.begin(); int i = … | |
Re: Input validation can be a challenge, depending how nitpicky you want to be. Since your input format is so specific I'd suggest a grind it out char by char analysis. Maybe something along this lines: [code] bool isValidDate(stringType input) { bool result = true; //ensure each placeholder in input is … | |
Re: Not if the substrings need to go into a rigid set of variables. You could declare an array/vector that could hold up to 6 strings and extract them out of the original string one at a time storing them in the array as you go. In the array they won't … | |
Re: Go here: [url]http://www.cppreference.com[/url] Look for C++ Strings and go to that page. Look up find(), substr(), erase(), and anything else that might look useful. Use whatever combination of methods seem to work, say maybe a loop within which is a call to find followed by a call to substr() followed … | |
Re: Sounds like you're trying to delete some memory you already deleted, or that someone else deleted for you (like if you were using an STL container class to hold the number of links) | |
Re: The Node struct is correct. However, your use the btree class does have several problems. First: You don't need to scope operator in the following line, and those like it. void btree::preorderWalk( Node *root ); It's not actually an error according to my compiler, but it isn't necessary. This is … | |
Re: First, I assume that num represents the number of nodes visited during the search process. If so, I'd assign num the value of zero inside the calling function and increment the value by one, outside of the if/else statements, each time the function is called. If num isn't assigned the … | |
Re: The modulus operator is routinely used on variables of type int. If the amount of the difference is a type float or double then you could multiply the difference by one hundred and cast it to an int. Then you can use integer math, including the % and /= operators, … | |
Re: streams to manipulate files come in three basic types (at least for us beginners): streams dedicated to reading from files--- ifstream, streams dedicated to writing to files--- ofstream, and streams that can either read from or write to files depending on what mode it's in---fstream. To use an ofstream you … | |
Re: // increment source string **p2; In some cases it can even be a typo! | |
Re: My rule of thumb is to use for loops if I have control over when the loop will stop---for example if I want it to work X times or if I want it loop while X < Y, etc, AND if the increment I want to make is the same … | |
Re: I'd declare a function accepting an int and returning void. The stopping value would be when the int passed in is less than 10, since that would be Eigen digit If the int passed in wasn't the Eigen digit, then I'd declare an int called temp to pass to the … | |
Re: There are several concerns with the code snippet you posted. First, don't use eof() in the while conditionals. Second, the if statements are superfluous. Third, the first time through the loop, current will always be placed in the file associated with fout1. That may or may not be appropriate, I … | |
Re: Depends I suppose on the list of wrong characters, and the type of string you're going to use, etc. A brute force char by char analysis of the string will always work of course, but you may be able to use find(), strtok(), etc too, depending on the situation. | |
Re: Welcome to Daniweb! Please use [ code ][ /code ] tags (with the spaces next to the square brackets) when posting code to this board to maintain the indentation you (hopefully) use when writing your code. The error message probably means that you should have a statement of some sort … | |
Re: I'd attack this problem by creating a display() method for the Matrix class or overload the << for the Matrix class. This will allow you to see what the result of sum /= 5 is. I think the real problem is the need for an overloaded assignment operator for the … | |
Re: [code] for (int compare = 0; compare < count - 1; compare++) { if (array[compare - 1] [/code] While the above code is syntactically correct it will produce a runtime error since compare - 1 will be -1 when compare is zero, and -1 is out of bounds for array … | |
Re: Here's a slightly different algorirhm. [code] Given: 1) a fixed maze with: a) defined room for mouse to start and b) defined room for placement of cheese and 2) each room has: a) a flag for being visited, and b) 4 sides which may either be a wall or a … | |
Re: First welcome to Daniweb. Second, when posting code on Daniweb please enclose it in code tags to maintain the indentation you (hopefully) use in the code you write. Use of the tags is discussed in the watermark to the message box you type in before you start typing as well … | |
Re: You appeared to have too many loops and too many calls to push_back(). See if this works better. [code] Polynomial operator+(const Polynomial& p1, const Polynomial& p2) { Polynomial result; term r; vector<term>::const_iterator p1Current = p1.poly.begin(); vector<term>::const_iterator p1End = p1.poly.end(); vector<term>::const_iterator p2Current; vector<term>::const_iterator p2End = p2.poly.end(); for( ; p1Current != p1End; … | |
Re: char list[size]; dataFile.write(reinterpret_cast<char *>(&list),size); You probably want to put something into list after you declare it and before you write it to file. If you must use the binary mode of read/write from file, then use the read() method to read from the file into the string. Otherwise, I'd encourage … | |
Re: Standard input in C++ is buffered (nonstandard methods are available to get unbufffered input if it's needed, but that can limit the portability of your program). This means when your program obtains data via the key board or from a file or from wherever else, the data is entered character … | |
Re: Do you mean you want the first row and column of numbers in the table will represent the labels of the row and column respectively? If so and the table is table of ints or a table of strings, then the labels can be incorprated into the table by hardcoding … | |
Re: Input the whole expression as a string using the getline() method appropriate for the style of string you are using. Then parse the input expression into tokens using a stringstream. First create the stringstream by using the input string as a parameter to the stringstream constructor. Then use the >> … | |
Re: If you use code tags (you can read about them in the stickies above or in the blue watermarks that appear in the message box before you start typing/pasting in it) then the indentation you (hopefully) use in your code will be maintained in the post. It's a bit of … | |
Re: I have stripped down your progam to highlight changes that seem appropriate and entered comments where changes made. [code] int main() { bool again = true; char letter; //change letter to type char, not type int while (again) { //displays menu cout << "What would you like to do?" << … | |
Re: First, have main() return an int, not void. Second, in main() when you call eval() using polynomial a like this: a.eval(2.0); you are ignoring the return value of eval(). To see what the return value is you could print it to the screen directly like this: cout << a.eval(2.0) << … | |
Re: There are any number of ways to sort an array. Some of them are called bubble sort, qsort, sort(), merge sort, selection sort, etc. Each has their attractions and detractions. For arrays of relatively small size the bubble sort seems to be very popular with beginning programers because of the … | |
Re: the item inside the switch() parenthesis must resolve to an int. That means each case must resolve to an int. 'Ja' is not an int. In fact it isn't anything. 'J' is a char and char are interpreted as ints by the computer so that works. "Ja" is a string, … | |
Re: Maybe I'm missing something here, but one of the hardest things for me is to remember the computer doesn't do things like I do. I always try to get the computer to do things the way I do when I should be taking advantage of the special characteristics of the … | |
Re: The best approach would be to show how you have altered your original code. It should be something like this: [code] int main() { int i,no; //name and acc_type have no memory associated with them, so you can't use them yet. //char *name,*acc_type; //allocate some memory to name---say maybe static … | |
Re: You could use a boolean flag to control the loop. Ask for input wihin the loop body and evaluate the result. If the input is zero then change the value of the flag. If not, then do your calculations. | |
Re: You first need to decide what tools you have to solve the problem. I'd use C++ with classes to represent a Card and a Hand and I'd sort the Cards in a Hand as they are dealt rather than sort the hand after the deal, but it could be done … | |
Re: >>float maximum(float x, float y, float z); drop the semicolon at the end of this line. With the function maximum() being defined before main() you don't need a function declaration as well. So you can delete the following line: float maximum(float number1, float number2, float number3); // functional prototype | |
Re: continue means go the start of the loop ignoring any remaining lines in the loop break means go to the end of the loop ignoring any further lines or iterations of the loop | |
Re: First, don't use the return value of eof() to control loops. Intsted call the stream directly within the conditional. In this case it would be: [code] while (getline (eyetrack, line)) cout << line << endl; [/code] Then you can query eof() to determine if input stopped due to finding end … | |
Re: >>How did it come to interpret it that way? "" is an empty literal string. The compiler is calling it a char pointer rather than a char array. >>while (cin.get() !="") get() is an overloaded istream method that can be used to get either a single char or a C … | |
Re: To elaborate vijayan121s point; as I understand it, arrays of user declared type are constructed using the default constructor of the user defined type. If you don't declare a non-default constructor for your user defined type the compiler will declare a default constructor for you. However, if you declare a … | |
Re: A little more restricted than global objects, but still worth considering in the right context, are class members declared with the keyword static as part of the declaration. These are variables that can be used by any instance of the class, but only by instances of that class. For example, … |
The End.