1,608 Posted Topics
![]() | Re: You could use a struct, a map, and maybe even STL pair class, to do something similar, but you can't do it with strings, unless the version information is a single char only. |
Re: The algorithym is the same as if you were doing it with pencil and paper. Break it down into smaller and smaller pieces, then rebuild it. [code] int i, m; double y, x, b, a, first, second, third; //intermediate calculations to get first, second, and third go here //then add … | |
Re: Since you're using strtok() to separate the words, you're probably using a null terminated char array (C style string) as the original string and not using a STL string class object to hold the original string. If that's correct then instead of using the == operator to compare two C … | |
Re: Multidimensional arrays are concepts designed to make the job/life of the programmer easier as the compiler may well convert the multidimensional array into a standard, single dimensional array. Multidimensional arrays are useful when data is thought of as being in tabular form such as seen in a spread sheet or … | |
Re: The erase() method of the STL class is overloaded to accept several different parameters. One version allows you to delete a single char at a specific address using an iterator to specify the address. One version allows you to erase a range of characters from the string using two iterators, … | |
Re: There's only 1 cout in the snippet posted, but it's going to repeat the value of byte_1 as an int 532 * 32 times. Since you are hardcoding the byte names eliminate the inner loop and change all the buff[i][x] where i is the i from the outer loop and … | |
Re: to "clear" the char array if it is used to hold strings you only need to assign the null char to index 0. Note that '\0', \0, and 0 are all ways of indicating null char but "\0" isn't anything really. To put a null char in each index zero … | |
Re: To my knowledge the only requirement for a binary tree is that each node has one parent and two children, or one root and two leaves if you want to use that analagy (the only exception is the first node entered, which has no parent/root). You can set it up … | |
Re: As another solution you can avoid initializing the array elements and assign elements as needed instead. This seems to work for me at least. [code] #include <iostream> using namespace std; int main() { int i, j; char board[19][19]; //assign all elements ',' for(i = 0; i < 19; ++i) for(j … | |
Re: First, remember to post code surrounded by code tags to retain indentation to make the code easier to read. Assuming you want the peak of the triangle centered over the base then I would would calculate the numbers and put them into a container printing the container once all calcualtions … | |
Re: Why doesn't the recieving/calling class know about the Zoo class? | |
Re: Thinking things through the best you can, frequently pencil and paper help a lot, before writing code will be very helpful. For example, you may want to to declare the bookstore as a collection of holdings and transactions. Each holding has a book and an inventory amount. Each transaction has … | |
Re: I thought this was initialization: int num[] = {1, 2, 3, 4}; and this: [code] int num[4]; for(int i = 0; i < 4; i++i) num[i] = i; [/code] represented serial assignment. Furthermore I think that either the instructor, the OP, or I (and I don't think it's me this … | |
Re: The standard says that you should be using namspaces, that the iostream header file is located in namespace std, and that the iostream header file should be included as iostream not as iostream.h. The technical differences between iostream and iostream.h header files are so minute that probably less than 10 … | |
Re: To me you appear to be trying to draw a collection of '.' to the console (or page or grid or even a file---the latter so you could print it on paper if you wanted). However, at the moment Point.draw() and Linea.draw() just output a single '.' at the current … | |
Re: Basically it sounds like you want to eliminate the last line of the file but not before you place an asterix before the correct answer. Correct? If so, then there are several possible approaches based on the exact format of the file. If each question section in the file consists … | |
Re: cstring replaces string.h which is where a lot of the standard functions to manipulate C style strings (null terminated char array) are located. string is the header for the STL string class and it contains the functions to manipulate STL string objects. | |
Re: Use an istringstream to parse the line into words delimited by whitespace. I suppose you could use strok() or similar protocol as well if you wanted. Here's one version using STL strings as opposed to C style strings. WARNING: code snippets posted below not tested [code] string inputString = "This … | |
Re: It's an imperfect world we live in. If the name of achievement field can be of variable length and have variable amount of whitespace and you can't restrcture the file by including a delimiting char of some sort, then one approach I can think of is to view each line … | |
Re: One of the things functions can do is call other functions. You've already demostrated that by calling getValues(), findLow(), etc from within the function called main(). If you aren't able to change the function prototype/definition, then this fact should be able to help you devise a flow to the program … | |
Re: For what it's worth this reference: [url]www.cppreference.com[/url] indicates back() returns a reference to the last object. You can consult the standards to determine the answer if you wish. It is my understanding that end() returns an iterator to one past the last object in the container, not an iterator to … | |
Re: If I gave you a pencil and paper and then I gave you 12 numbers that you had to find the average of, how would you do it? I suspect you would add each number to a running total to accumulate a total and once you had the total accumulation … | |
Re: Your code is almost impossible to read due to poor formatting. Learning how to format with consistent and appropriate indentation will save you lots of time trying to debug and make it easier for others trying to help or trying to maintain your code. I don't see where you declare … | |
Re: Visualize each number and each operator in the input string to be a token. Then, if each token is separated from the next by a space you can use >> to get each token into separate strings. If ()s are allowed, then typically they aren't separated from the numerical input … ![]() | |
Re: With this file format: 1234 Samuel Spade 15.5 2 H the name field will always include a space, between the first and last name. Using get() will stop input at the space. You could try using getline(), but that seems too complicated since there is no single number of input … ![]() | |
Re: The code as written accepts keyboard input. If lines 17 and 18 are uncommented and line 19 commented out then you can use the same code to obtain input by reading from files that you used with keyboard input. Otherwise, it's a loop that obtains input and passes that and … | |
Re: Does your code compile? Does it run as you expect it to for both routine and predictable special cases, if there are any? If so then the code is probably correct. If not, then isolate the part(s) that are causing the problem and try to get it to compile, run … | |
Re: Do you need the else statement there? The way you currently have it set up if l->data != value the first time through the loop, the loop will stop, and you will never advance to l->next. If l->data == value the first time through the loop you will never get … | |
Re: Suspect it has to do with these two lines: 1) bool checkRows ( int theSquare[][4], const int numRows, const int magicValue ); 2) checkRows ( theSquare, numRows, magicValue ); Line one says checkRows is expecting a 2 dimensional array of type int with 4, and exactly 4 columns in it. … | |
Re: Post deleted, didn't check my internal delay to follow up on posts posted while I was writing mine. | |
Re: Try this program. It should print input from file to screen. [code] #include <iostream> #include <fstream> #include <string> using namespace std; int main() { char gender; int answers[10]; string name; int i; ifstream fin("clients.txt") if(!fin) { cout << "unable to open file" << endl; exit(1); } while(fin >> gender) //if … | |
Re: Your formatting leaves a lot to be desired. [code] void linkedList :: create() { node *newl = NULL; node *end = newl; int data; head = NULL; cout << "\nCreate the List. Press <ENTER> after each entry\n"; for(int i = 0; i <= 5; i++) { cout << "Enter a … | |
Re: 3. Remove these letters from the alphabet: declare a string containing all the letters of the alphabet. loop through string2 and compare each letter in it with each letter in the alphabet string. Each time you find a letter from string2 in the alphabet string replace it with an asterix. … | |
Re: 1) correct my faults. Sorry too big a task. Specific questions are more likely to be answered. However, for starters I'll start here: A) Posting code this board is a little tricky. You need to use code tags to keep the indentation I hope you used when writing the code. … | |
Re: Do you know about user defined types like structs or classes? Do you know about arrays? Do you know about streams that write to and read from files? Do you know what pseudocode is? Do you know how to request user input and accept it into variables? Do you know … | |
Re: If you want to hand roll it yourself you could try something along these lines for the + operator body: [code] totalDays = dayInMonth + days; if totalDays <= numDaysInMonth(month, year) dayInMonth = totalDays; else while totalDays > numDaysInMonth(month, year) totalDays -= numDaysInMonth(month, year); ++month; if(month == 13) month = … | |
Re: The way it should be done depends on you. Do you know about arrays, multidimensional arrays, functions, how to determine if a move results in a win using pencil and paper, etc.? If you know about these things then write down on paper instructions on how to set up and … | |
Re: The range from -1 to 1 is 2. So get random numbers from 0-2 (rand() % 3 should do that), then subtract 1 from that number. The results should be -1, 0 or 1. Now if you want decimal values, not integer values, ranging from -1 to 1, then a … | |
Re: accept input as a string---no error checking needed as string type is always valid check first char of string to see if it's alphabetical char, if not get new string if first char of string is alphabetical char convert it to upper case as needed. can clear the input string … | |
Re: include the iostream and fstream header files at the top of the program. You can use the ifstream object you declared, inFile, just like you would cin. | |
Re: The file better set up so each field is entered in the in the same order. Otherwise you're SOL (usually). Since you know how the file is set up, you know which field comes first, which comes second, etc. | |
Re: xyz should be an array of ClassList objects each of which has an array of test results in it. Each ClassList object stands for a single student. You want to calculate the average test result for each student withing xyz. So you need a nested loop, the outer one to … | |
Re: Use a loop within a loop----a process often called nested loops. The outer loop controls the row and the inner loop controls the column. The body of the inner loop is where the output statement occurs. | |
Re: I'd suggest using a single ifstream clearing and closing at the end of each file and reopening with the next file in sequence. [code] ifstream fin("x.txt") while fin >> name fin >> amount cout << name << ' ' << amount << ' ' sum += amount fin.clear() fin.close() fin.open("xx.txt") … | |
Re: 1) don't use eof() to control the loop. 2) Read the file line by line using getline(), not >>. 3) Once the line is read in parse by the following cycle: look for first space, then look for next digit, then ignore the rest It's not the most straightforward line … | |
Re: if arrayname[2] is a C style string then atox would be doable. if arrayname[2] is an STL string then a C style string could be obtained from an STL string using the c_str() string class method. Alternatively, you could try Ancient Dragons recommendation and try using a stringstream object to … | |
Re: no need to cast all the literal char value to an int. Evaluate each section of the conditional starting on line 63 individually. All options in the conditional must be true to continue the loop. If input == any of the desired char then the loop should stop. Therefore I … | |
Re: Read about using code tags when posting code to this board or risk restricting the number of responses from quality responders. return main(); //OUCH! Read up on loops and use an int called flag to control it after telling the user they could enter 3 to stop the game. | |
Re: Do you even need a container (array, vector, list, whatever) for this project? Just a recursive function with single numerical variable should do it I think. The variable would need a default value to discontinue input and spit the variable from each function call return value back out to the … |
The End.