466 Posted Topics
Re: OK, I know I'm going to regret this, because this is already an insane thread, but here goes. I have some general points to make: [list=1] [*]Don't call [icode]main()[/icode]. [*][icode]main()[/icode] should never be called. [*]Remove all calls to [icode]main()[/icode] from your code. Immediately. [*]Don't call [icode]main()[/icode]. Ever. [*]When people with … | |
Re: [QUOTE=bensewards;1469864]Also, extract names from the input stream with getline()[/QUOTE] Be careful, [icode]std::cin.getline()[/icode] is different to [icode]getline()[/icode]. | |
Re: Is there a reason you're choosing to use a c-style [icode]char[/icode] array instead of [icode]std::string[/icode]? If not, I would use [icode]std::string[/icode]. You can then do this: [code] std::string lineFromFile; while(infile.fail() == false){ getline(infile, lineFromFile); /* Check things to do with the line */ std::cout << lineFromFile << std::endl; } [/code] … | |
Re: [QUOTE=blee93;1468881]This problem makes me alter my code for the previous problem in the book so that I must enter the diameter before the name.[/QUOTE] The order that you assign values to the members of a structure shouldn't matter. Are you sure that this is the case? Also, you could consider … | |
Re: [QUOTE=caltech;1469549]It was working, until I was messing around trying to get the average of the numbers divisible by both 3 and 5 (next on the list)[/quote] You need to check if something is divisible by 3 [b]and[/b] 5 [i]before[/i] you check division by 3 or 5 individually, with something like … | |
Re: [QUOTE=Fbody;1468529]If I've read your code correctly the construction of the new iterator at the beginning of the iteration accomplishes what you are hoping this decrement will.[/QUOTE] No, you do need to decrement the iterator here. There are two [icode]while[/icode] loops and the decrement is inside the inner one, like this: … | |
Re: OK, you probably shouldn't aim a forum question at a particular user, since that user may [list=1] [*]Not be checking the forum that week [*]Not know the answer to your question. [/list] Better to make a post with a title that describes you problem, such as "Count identical vectors" or … | |
Re: I think what you have is a reasonable start. You have the right idea, using nested [icode]if[/icode] statements is one way to do it. I think that papanyquiL is correct that a [icode]switch()... case[/icode] statement would be more tidy (for deciding which choice the user has made). I would probably … | |
Re: You should pass the [icode]result[/icode] list by [i]reference[/i], otherwise it will never be changed in the calling function. So, the prototype should look like: [code] void Merge(list< int > one, list< int > two, list< int > &result); [/code] You might consider passing them all by reference, for faster passing. … | |
Re: [QUOTE=psrknellai;1469040]Hi frnd i am sivaram, first you include these header files include<time.h>,include<stdlib.h> and then in main() you put "srand(time(NULL));" after your initializations and before rand(); function every time you will get different output[/QUOTE] I generally don't like to do this, exactly because you get a "completely" random sequence each time. … | |
Re: How do you define the "best" combination? I think that this could be quite hard, sine there are a [b]huge[/b] number of combinations in which the numbers can be summed. For a general case you can't know how many of the numbers you will have to use (they could all … | |
Re: There are a bunch of ways to read in a file, but the best way for this kind of thing is probably to use something like: [code] std::string word; std::ifstream infile("myFilename.txt", std::ios::in); while(infile.fail() != true){ infile >> word; std::cout << pigLatinString(word) << ' '; } [/code] This would go through … | |
Re: It's a shame you have those restrictions, because you can do it with [icode]std::map[/icode] really easily: [code] std::map< std::string, unsigned > fruits; while(inputFile.fail() != true){ std::string fruitName; inputFile >> fruitName; fruits[fruitName]++; } std::cout << "Found the following fruits:" << std::endl; std::map< std::string, unsigned >::iterator it = fruits.begin(); while(it != fruits.end()) … | |
Hi guys, I'm sure that the answer to this is simple, but I don't know what it is and it's bugging me. For reasons that aren't important, I'm trying to make a kind of light-ish weight array class. I want to be able to use syntax like that of [icode]std::vector[/icode] … | |
Re: You should use code tags! OK, you can use [icode]getline()[/icode] for this: [code] std::ifstream infile("myFilename.txt", std::ios::in); std::string word1, word2; while(infile.fail() != true){ getline(infile, word1, ';'); getline(infile, word2, ';'); /* Now do things with that you want with the words */ } [/code] This will get words from the file two … | |
Re: I think [i]everything[/i] is stored on the stack unless you specifically do some kind of dynamic allocation using [icode]new[/icode] or [icode]malloc()[/icode] or something similar. | |
Re: [QUOTE=sfuo;1465661]Put a condition in where it will only output if it is not the last element or break outta the loop if it is on the last element instead of displaying a comma.[/QUOTE] I prefer to print the first element outside the loop, then you only need to check the … | |
![]() | Re: [QUOTE=Josue198s;1465189]guys how to slpit a date string ie....30/jully/2001[/QUOTE] [list=1] [*]Read the date into a [icode]char[/icode] array (from the user or a file or wherever). [*]A date can have a simple and strict format, so you can specify a few acceptable ones and compare the input to those. [*]Go through the … |
Re: Two things: [list=1] [*]You should use [icode]ctime[/icode] instead of [icode]time.h[/icode] [*]What is the reason for using [icode]std::string *[/icode] instead of [icode]std::string[/icode] for most of your strings? [/list] | |
Re: [QUOTE=addbot101;1465762]WHAT SHOULD I DO PLS HELP[/QUOTE] Stop panicking and write some code! The clues are all in the question, just read it calmly and plan what you need to do. It says "[I]Represent the parking spaces as a structure with 10 elements[/I]", so your [icode]ParkingLot[/icode] structure will have to contain … | |
Re: Wow, you win the prize for laziest post! At least ask your question in the actual post or, even better, show that you have written some code and provide examples of the kind of error that you're getting. | |
Re: [QUOTE=becool007;1464297]I used strcopy and strcat to join the two.[/QUOTE] As firstPerson said, you could use a [icode]std::stringstream[/icode] for this: [code] /* A string and a double that we want to combine */ std::string messageRoot("The average value for box 1 is: "); double ave = 12.01; /* Make a stringstream (initialised … | |
Re: Yeah, your code looks like a constructor but you don't assign memory for the array at any point, so trying to access elements to change their values will seg. fault. Unless you have declared [icode]A[/icode] as a 2D array on the stack (which would not be a good idea, since … | |
Re: [QUOTE=fabricetoussain;1462773]no i have not learned arrays yet.[/QUOTE] If you haven't learned about arrays yet, then you probably haven't learned about structures either. But, "in for a penny, in for a pound" and all that. So, I'd recommend using structures (or classes, but I wanted to at least [i]try[/i] and keep … | |
Re: If you're not doing this as an exercise, you can use [icode]std::count()[/icode] to find the number of spaces and then subtract this from the total: [code] #include <iostream> #include <string> #include <algorithm> // std::count() is in here int main() { std::string banner; std::cout << "Enter banner text:" << std::endl; getline(std::cin, … | |
Re: You can use [icode]clock()[/icode] to do this. You can find more about using [icode]clock()[/icode] [URL="http://www.cplusplus.com/reference/clibrary/ctime/clock/"]here[/URL]. There's actually an exact example of what you want to do. | |
Re: [QUOTE=deanus;1462702]Hi, If I'm not mistaken, the following code will allocate 5 integer elements from memory to 'ptr': [CODE] int *ptr; ptr = new int[5]; [/CODE] [/quote] Not quite correct. [icode]new[/icode] will allocate space for five integers, stored contiguously, [i]somewhere[/i] in the memory. It will then return the address of the … | |
Re: Do you need to convert at all? If you're not going to do any maths with the numbers then you just leave them and print them to the new file. If you do need to convert them, you can use [icode]atoi()[/icode] to do it. As Jonsca said, the simplest way … | |
Re: I'm assuming that the cylinder is oriented with its axis along the z-axis of your co-ordinate system. So, your function that describes the line will be a vector function of some dummy variable, [i]t[/i], say: [b][u]f[/u][/b]([i]t[/i]) = (f_x([i]t[/i]), f_y([i]t[/i]), f_z([i]t[/i])). So, you just need to find the point where f_z([i]t[/i]) … | |
Re: You can do this with the functions in the [icode]<ctime>[/icode] header file. | |
Re: There's a really short example program for reading from one camera on the [URL="http://opencv.willowgarage.com/wiki/CameraCapture"]Willow Garage wiki[/URL]. (It's at the top of the page, and is all on one line for some reason, so you'll have to reformat it to actually understand it in any way!) Maybe give that a go … | |
Re: Perfect in what way? You may have to be more specific. | |
Re: Why not both? The most suitable language will be different for different applications. The more languages you know, the more flexible you can be. Even if you can't take classes in both, you can still work in the other language for personal projects, there are plenty of free resources on … | |
Re: I think you're not subtracting the residual bits of number that are less than 100 from the number in the function [icode]n2w1to999()[/icode]. You should replace the lines that look like: [code] if (residual < 80 && residual > 69){ words1 += "seventy " ; residual = digit - 70; } … | |
Re: [QUOTE=daviddoria;1454882]Then step through the code with a debugger and ensure that it is doing every step the same as you did by hand. This will let you find the bug, then you just need to fix it![/QUOTE] I second that, definitely use a debugger to check the values that are … | |
Re: [icode]fnctn[/icode] is a [i]function pointer[/i], this is given away by the syntax [icode](*fnctn)[/icode]. It's a type of pointer that points to a [i]function[/i] instead of a variable, as in the case of a normal pointer. Here, it says that [icode]fnctn[/icode] points to a function that returns [icode]void[/icode] (since the type … | |
Re: You just missed the code-tag on your post :) OK, what you could do is add another variable to each of the grade groups that you have (A, B, C etc) that would store the average as you go along. So every time you add one to count, update the … | |
Re: [QUOTE=Dexxta27;1456564]You could try initializing all variables to zero like the above post.[/QUOTE] No. I think the point that gerard4143 was making was that the user is asked for input [i]outside[/i] the [icode]while[/icode] loop in the original code. This will definitely result in either the [icode]while[/icode] loop [i]never[/i] getting executed, or … | |
Re: Er, did you mean to start a poll with a random question in it? To answer your (post) question: [LIST=1] [*]Read in a date from the user as [icode]std::string[/icode] [*]Parse the string to extract the day, month, year etc. [*]Calculate the number of days from the date the user entered … | |
Re: If the file is guaranteed to be in the form that you have above, you can do something simple like: [code] std::string names[n]; double grade[n]; std::ifstream infile("filename.txt", std::ios::in); unsigned i = 0; while(infile.fail() == false && i < n){ infile >> names[i] >> grade[i]; ++i; } [/code] where [icode]n[/icode] is … | |
Re: OK, from the top... You should use [icode]#include <cmath>[/icode], not [icode]#include <math.h>[/icode]. The latter is what you'd do in C, not C++. [icode]argc[/icode] stores the [i]number[/i] of parameters passed on the command line, not the actual parameters, these are passed in [icode]argv[/icode]. So, in this case you can see that … | |
I have to calculate the modal value of [icode]std::vector< int >[/icode] quite often, so I thought I'd share my general method for people to look at, use and/or comment. In my case, the vectors usually contain values in the range 0 - 255. The code first creates a [icode]std::vector< int … | |
Re: Yeah, the main error here is that it's in Java. Your C++ compiler's going to really complain about that. | |
Re: I'd probably explain the situation to your professor and try and secure an extra 24 hours on the deadline to manually rewrite the lost file. That could be a more efficient use of time than trying to recover the encrypted file. Also, for critical projects, make [b]more regular backups![/b] | |
Re: Why do you need to be able to sort it fast? I'm asking because it takes quite a long time to do almost anything with 12GB of data, including generate it and read it. So it sounds to me like you might have 12 GB of data on disk that … | |
Re: If you want to check for white space characters, you can use [icode]isspace()[/icode], which is in the same header file ([URL="http://www.cplusplus.com/reference/clibrary/cctype/"]ctype.h[/URL]) that [icode]isalpha()[/icode] is in. | |
Re: Firstly, your code is terribly formatted! There are literally hundreds of extra carriage returns and the indenting is very irregular, making the code pretty much unreadable. Please show some effort and format you code in a sensible manner, rather than expect the reader to put in the extra effort to … | |
Re: Does the code read [i]any[/i] of the data from the file? As Jonsca said, that one line is OK. If it's just one line in a file, you can use a [icode]try... catch[/icode] statement to catch the exception and print out the line number of the line that has a … | |
Re: Your code if not particularly efficient, since you do a lot of checking to see if you're in the centre, both vertically and horizontally. In fact this is all determined right from the beginning by the dimensions of the diamond that you want to print. So, you can just use … | |
Re: I think you probably want line 29 to be [icode]bufptr[ (int)iIn < iMax ? iIn : iMax - 1 ] = '\0';[/icode] instead of [icode]bufptr[ (int)iIn < iMax ? iIn : iMax ] = '\0';[/icode] The way you have it at the moment will occasionally try to write to [icode]bufptr[iMax][/icode], … |
The End.