565 Posted Topics
Re: Well, you could try this: def sumFile(): sums = 0.0 files = ['a1.txt', 'a2.txt', 'a3.txt', 'a4.txt', 'a5.txt'] #here are your files for i in files: elems = (line.strip('\n') for line in open(i).readlines()) for j in elems: try: sums+=float(j) except ValueError as e: pass print sums sumFile() | |
Re: For your 1st part. This will tell you if a number is prime or not: bool isPrime(int nr){ if (nr == 0 || nr == 1) return false; for (int i=2;i<nr/2+1;i++) if (nr%i==0) return false; return true; } Now insert this validation into your loop, and see what you get. … | |
Re: Try this: #include <stdio.h> int main(void) { int ten = 10; int two = 2; printf("Doing it wrong: "); printf("%d minus %d is %d\n", ten ); // forgot 2 arguments printf("Doing it right: "); printf("%d minus %d is %d\n", ten, 2, ten - two ); return 0; } and see … | |
Re: Let me give you a head start: def myfunc(l): for i in range (1, len(l)-1): for j in range (1, len(l)-1): for define MyFunction (list): for OuterForLoopCounter in 1 to listlength-1: for InnerForLoopCounter in 1 to listlength-1: now, how about implementing the rest: if list[InnerForLoopCounter] is greater than list[InnerForLoopCounter+1]: swap … | |
Re: Here, maybe this can help you: public void loadStudentsFromFile(){ BufferedReader in = null; try{ in = new BufferedReader(new FileReader(filename)); String line; while ((line = in.readLine()) != null){ String tokens[] = line.split(","); studs.add(new Student(Integer.parseInt(tokens[0]),tokens[1], Double.parseDouble(tokens[2]))); } } catch (IOException e){ System.out.println(e.getMessage()); } } where `studs` is: private ArrayList<Student> studs = new … | |
Re: As **Adak** suggested, single digits followed by a comma (`','`) must be printed on their own line, pair of digits, separated by a dash (`'-'`) represents a sequence, where the 1st number is the lower bound, and the 2nd number is the upper bound, and you should print the whole … | |
Re: How about looping like this: int word_chooser = (int) (Math.random()*300); String word; LineNumberReader r = new LineNumberReader(new FileReader("words.txt")); while (r.getLineNumber()<word_chooser) r.readLine(); word = r.readLine(); System.out.println(word); It's rather impossible to read just a line from the middle of the file not knowing anything about it from advance. You'll have to read … | |
Re: There's no need to have a function `input()` like this: void student::input(string name, int id, int grade1, int grade2, int grade3) { cout<<"enter student id number"<<endl; cin>>id; cout<<"enter student name"<<endl; cin>>name; cout<<"enter three grades"<<endl; cin>>grade1; cin>>grade2; cin>>grade3; } when you have your private members like the function's arguments: string name; … | |
Re: This might help you: try: inventory.get('backpack').remove('dagger') except ValueError: pass | |
Re: Have you even bother searching your answer on Google first? Maybe this liks can help you: [Click Here](http://docs.oracle.com/javase/tutorial/java/concepts/interface.html) [Click Here](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html) An interface is a "pure" abstract class. It provides only a form, such as the method names, argument lists and the return types, but not the actual implementation, not the … | |
Re: Well, it's not hard to figure that yourself, given the present code... DWORD WINAPI procOpen(std::string path){ LPSTR run=const_cast<char *>(path.c_str()); STARTUPINFO sInfo; PROCESS_INFORMATION pInfo; ZeroMemory(&sInfo, sizeof(sInfo)); sInfo.cb = sizeof(sInfo); ZeroMemory(&pInfo, sizeof(pInfo)); DWORD id; if (CreateProcess(NULL, run, NULL, NULL, false, 0, NULL, NULL, &sInfo, &pInfo)) { id = GetCurrentProcessId(); CloseHandle(pInfo.hProcess); } return … | |
Re: As **gkbush** said, you first need to make sure that the strings have even length. Here's a simple function that might help you: void getEven(string &str1, string &str2){ if (str1.size()==str2.size()) return; else if (str1.size()<str2.size()) while (str1.size()!=str2.size()) str1.push_back('0'); else while (str2.size()!=str1.size()) str2.push_back('0'); } This will add `'0'`'es to the smaller string. … | |
Re: > The code pName = &name should be either pName = name or pName = &name[0]. Actually `pName = &name[0];` isn't valid, because you try to convert `char*` to `string*`. Also `char* pColor = &color[10];` will do you no good, since `&color[10]` references some memory area that `color` itself doesn't … | |
Re: Well, you could put a `System.exit(0);` after you exit from the infinite loop... Say, for example, that you have a Thread class as mentioned above: new Thread(new Runnable(){ @SuppressWarnings("static-access") public void run(){ int i=0; while(stp){ captureScreenShot(".", "jpg", "pic"+Integer.toString(i++)+".jpg"); try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { //error handling if needed … | |
Re: Well, to make it smaller, you could do like this: #include <stdio.h> #include <string.h> int main(){ FILE *f; char cho; char filename[] = "text .txt"; char text[] = "Your choice is .\n"; char* error,* errtxt; printf("Insert choice: "); scanf ("%c", &cho); switch(cho){ case '1': filename[4] = text[15] = cho; break; … | |
Re: "Sir, do you know why we stopped you?" "No." "You, sir, were speeding." "Impossible." | |
Re: Making your `FIGURE_TYPE` struct from your `Figure` class public can solve your problem. Presumably your `FIGURE_TYPE` is in the defauls private block of your class, which means you can't access it directly. Making it public allows other classes to use that struct, even if it's declared in the `Figure` class. … | |
Re: Depends on the nature of your question: * life is the opposite of death. > what life is all about No one knows, every man defines his purposes, either by adopting some ideas, or by creating some. Life is that journey in which you strive to reach your goals... | |
Re: > # Pointer to pointer to pointer to 2d array Why would you need that? Can you explain a bit? Because, if you want a 2d array you should do what **deceptikon** suggested; perhaps you want it for a reason, one that you haven't shared... | |
Re: char *strNumber = argv[1]; it's different from the object : string strNumber = argv[1]; `char *strNumber` is an array of characters, whereas `string strNumber` is a `string` object. try this instead: #include <string> #include <iostream> using namespace std; int main(int argc, char* argv[]){ string nrString (argv[1]); nrString = '0' + … | |
Re: > and if it is pointer, then why do you still need to use c_str() ? Array of charaters are considered (in C++) to be C style strings, where they would be just a sequence of characters in the memory. `string` on the other hand, is a C++ class that … | |
Re: Maybe this can come in handy: import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class view extends JFrame{ private JTextArea tx = new JTextArea(); private JScrollPane scroll = new JScrollPane(tx); public view(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tx.setEditable(false); JPanel but = new JPanel (new GridLayout(2, 2)); but.add(new … | |
Re: Maybe this link will help you: [Click Here](http://stackoverflow.com/a/7100565/1432385) You actually write bytes, and when trying to write an int, presumably your platform int is iterpreted as 4 bytes, you will have like this: byte[] int[][][][] //4*byte As you know from your assembly courses, a byte is represented on 8 bits … | |
Re: How about posting the entire error message? Perhaps you missed something... | |
Re: Here's a hint of how you can compute this number: **8**^4 + **2**^4 + **0**^4 + **8**^4 = 8208. Split the number into its digits. Sum the digits acordingly than compare to the initial number. How to split a number to get its digits, well simple: int a = 1234; … | |
Re: Well, as **mike** and **Suzie** said, the remainder is the part that remains after dividing two numbers. It can take values from 0 to divisor-1 (the number by which it divides) in the usual integer division. If you want to have the fractionar part of the number, and check if … | |
Re: Depending on your version of Python: 2.x or 3.x this applies: 2.x: x = raw_input("Beeps: ") #gets the input as a string try: x=int(x) #tries to convert it to an int except Exception as e: print "Not an int." #if it fails an error message is shown 3.x: x = … | |
Re: As **JamesCherrill** said, use the `append()` function to insert strings into your textArea. `setText()` sets the text area to whatever String you provide, much like opening a file in `"w"` or in `"a+"` mode. jTextArea2.setText("Item Name"+" | "+"Price"+" | "+"Item Number\n"); //clears the text area and inserts the header for(int … | |
Re: Till than, you can make use of what C++ already offers you: std::string to_string(double nr){ std::stringstream token; token<<nr; return token.str(); } | |
Re: Your while is kinda wrong: while (cur !=NULL); Your code chrashes the program: do{ if( cur->st.id==id ) break; p = cur; cur = cur -> link; } while (cur !=NULL); //after it finishes the while p->link = cur->link; //link is undefined if cur = NULL. NULL doesn't have a "link"... … | |
Re: So exactly, what do you want? Do you want to > run a function every actual tick. or do you want to get the milliseconds? | |
Re: It's because `TrainA` is not in the scope of the insert function. `TrainA` is declared inside a for loop (line 166), which, after it's done, will free the variable, making it unknown to the whole function. Try to get it out of that for. Not to mention that this is … | |
Re: Deleting items from a linked list: 1: deleting the head. If you want to delete your head of the list you should do this: node temp set temp = head set head = head.next delete temp 2: deleting an item from the middle For deleting an item from the middle … | |
Re: Well, depends on your merging. If you need only to concatenate the lists, than you should set your 1st list tail node to the head of your 2nd list(blink), and the tail of your 2nd list to the head of your 3rd list. To make it a circular list, set … | |
Re: Try initializing head to something, say to `NULL` by calling `linklist_1()` 1st. Or better, initialize head to `NULL` in the constructor (which you don't have)... //class public: linklist(){head=NULL;} void linklist_1(); //no need; void linklist_2(); void insFirstNode (string name, int id, string prog, double cgpa); void printAllList(); //class stuff and you … | |
Re: Yes, indeed. Line 20: cin>>m_name[count]; should be cin>>m_name; Also, it is ill-advised to use the `#include <conio.h>` header, because is a non-standard include, and is OS dependent (mainly Windows). You printing function is also wrong. For example, if I type in option 1, and I put there some string, for … | |
Re: In place reversal: char * str <- "something" char temp i <- 0 j <- strlen(str) - 1 while (i <= j) do temp = str[i] str[i++] = str[j] str[j--] = temp end_while On another `char*` char * str <- "something" char rev [strlen(str)] i <- 0 j <- strlen(str) … | |
Re: We're still here... Perhaps there were some buggs when they computed the end of the world date. | |
Re: If you're searching just for the editor, with no additional environment to your programming language, I would suggest Sublime Text 2 or Notepad++ (no direct support for *nix). If you do search for a complete IDE, than I suggest trying some IDE's, and settle for what suites you better. | |
Re: But, have you tried to write some code? Show us what you got. | |
Re: **@Raisefamus** No need for a goto statement to simulate a loop, and [here](http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF) is the explanation. **@Feal456** You could use a while loop as mentioned before. You could use also a for loop: string Choice; for (;;){ cout<<"Choose: "; cin>>Choice; //if-else if conditions; else if (Choice == "6") break; else … | |
Re: Here, this might help you: try: with open (cardno+".txt", "r") as f: try: value = float(f.readline()) except ValueError as e: print "Empty card" if (value < total): print "Not enough money." else: value -= total with open (cardno+".txt", "w") as f: f.write(str(value)) except IOError as e: print "Invalid card" | |
Re: Well, code completion is good when you're dealing with a large application, and when you have lots of classes, with similar names/functions/members. Related to small applications, I do like to type everything, because: 1. when you need something, you have to go search it first in your brain, than in … | |
Re: Here, this function will check if a string is a number or not: bool isNumber(string str){ bool doted=false; if (((int)str[0]<(int)'0' || (int)str[0]>(int) '9') && str[0]!='-') return false; for (size_t i=1;i<str.size();i++){ if (!doted && str[i]=='.') doted=true; else if (((int)str[i]<(int)'0' || (int)str[i]>(int) '9')) return false; } return true; } > and i … | |
Re: Well you could easily acomplish that by using some Linux C functions: send: [Click Here](http://linux.about.com/library/cmd/blcmdl2_send.htm) (also in your terminal man send) recv: [Click Here](http://linux.about.com/library/cmd/blcmdl2_recv.htm) (also in your terminal man recv) Here's a tutorial on that: [Click Here](http://beej.us/guide/bgipc/output/html/multipage/unixsock.html) Follow the steps Beej puts there, and you'll be fine. | |
Re: Well, you could just simply return from that function: def f(): i=raw_input(": ") #i=input(": ") if (i=="1"): print "Hello!" #print ("Hello!") else: return | |
Re: Well, I have to say: your code it's quite messy, and full of errors. class Class but yet you call the constructor as database(); You have this as private vector<string>database; yet you call void addString(vector<string> database,vector<string> newItem); with its own database `vector<string>` which will make this database.push_back(newItem[r]); locally, not actually … | |
The End.