• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=harryh11;42971176]I might not be explaining myself very well. I have an one-dimensional array of strings called StringArray. String[] = StringArray[10] Every element of StringArray has the format: StringArray[0] = "x0, y0" StringArray[1] = "x1, y1" ... ... StringArray[10] = "x9, y9" StringArray is static and I want to use it in different ways. Sometimes I simply System.out.print(StringArray[a]) But sometimes I want to actually make a new pair of strings, StringA and StringB, so that they are as follows StringA = "xa" StringB = "ya" Where a is whatever index I need it to be at the time. So I've got a load of strings which are 2 words separated by a comma, and I want to pluck the individual words from those strings, and ignore the comma. I hope I am being clear here.[/QUOTE] If you intend to use them separately, why not store them separately and add them together where necessary? Or even have two lists? One with all x's and one with all y's.
[QUOTE=ZeekyHBomb;42975549]Oops, yeah, std::getline is defined for std::string (and char*), but it doesn't do integer conversion. You could do that manually, using std::stoi (C++11) or std::istringstream.[/QUOTE] Where would i want to use that line to ignore the "/"? If i have: [code] getline(patient, lastName, ' '); getline(patient, firstName, ' '); getline(patient, systolic, '/'); getline(patient, diastolic); [/code] would i want to use the std::istringstream? My roommate and I are going through it right now trying to figure it out.
You store the numbers in a string temporarily and then feed it to a istringstream for conversion; istringstream implements the istream interface. [code]std::string systolicStr, diasolicStr; //... getline(patient, systolicStr, '/'); getline(patient, diastolicStr); std::istringstream pressureConv(systolicStr); pressureConv >> systolic; pressureConv.str(diasolicStr); pressureConv >> diastolic;[/code]
[code] //Aaron White //ProgramII //input data, output customer info and transaction info #include "stdafx.h" #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; void Pause () { //freezes screen; char junk; cout <<endl << endl << "Press some keys followed by the Enter key to continue..."; cin.ignore(); cin.get(junk); //end pause } int main() { //MAIN BODY //identify/open variables ifstream patient; patient.open("C:\\patient.dat"); if (patient) { cerr << "Input file found...\n\n"; } else { cerr << "Input files open FAILED..."; } //variables string firstName, lastName; int systolic; int diastolic; //read file //STD:STRING STUFF GOES HERE? //test output for name patient >> lastName >> firstName >> systolic >> diastolic; //Systolic readings if(systolic >= 160) cout << "Systolic Blood Pressure of " << systolic << " is Stage 2 Hypertensive. \n\n"; else if(systolic >=140) cout << "Systolic Blood Pressure of " << systolic << " is Stage 1 Hypertensive.\n\n"; else if(systolic >=120) cout << "Systolic Blood Pressure of " << systolic << " is Pre-hypertensive.\n\n"; else if(systolic <=119) cout << "Systolic Blood Pressure of " << systolic << " is Normal.\n\n"; //Diastolic Reading if(diastolic >= 100) cout << "Diastolic Blood Pressure of " << diastolic << " is Stage 2 Hypertensive. \n\n"; else if(diastolic >=90) cout << "Diastolic Blood Pressure of " << diastolic << " is Stage 1 Hypertensive.\n\n"; else if(diastolic >=80) cout << "Diastolic Blood Pressure of " << diastolic << " is Pre-hypertensive.\n\n"; else if(diastolic <=79) cout << "Diastolic Blood Pressure of " << diastolic << " is Normal.\n\n"; patient.close(); //Pause(); system("PAUSE"); return(0); } [/code] So, if i had this i would want to throw it in before i have the lines that output and calculate everything?
Yeah
[QUOTE=ZeekyHBomb;42980881]Yeah[/QUOTE] Thanks again for all the help. My teacher finally got back to me. Turns out he had a relatively simple solution. I just had char slashChar then patient >> lastName >> firstName >> systolic >> slashChar >> diastolic; Seems to work well.
I'm trying to make a simple virtual machine, but I don't know how the instruction system would work. If I had a script [code] PUSH 1000h ; random memory address POP ECX MOV EAX,[ECX+4+8] [/code] How would I actually compile that into bytecode that I can interpret in the virtual machine?
[QUOTE=ollie;42987224]I'm trying to make a simple virtual machine, but I don't know how the instruction system would work. If I had a script [code] PUSH 1000h ; random memory address POP ECX MOV EAX,[ECX+4+8] [/code] How would I actually compile that into bytecode that I can interpret in the virtual machine?[/QUOTE] Basically you need to invent an instruction encoding that your "processor" can understand. I only know MIPS encoding because it's incredibly simple, so my examples will all be on it. Here's a little on how MIPS encoding works: There's R format instructions and I format instructions. Both are contained in a 32 bit word, but I'll only cover the R format. R format instructions have the following data in it: OP code, RS, RT, RD, shamt, function. OP code is the kind of instruction that'll be ran. RS and RT are registers that each point to a location in memory. RD is the destination register, which will contain the memory location that'll have the result written to. shamt is the shift amount, which tells the processor how many bits to shift when using commands like sll or srl. function is mainly used to tell the processor more specifically what kind of op you're doing. All of this data is distributed in the 32 bits like so: oooo ooss ssss tttt tddd ddmm mmmf ffff Where o = op code, s = rs, t = rt, d = rd, m = shamt, and f = function. So for an instruction like add, $t0, $t1, $t2 would turn into: 000000 01000 01001 01010 00000 100000 The CPU, from this data, would know: The operation is arithmatic due to the opcode being 0 That we're adding due to the function code being 0x20 That rs and rt are registers 8 and 9. Which equate to $t0 and $t1 [url=http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm#RegisterDescription]based on this table.[/url] That rd is register 10/$t2, which is where we'll store $t0+$t1. You're not using MIPS, but the idea is essentially the same. Instead of $t0 and $t1 you have things like EAX and ECX. While functions would be PUSH, POP, and MOV. You can store constants in things like shamt. Or make several different format instructions so you can have larger constants (See I format instruction for mips.) So basically you need to know what kind of instructions you'd like to run, and how you want to store the data required to run all these different instructions inside of a consistent binary register. Then you can compile bytecode by parsing the source code using whatever means you want (even scanf and big switch statements if you want) into a file which will basically be all the different binary instructions in a row. Then you can write some virtual machine or interpreter that can read the compiled bytecode and you're done.
[QUOTE=Naelstrom;42987726]Basically you need to invent an instruction encoding that your "processor" can understand. I only know MIPS encoding because it's incredibly simple, so my examples will all be on it. Here's a little on how MIPS encoding works: There's R format instructions and I format instructions. Both are contained in a 32 bit word, but I'll only cover the R format. R format instructions have the following data in it: OP code, RS, RT, RD, shamt, function. OP code is the kind of instruction that'll be ran. RS and RT are registers that each point to a location in memory. RD is the destination register, which will contain the memory location that'll have the result written to. shamt is the shift amount, which tells the processor how many bits to shift when using commands like sll or srl. function is mainly used to tell the processor more specifically what kind of op you're doing. All of this data is distributed in the 32 bits like so: oooo ooss ssss tttt tddd ddmm mmmf ffff Where o = op code, s = rs, t = rt, d = rd, m = shamt, and f = function. So for an instruction like add, $t0, $t1, $t2 would turn into: 000000 01000 01001 01010 00000 100000 The CPU, from this data, would know: The operation is arithmatic due to the opcode being 0 That we're adding due to the function code being 0x20 That rs and rt are registers 8 and 9. Which equate to $t0 and $t1 [url=http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm#RegisterDescription]based on this table.[/url] That rd is register 10/$t2, which is where we'll store $t0+$t1. You're not using MIPS, but the idea is essentially the same. Instead of $t0 and $t1 you have things like EAX and ECX. While functions would be PUSH, POP, and MOV. You can store constants in things like shamt. Or make several different format instructions so you can have larger constants (See I format instruction for mips.) So basically you need to know what kind of instructions you'd like to run, and how you want to store the data required to run all these different instructions inside of a consistent binary register. Then you can compile bytecode by parsing the source code using whatever means you want (even scanf and big switch statements if you want) into a file which will basically be all the different binary instructions in a row. Then you can write some virtual machine or interpreter that can read the compiled bytecode and you're done.[/QUOTE] Thanks a lot for that, I still have one question though. When I have a script like this for example: [code] MOV EAX,100 MOV EAX,EBX MOV EAX,[ECX+100] [/code] The mnemonic MOV looks the same in all 3 cases, but I would actually implement 3 different opcodes to fit all those cases, constant to register, register to register, memory to register, right?
[QUOTE=ollie;42988092]Thanks a lot for that, I still have one question though. When I have a script like this for example: [code] MOV EAX,100 MOV EAX,EBX MOV EAX,[ECX+100] [/code] The mnemonic MOV looks the same in all 3 cases, but I would actually implement 3 different opcodes to fit all those cases, constant to register, register to register, memory to register, right?[/QUOTE] You got it. There's several ways you can solve the problem, actually. In mips you could use an I format instruction which gives you 16-bits for an immediate value and only takes two registers. If you have both registers and the immediate is 0, you know to move register to register. If one of the registers is 0 then you would know to just move the immediate value to register. Then if both registers exist and an immediate, you do memory to register. Or something along those lines.
Anyone have any good places to read up on loops? We have some loops to do for tomorrow night, and i would love to find something a bit more clear than my book. For example, one loop we have is to take a factorial of a number. I get what i need to do. I'll need to take the number, then subtract one each time until i get to 1, then multiply my remaining answer each time. So i would want to take say, 7, and get 5*4*3*2*1=120. I know the steps to get there, i'm just not sure how to throw them in a loop. I'm guessing i would want to start outside of the loop with a cin number, then subtract one from that (label it userNumber, maybe.) then once in the loop, subtract 1 from that number, until i get to 1, and multiply all of those numbers together. Any ideas?
Does anyone know what type of dithering GLES20.glEnable(GLES20.GL_DITHER); uses? The example they used [URL="http://www.matim-dev.com/dithering---improve-gradient-quality.html"]over here[/URL] ([URL="http://www.matim-dev.com/uploads/1/5/8/0/15804842/577775977.png"]pic[/URL]) seemed to be exactly what I needed to match some old sprites but I can't find what technique it uses and [URL="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205565(v=vs.85).aspx"]DirectX[/URL] uses Ordered Bayer 4x4 which does not match.
[QUOTE=JohnStamosFan;42992164]Anyone have any good places to read up on loops? We have some loops to do for tomorrow night, and i would love to find something a bit more clear than my book. For example, one loop we have is to take a factorial of a number. I get what i need to do. I'll need to take the number, then subtract one each time until i get to 1, then multiply my remaining answer each time. So i would want to take say, 7, and get 5*4*3*2*1=120. I know the steps to get there, i'm just not sure how to throw them in a loop. I'm guessing i would want to start outside of the loop with a cin number, then subtract one from that (label it userNumber, maybe.) then once in the loop, subtract 1 from that number, until i get to 1, and multiply all of those numbers together. Any ideas?[/QUOTE] I'm not exactly sure of what kind of information you seek, but loops are fairly simple things. You have a condition which is evaluated, if the condition evaluates to true, the steps in your loop are executed once, then the condition is evaluated again and the conditions are executed again. This keeps going on until the condition no longer evaluates to true. The loop is then finished.
[QUOTE=mobrockers;42992318]I'm not exactly sure of what kind of information you seek, but loops are fairly simple things. You have a condition which is evaluated, if the condition evaluates to true, the steps in your loop are executed once, then the condition is evaluated again and the conditions are executed again. This keeps going on until the condition no longer evaluates to true. The loop is then finished.[/QUOTE] Yeah, i dunno. My question was super vague. I'll re-read the chapter on loops and come back, i'm sure, with a ton of questions. I'll see if i can get my idea of how this should come together to form an actual program on my screen. OFF I GO!
Excuse the extremely sloppy movement system, I'm just testing it. I appear to have run into a problem. [code]if (Gdx.input.isKeyPressed(Keys.W)) { character.collMesh.setLinearVelocity(new Vector3(velocity.x,velocity.y,10)); } else if (Gdx.input.isKeyPressed(Keys.R)) { character.collMesh.setLinearVelocity(new Vector3(velocity.x,velocity.y,-10)); } else { character.collMesh.setLinearVelocity(new Vector3(velocity.x,velocity.y,0)); } if (Gdx.input.isKeyPressed(Keys.A)) { character.collMesh.setLinearVelocity(new Vector3(-10,velocity.y,velocity.z)); } else if (Gdx.input.isKeyPressed(Keys.S)) { character.collMesh.setLinearVelocity(new Vector3(10,velocity.y,velocity.z)); } else { character.collMesh.setLinearVelocity(new Vector3(0,velocity.y,velocity.z)); }[/code] With this I can move left and right, but I cannot move forward and backwards. Switching the z and y axis does nothing either. What am I doing wrong? This is using Bullet physics. Collisions appear to work fine, I just can't move forward or backwards. [editline].[/editline] Nevermind, got it.
In Java what is the best way to have 2 threads print out different strings 100 times and then finish? I tried using wait and notify but the program never ended, it went to 100 then just hung.
Could someone explain a little of the logic behind the coding of the factorial of large numbers? I need to be able to calculate the factorial of 300. I understand that each individual digit for the result needs to be placed in an array, which would be the easiest way to do this with the knowledge I've learned in this class. However, I'm having a hard time understanding the examples I've found online. I've spent a while looking them over, and I feel like I'm not even sure where to start. [editline]26th November 2013[/editline] Coding in C++
I have a folder with a bunch of pictures in it, and I want to load them all in without having to type out every name of every picture. I'm using the Standard Widget Toolkit for Java, I know how to load individual pictures and display them, but I don't know how, or if it's even possible, to load every picture from a directory in a more efficient way than typing each file name. I was thinking I could make an array with all the names and use a for loop, but that still involves typing all of the image names. Any help would be appreciated. Oh, and I want to load them in as ImageData objects, not Images, so I can re-size them before creating the actual Image objects.
[QUOTE=Zareox7;42994164]Could someone explain a little of the logic behind the coding of the factorial of large numbers? I need to be able to calculate the factorial of 300. I understand that each individual digit for the result needs to be placed in an array, which would be the easiest way to do this with the knowledge I've learned in this class. However, I'm having a hard time understanding the examples I've found online. I've spent a while looking them over, and I feel like I'm not even sure where to start. [editline]26th November 2013[/editline] Coding in C++[/QUOTE] Okay, I spent a little bit more time on some of the examples, but none of the examples ever work when I copy then into my IDE. I tried coding my representation of what they have, and it doesn't work logically. [cpp]#include <iostream> using namespace std; int main() { int ResultDigits, ArrayResult[300], Remainder, Factorial, ProductResult, counter; cout << "Enter a number for factorial calculation: "; cin >> Factorial; for (int ResultDigits=0;ResultDigits<=300;ResultDigits++){ ArrayResult[ResultDigits] = 0; } ArrayResult[300]=1; for (counter = 2; counter <= Factorial; counter++){ while (ResultDigits != 0){ ProductResult = ArrayResult[ResultDigits]*counter+Remainder; Remainder = 0; cout << ProductResult << endl; if (ProductResult > 9){ ArrayResult[ResultDigits] = ProductResult%10; Remainder = ProductResult/10; } else{ ArrayResult[ResultDigits] = ProductResult; } ResultDigits--; } Remainder=0; ProductResult=0; ResultDigits=300; } for (ResultDigits=0;ResultDigits <= 300; ResultDigits++){ if(ArrayResult[ResultDigits] != 0 || counter==1){ cout << ArrayResult[ResultDigits]; counter=1; } } return 0; }[/cpp] [editline]26th November 2013[/editline] I tested a bit of my code, and it seems to be completely skipping my first for loop. That was the Cout you see inside my while statement was testing if it got that far. I dropped a cout into every loop to see which happened, and it turns out it goes from ArrayResult[300]=1 straight to the last for loop. [editline]26th November 2013[/editline] My result seems to be always 1.
[QUOTE=laharlsblade;42994258]I have a folder with a bunch of pictures in it, and I want to load them all in without having to type out every name of every picture. I'm using the Standard Widget Toolkit for Java, I know how to load individual pictures and display them, but I don't know how, or if it's even possible, to load every picture from a directory in a more efficient way than typing each file name. I was thinking I could make an array with all the names and use a for loop, but that still involves typing all of the image names. Any help would be appreciated. Oh, and I want to load them in as ImageData objects, not Images, so I can re-size them before creating the actual Image objects.[/QUOTE] [cpp] File folder = new File("path"); File[] fileNames = folder.listFiles(); [/cpp]
I'm trying to create an options screen for my text-based game, but I've come across an obstacle. Heres what I'm basically doing. I want to make the Options screen call the Sound options screen. Options -> Sound Options But, I also want a back option so I can get the player back to the original options screen. Options <- Sound Options Although, if you know C++, you'll know that you can't call functions that have been written after the current function. Any ideas, guise?
[url]http://www.cplusplus.com/doc/tutorial/functions2/#declaring[/url]
[QUOTE=SteelSliver;42995574]I'm trying to create an options screen for my text-based game, but I've come across an obstacle. Heres what I'm basically doing. I want to make the Options screen call the Sound options screen. Options -> Sound Options But, I also want a back option so I can get the player back to the original options screen. Options <- Sound Options Although, if you know C++, you'll know that you can't call functions that have been written after the current function. Any ideas, guise?[/QUOTE] [url=http://www.learncpp.com/cpp-tutorial/17-forward-declarations/]forward declare[/url] them.
Why couldn't I figure that out? I'm so es stupido!
I'm having problems with Game Maker. I recently learned some basics of GML, which is similiar to C++. So I put the code here - if keyboard_check_pressed(ord('W')) then sprite_index = explorer_up if keyboard_check_pressed(ord('S')) then sprite_index = explorer_down if keyboard_check_pressed(ord('A')) then sprite_index = explorer_left if keyboard_check_pressed(ord('D')) then sprite_index = explorer_up It's for sprite changes while character is moving. I'm still practicing and working on it, any help would be greatly appreciated. P.S: If someone knows good wall-stop system (char is getting stuck on curves) that'd be also great.
Wow, apparently printing random hex values to the console can make your computer beep. I should probably be worried.
[QUOTE=ollie;42997257]Wow, apparently printing random hex values to the console can make your computer beep. I should probably be worried.[/QUOTE] beeps are fun. I remember when I discovered the beep() function in C++. Then I made a program which was a piano with beeps. It wasn't on my computer so hopefully that isn't harmful to do alot :L
[QUOTE=ollie;42997257]Wow, apparently printing random hex values to the console can make your computer beep. I should probably be worried.[/QUOTE] ASCII value 7 represents a beep.
[QUOTE=Over-Run;42993691]In Java what is the best way to have 2 threads print out different strings 100 times and then finish? I tried using wait and notify but the program never ended, it went to 100 then just hung.[/QUOTE] Just count to 100 in a loop for both threads and print stuff. [QUOTE=Zareox7;42994545]Okay, I spent a little bit more time on some of the examples, but none of the examples ever work when I copy then into my IDE. I tried coding my representation of what they have, and it doesn't work logically. [cpp]//code[/cpp] [editline]26th November 2013[/editline] I tested a bit of my code, and it seems to be completely skipping my first for loop. That was the Cout you see inside my while statement was testing if it got that far. I dropped a cout into every loop to see which happened, and it turns out it goes from ArrayResult[300]=1 straight to the last for loop. [editline]26th November 2013[/editline] My result seems to be always 1.[/QUOTE] Didn't review your logic, but after fixing some undefined behaviors it seems to work. Integers in C++ are not initialized to a known value, you must assign something to them before using them. Further more, when you have an array of size 300, valid indices are in 0..299; 300 is out of bounds. It's also considered good to declare variables as late as possible, but as early as necessary. Here's a version of your code with does both. Also note that I initialize the array with {}, which removed the need of the loop to set all elements to 0. [code]#include <iostream> using namespace std; int main() { cout << "Enter a number for factorial calculation: "; int Factorial; cin >> Factorial; int ArrayResult[300]={}; ArrayResult[299]=1; int Remainder=0; for (int counter = 2; counter <= Factorial; counter++){ for (int ResultDigits = 299; ResultDigits != 0; ResultDigits--){ int ProductResult = ArrayResult[ResultDigits]*counter+Remainder; Remainder = 0; cout << ProductResult << endl; if (ProductResult > 9){ ArrayResult[ResultDigits] = ProductResult%10; Remainder = ProductResult/10; } else{ ArrayResult[ResultDigits] = ProductResult; } } } bool count=false; for (int ResultDigits=0;ResultDigits < 300; ResultDigits++){ if(ArrayResult[ResultDigits] != 0 || count){ cout << ArrayResult[ResultDigits]; count=true; } } return 0; }[/code] [QUOTE=ollie;42997257]Wow, apparently printing random hex values to the console can make your computer beep. I should probably be worried.[/QUOTE] Well, for one there's \a, the bell character. [QUOTE=Duskling;42997532]beeps are fun. I remember when I discovered the beep() function in C++. Then I made a program which was a piano with beeps. It wasn't on my computer so hopefully that isn't harmful to do alot :L[/QUOTE] Well, that probably caused some minor wear on the speaker. [editline]27th November 2013[/editline] [QUOTE=JTay;42995762]I'm having problems with Game Maker. I recently learned some basics of GML, which is similiar to C++. So I put the code here - if keyboard_check_pressed(ord('W')) then sprite_index = explorer_up if keyboard_check_pressed(ord('S')) then sprite_index = explorer_down if keyboard_check_pressed(ord('A')) then sprite_index = explorer_left if keyboard_check_pressed(ord('D')) then sprite_index = explorer_up It's for sprite changes while character is moving. I'm still practicing and working on it, any help would be greatly appreciated. P.S: If someone knows good wall-stop system (char is getting stuck on curves) that'd be also great.[/QUOTE] Depends on your definition of "similar", but it doesn't really matter. This is not solely a C++ or C++-like languages help thread :) What's wrong with the code? If you just want constructive criticism on that tiny snippet, you should probably do the keyboard_check_pressed check for each key once and store the result in a more descriptive boolean variable. And you should make plans to allow the user to re-bind those actions to other keys. "wall-stop system" is usually called collision detection/handling/avoidance. [url=http://gamemaker.info/en/manual/403_03_planning]Here[/url]'s an official (?) resource on that topic.
Just started programming in C++, I am only used to java tho, so I don't really understand what is wrong here. [code] #include "mainwindow.h" #include "ui_mainwindow.h" #include "QRect" #include "QDesktopWidget" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QRect position = frameGeometry(); position.moveCenter(QDesktopWidget().availableGeometry().center()); move(position.topLeft()); ui->console->addItem("Starting program"); consoleAdd("Moving window to center"); } MainWindow::~MainWindow() { delete ui; } void consoleAdd(String s){ ui->console->addItem(s); } [/code] Basically it tells me the method consoleAdd(String s) is wrong, it is not declared in the scope and apparently setting it void is also wrong. Tried to google the scope error but only found entries which are used to add new objects.
Sorry, you need to Log In to post a reply to this thread.