2,827 Posted Topics
![]() | Re: If you want random numbers, use srand and rand from cstdlib, and time from ctime: [url]http://www.cplusplus.com/reference/clibrary/cstdlib/srand/[/url] [url]http://www.cplusplus.com/reference/clibrary/cstdlib/rand/[/url] [url]http://www.cplusplus.com/reference/clibrary/ctime/time/[/url] This fills up an array of 5 integers with values ranging from 0 to 9, inclusive: [code] int a[5]; srand(time(0)); for(int i = 0; i < 5; i++) { a[i] = rand() … ![]() |
Re: I'd say the spec is wrong: [code] int move_disk(int disk,int peg1,int peg2,int peg3) [/code] I think it's this: [code] // return true if move was allowed, false otherwise bool move_disk(int fromPegNum, int toPegNum) [/code] Presumably you have arrays [code] int peg1[5]; int peg2[5]; int peg3[5]; [/code] make -1 represent "no … | |
Re: So this is not your code? If this is something you got off the internet, I would not use it. There are better examples. In particular, this function is either poorly named or takes too many parameters: [code] int daysFrom1900 (int year, int month, int day, int year2, int month2, … | |
Re: >> For historical reasons, convention is to use structs for occasions when you are going to fill it only with plain old data, or POD - that is, no functions and no types that are themselves classes with member functions. I follow this guidance. Since "structs" are available in C … | |
Re: [QUOTE=Szeth;]Wow, your last post really confuses me. I think I'm starting to see double, it's midnight here where I am deployed. I gotta have this done in six hours, and then once it is done, I need to change it again and finish that in like 3 hours.[/QUOTE] It's a … | |
Re: Scoping problems. Too many variables named i. Make sure i is what you think it is. Line 2 - wrong. The numbers in the brackets is the size. It's not a variable, particularly not an uninitialized variable. I assume it should be 3 instead of i? Line 10. Scoping problems. … | |
Re: The problem is that since, unless set otherwise, the keyboard uses buffered input and it echoes the keystrokes. Your code is blocked till the OS gives you back control, so whatever you put in your code to add asterisks or whatever won't execute till the damage is already done (strokes … | |
Re: Damn, this is a refreshing post, particularly a refreshing FIRST post. I'm sure there are some great tutorials out there. I don't have any to link. I'm sure others do. I'll throw in my two cents though. Think of Denzel Washington in Philadelphia: "Explain this to me like I'm a … | |
Re: How about using an array of pointers to functions? [url]http://stackoverflow.com/questions/252748/how-to-use-array-of-function-pointers[/url] this->UpdateMethod would be the array index. | |
Re: By your < operator, p1 isn't less than p2 and p2 isn't less than p1, so... they must be equal, so p2 cannot be inserted since it would be a duplicate. | |
Here's the code. [code] #include <iostream> using namespace std; int a = 0; int foo() { a++; return a; } int main() { if(1 || foo()) { cout << "Hello World\n"; } cout << a << endl; return 0; } [/code] Result : 0 is displayed. Change line 15 to … | |
Re: [QUOTE=pete_g;1474115]Hi frogboy No, I presume not as vectors come much later in the book. I presume I would have to do it using if statements. Just cannot get my head around it. Pete[/QUOTE] A simple array will do you fine. Or with only three numbers, don't even bother. Anything more … | |
Re: A few things. One, I think you would strongly benefit from using the toupper and tolower functions from the cctype library. [url]http://www.cplusplus.com/reference/clibrary/cctype/toupper.html[/url] [url]http://www.cplusplus.com/reference/clibrary/cctype/tolower.html[/url] Also, I don't think you need to declare variables A through F. In this code here: [code=C++] if (option == 'Y' || option == 'y') { cout … | |
126 posts, 72 of them negative. Anyone who has been to the C++ forum lately knows who I am referring to. He hits all the forums out there. They've all banned him. That's his game. How long can he post complete drivel till he gets banned? He's been around here … | |
Re: >> mod by 2 What's this mean? Be precise. I assume you mean you split the possible range in half, pick one side, then guess the midpoint of that side, then do it again and again till you find it. That's a binary search. That's fine, but you left something … | |
![]() | Re: [code] struct seat { bool reserved; string passengerName; int movie; // 0, 1, or 2 // possibly add a price and seat number here, but depending on // how you organize things, maybe not. }; seat seats[10][20]; [/code] Something like that. reserved is false if the seat isn't taken. To … ![]() |
Re: Delete lines 71, 72, 74. Worry about valid input later. One thing at a time. So you want to call main... Well you don't because everyone told you not to, but you want to accomplish what you were trying to accomplish by calling main. But before trying to REPEAT anything, … | |
Re: Not bad! Critique... I didn't run it. I don't have Dev C++. Few people do. I saw the README hoping for some guidance on how to build it since I tend not to double click executables. The README was good. The "Under 18" stipulation also applies to "Over 18 and … | |
I just learned about the AtomicInteger a few days ago. Didn't realize it existed before. I haven't tried it yet. I used to simply write my own MutableInteger class. Integer has no "set" method. AtomicInteger does, so I imagine that it can now replace my MutableInteger class. My question is … | |
Re: Line 9 - You create the pointer variable, but never use it. Line 13 - You're using an array index, which the assignment does not allow. You need to change line 13 to this: [code] total = total + *p; [/code] and you need to adjust the p pointer. If … | |
Re: You don't learn pointers by making a big long program like that. Everyone starts at "Hello World" or something similar. None of the tutorials out there that have been linked by various people on this forum do it by taking apart examples like you posted. They're all "Hello World", "Count … | |
Re: Storing your board as nine strings overcomplicates things. Each spot can have three possible states: taken by x, taken by y, or not taken. So you should probably represent the board as a 3 x 3 array of chars or a regular old one dimensional array of 9 chars. '-' … | |
Re: [code] #include <fstream> #include <string> using namespace std; int main() { string word1 = "dog\n\n"; string word2 = "dog\n"; string word3 = "dog"; ofstream out("dog.txt", ios::app|ios::ate); out << word1 << word2 << word3; out.close(); ofstream out2("dog.txt", ios::app|ios::ate); out2 << word1 << word2 << word3; out2.close(); return 0; }[/code] Results: [quote] … | |
Re: You can't return an array, but you can pass an array by reference or you can return a POINTER to the first element of the array, which can generally be though of as the same thing (someone may chime in with the intricacies of the difference between a pointer and … | |
Re: [QUOTE=Clinton Portis;]your code is based on the premise that cin extraction into an int datatype is '.' delimited, but i believe that this is not the case.[/quote] Correct. It is not. Integer extraction STOPS at the decimal, but it does not THROW OUT the decimal. It remains in the stream. … | |
Re: Line 13 does nothing. Line 12 seems quite problematic (no null terminator for str1). Does line 12 seg fault? Truth in labeling -- you need to give you variabless more descriptive names, for you and for us. Right now it's practically impossible to figure out what you're trying to do. … | |
Re: Seems to me you should simply open the file with the ios::app flag ("app" for "append"). See this page. Scroll down a little bit for a discussion of the available flags. [url]http://www.cplusplus.com/doc/tutorial/files/[/url] [quote] std::outFile [/quote] Get the "std::" out of there. outFile is a variable that YOU created, so its … | |
Re: [QUOTE=niranjan889;1474130]i have tried to use #include<string> and #include<string> but the error is [B]unable to include file IOSTREAM and STRING also getline has no prototype[/B][/QUOTE] You have [ICODE]#include <string> [/ICODE] twice. You need [ICODE]#include <iostream>[/ICODE]. You don't need the [ICODE]conio.h[/ICODE] line. If you delete the conio.h line and make the changes … | |
Re: Line 70 - You are going to get a seg fault if i >= 7, so confirm that that never occurs. Very easy to do. Stick this after line 69. [code] assert(i < 7); [/code] Stick this at the top. [code] #include <cassert> [/code] If the assertion fails, you'll get … | |
Re: Lots of ways to do it. I'd say it boils down to keeping track of what disks are on what pegs. Since this involves three STACKs of disks, I suggest using three stacks to keep track of things. When you make a move, you "pop" off one stack and "push" … | |
Re: [code] int sales[6][5]; [/code] Row indexes are 0 to 5. Rows 0 to 4 represent each product. Row 5 is a totals row. Column indexes are 0 to . Column 0 to 3 represent each salesman. Column 4 is a totals row. If salesman 2 sells $10 of product 1, … | |
Re: To the OP... You might want to consider starting a new thread. This one's pretty long. No one like me who hasn't been following this is going to go back through all the old posts, so no one new is going to comment probably. As far as portability, obsolete headers, … | |
Re: Code tags and formatting please. Code is too hard to read otherwise. [noparse] [code] // formatted code goes here. Convert all tabs to spaces please [/code] [/noparse] NetBeans and Visual Studio both have auto-formatters that will do the formatting/indenting/tabs-to-spaces formatting for you. Both are free. | |
Re: >> this is the wa my college professors taught us im sorry its just the way im used to it. You might want to give the formatting a try anyway. I predict programming will become much easier since a lot errors will stand out in a way that don't stand … | |
Re: 1) You have some Unicode character or whatever in there. You have directional quotes. You want regular old quotes, so retype that "DecimalFormat" line and see if it gets rid of the coding error. 2) Delete the semicolon at the end of [ICODE]public static void main (String[] args)[COLOR="Red"];[/COLOR][/ICODE]. 3) Please … | |
Re: >> I don't have a compiler. Get a compiler. They're free. How are you going to test whatever answers you get from here without a compiler? | |
Re: [code] public class EmployeeTester { public static void main(String[]args) { Employee buck = new Employee("buckaroo", 50); buck.getName(); System.out.println(buck); } } [/code] Line 6 does nothing. You didn't harness the return value of getName() so it's lost. if you want to display the buck object, you would do this: [code] System.out.println(buck.toString()); … | |
Re: You keep over-correcting (i.e. making the needed changes, but then also changing code that was correct). Change line 18 back to what you had originally and don't change anything else! Line 18: [code] op = getmenu(); [/code] | |
| |
Re: [code] int getmenu() // returns 1 for addition, 2 for sub traction, // 3 for mult., 4 for division, -1 for { // display the menu // ask user for input // return 1 through 4 based on user's selection } [/code] Call it from main, then do a mathematical … | |
Re: I'm guessing this is a prank and either isn't a real noob actually looking for help or a mod has had a little fun and changed the user name and/or title to make a point as they do from time to time (Niek E comes to mind). Normally, though they … | |
![]() | Re: I don't think Bird should be an interface. It should be a class and should contain col and position and should have the functions getColor() and getPosition() and fly() as well as the getters and setters for col and position. Hummingbird should EXTEND rather than IMPLEMENT Bird. If you want … ![]() |
Re: Ugh. What a mess. Who could possibly read this code? Code tags, line returns, and some indentation would be helpful. | |
Re: I think you're making it way more complicated than it needs to be. One, I see no eason at all to create any new arrays anywhere other than in the constructors. It seems to me that the "add" function would be nothing more than this. [code] public void add( int … | |
Re: First, be clear what each element of the x[] array stores. A number from 0 to 9? A number from 0 to 2^31 - 1? A number from -2^31 to 2^31 - 1 (i.e. the entire integer range). If it's 0 through 9, everything is going to be done differently … | |
Re: >> thnx,it works,but when i debug,there's four '0' at the bottom ? Then it doesn't work. But I don't get the zeroes. I don't see why you would get them either. | |
Re: Seg faults can have all sorts of causes. Most likely i and j are not valid indexes for the array. It's impossible to tell though without seeing more code. | |
Re: [QUOTE=lochnessmonster;](*cobalt).gas how does this actually get converted by the compiler? how does it know what offset in the struct to go to? it seems like there should be some kind of internal calculation that is done...for example (*cobalt)+3;[/QUOTE] The entire struct has size 12 bytes, assuming ints and float are … | |
Re: Here's what you want to do IMO. [LIST=1] [*]Ask user for input. [*]Read in the input as a string. [*]Test the string for valid input (i.e. all digits). [*]If needed, check for/clear any bad bits and/or clear the stream so you'll be ready for step 1 again the next time, … | |
Re: Is there ANY compiler that compiles this? [code] double* value(nullptr); [/code] I've never seen that, but the things I haven't seen outweigh the things I've seen, so take it with a grain of salt. Is that this? [code] double* value = 0; [/code] There are compilers and there are IDEs. … |
The End.