[QUOTE=WeltEnSTurm;31743948]You can execute strings as Lua with luaL_dostring.[/QUOTE]
Though I want my code clean, and separate the Lua code from the C++ source.
#define LUA_INCLUDEFILE(s,x) luaL_dostring(state, #include "x")
Will most likely not work as-is, but you get the idea.
[QUOTE=WeltEnSTurm;31744101]#define LUA_INCLUDEFILE(s,x) luaL_dostring(state, #include "x")
Will most likely not work as-is, but you get the idea.[/QUOTE]
Hmm, I could probably do something like that, yeah.
I'm trying to make a VERY basic converter, and I'm trying to make it convert 1 type to another without having a button. What am I doing wrong?
[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
double feet = 0;
double inches = 0;
double farenheit = 0;
double miles = 0;
double pounds = 0;
double tons = 0;
double meters = 0;
double centimeters = 0;
double celsius = 0;
double kilometers = 0;
double ounces = 0;
double metricTon = 0;
private void feetTextBox_TextChanged(object sender, EventArgs e)
{
feet = double.Parse(feetTextBox.Text);
meters = feet + 2;
feetTextBox.Text = feet.ToString();
}
private void metersTextBox_TextChanged(object sender, EventArgs e)
{
meters = double.Parse(metersTextBox.Text);
feet = meters + 2;
metersTextBox.Text = meters.ToString();
}
}
[/code]
[editline]15th August 2011[/editline]
Also, how would I deal with exception handling with something like this? If you couldn't tell, I'm rather new to C#.
[code]
void Palindrome::inputString()
{
char * input = new char[];
cin >> input;
userInput = input;
cout << userInput << endl;
delete[] input;
}
[/code]
I keep getting heap corruption from using delete[]. Am I using this wrong, or how do I fix it?
[QUOTE=masonrulz;31744369]I'm trying to make a VERY basic converter, and I'm trying to make it convert 1 type to another without having a button. What am I doing wrong?
Also, how would I deal with exception handling with something like this? If you couldn't tell, I'm rather new to C#.[/QUOTE]
To do proper exception handling when converting doubles, ints, etc you need to use the double.TryParse method.
It generally takes a string and a variable that is defined as an output, and it also returns a bool that says whether or not the conversion was successful.
so
double meters;
if(double.TryParse(metersString,out meters))
{
//the conversion was successful so do whatever you want with meters now
}
[QUOTE=RiceWarrior;31744663][code]
void Palindrome::inputString()
{
char * input = new char[];
cin >> input;
userInput = input;
cout << userInput << endl;
delete[] input;
}
[/code]
I keep getting heap corruption from using delete[]. Am I using this wrong, or how do I fix it?[/QUOTE]
You have to define the array size you want to allocate with new.
[QUOTE=CriticalImpact;31744666]To do proper exception handling when converting doubles, ints, etc you need to use the double.TryParse method.
It generally takes a string and a variable that is defined as an output, and it also returns a bool that says whether or not the conversion was successful.
so
double meters;
if(double.TryParse(metersString,out meters))
{
//the conversion was successful so do whatever you want with meters now
}[/QUOTE]
Thanks a ton, that worked like a charm. I'm really new to C#, so any help I can get is really appreciated. How would I make it so a part of my program gets executed when the text in a textbox changes?
[QUOTE=sim642;31744716]You have to define the array size you want to allocate with new.[/QUOTE]
Is there a way to make it so that the array size is defined by the input? That's the reason I didn't define it in the first place so that the size is determined through the input.
EDIT:
Or is it a bad idea to just leave it allocated and not delete it?
Use std::string.
[QUOTE=ZeekyHBomb;31745216]Use std::string.[/QUOTE]
I guess it would be much, much easier to just use string. But I was pretty much solely using arrays and pointers to help myself understand them better. But considering how string has all the functions I need, I guess it'd be easier to just use that versus writing out all these functions for scanning arrays myself.
When I try to compile even a sfml example, the vsc++2010 complains about buffer overrun, nor works with -d libs, but if I use CodeBlocks and set up the project for sfml, it works as it should, I guess I should stick with CodeBlocks now.
[QUOTE=masonrulz;31744369]I'm trying to make a VERY basic converter, and I'm trying to make it convert 1 type to another without having a button. What am I doing wrong?
[code]
-code snip-
[/code]
[editline]15th August 2011[/editline]
Also, how would I deal with exception handling with something like this? If you couldn't tell, I'm rather new to C#.[/QUOTE]
I already answered this 2 pages ago:
[QUOTE=robmaister12;31738041]
Make sure that those methods are added as event callbacks and not just written in. In the winforms editor, click one of the textboxes then, in the properties panel, click the lightning bolt icon. Scroll down to TextChanged (or something very similar to that) and select the proper method for that textbox.
You can also do this manually (not really recommended if you're already using winforms to handle everything for you, but just so you know...) in the constructor with: metersTextBox.TextChanged += metersTextBox_TextChanged;. When you type in the += you can also hit Tab twice and it will write in the rest of the line for you and generate a new method block for the event.
Note that if you're going to write a new method for an event, you can just select the TextChanged event in the properties panel and double click it, it'll generate the method block for you instead of you having to write it yourself.
And about the exceptions, wrap whatever code that may throw an exception in a try {} block. To catch exceptions from a try block, add a catch {} block. Note that you can also catch certain types of exceptions: try { ... } catch(ArgumentException e) { Console.WriteLine(e.Message); }
For the double parsing that you're doing (I'm assuming you want to catch the parsing exceptions), you can also use double.TryParse. It won't throw an exception, just return false if it can't parse. So you wrap that method in an if statement. The last parameter needs to be written as "out myDouble" instead of just "myDouble"[/QUOTE]
[QUOTE=RiceWarrior;31745736]I guess it would be much, much easier to just use string. But I was pretty much solely using arrays and pointers to help myself understand them better. But considering how string has all the functions I need, I guess it'd be easier to just use that versus writing out all these functions for scanning arrays myself.[/QUOTE]
If you want to work with pointers, try doing linked list and tree stuff.
[QUOTE=robmaister12;31747362]I already answered this 2 pages ago:[/QUOTE]
Thanks, I got the program up and fully working;
[img]http://i.imgur.com/FP6Cy.png[/img]
I want to know if I am forming some bad C++ techniques.
This is the most advanced thing that I have created so far, which isn't saying much. I just want to make sure that I'm not doing something that should be done differently.
[CODE]
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int theNumber = 58;
int playerGuess;
cout << "Try to guess the number. \n";
cout << "Type in a number between 1 and 100. ";
cin >> playerGuess;
if (playerGuess == theNumber)
{
cout << "You're correct, congratulations! \n";
cout << "You're correct, congratulations! \n";
cout << "You're correct, congratulations! \n";
cout << "You're correct, congratulations! \n";
}
else
if (playerGuess < theNumber)
{
cout << "You're wrong, go higher! \n";
cout << "You're wrong, go higher! \n";
cout << "You're wrong, go higher! \n";
cout << "You're wrong, go higher! \n";
}
else
if (playerGuess > theNumber)
{
cout << "You're wrong, go lower! \n";
cout << "You're wrong, go lower! \n";
cout << "You're wrong, go lower! \n";
cout << "You're wrong, go lower! \n";
}
char f;
cin >> f;
return 0;
}
[/CODE]
I was going to try and make this create random numbers, so it wouldn't just be 58, but it seems to be a bit harder than what I'm doing right now.
You can do random numbers pretty easily. The simplest way is to do include <time.h>, call
[cpp]srand(time(0));[/cpp]
at the top of main, then
[cpp]rand() % MAX[/cpp]
wherever you need a random number, with the number being between 0 and MAX - 1. If you want a number
from 1-100 for example, you could do:
[cpp]int theNumber = rand() % 100 + 1;[/cpp]
[quote]if (playerGuess > theNumber)[/quote]
This if isn't necessary since the only other possibility is for the guess to be lower, so there's no need to check for it.
Why are you doing a cout 4 times?
One last thing: You might consider looking into while loops. That way the user can guess more than one number.
[QUOTE=jalb;31752579]You can do random numbers pretty easily. The simplest way is to do include <time.h>, call
[cpp]srand(time(0));[/cpp]
at the top of main, then
[cpp]rand() % MAX[/cpp]
wherever you need a random number, with the number being between 0 and MAX - 1. If you want a number
from 1-100 for example, you could do:
[cpp]int theNumber = rand() % 100 + 1;[/cpp]
This if isn't necessary since the only other possibility is for the guess to be lower, so there's no need to check for it.
Why are you doing a cout 4 times?
One last thing: You might consider looking into while loops. That way the user can guess more than one number.[/QUOTE]
Alright, thank you, I'll be sure to try this.
I was using cout 4 times simply to try the curly brackets, letting more than one line print.
By while loops, do you basically mean restarting the program? (Try Again?)
I just corrected everything, and changed theNumber to give a random number. It works!
[CODE]
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand(time(0));
int theNumber = rand() % 100 + 1;
int playerGuess;
cout << "Try to guess the number. \n";
cout << "Type in a number between 1 and 100. ";
cin >> playerGuess;
if (playerGuess == theNumber)
{
cout << "You're correct, congratulations! \n";
cout << "You're correct, congratulations! \n";
cout << "You're correct, congratulations! \n";
cout << "You're correct, congratulations! \n";
}
else
if (playerGuess < theNumber)
{
cout << "You're wrong, go higher! \n";
cout << "You're wrong, go higher! \n";
cout << "You're wrong, go higher! \n";
cout << "You're wrong, go higher! \n";
}
else
{
cout << "You're wrong, go lower! \n";
cout << "You're wrong, go lower! \n";
cout << "You're wrong, go lower! \n";
cout << "You're wrong, go lower! \n";
}
char f;
cin >> f;
return 0;
}
[/CODE]
Thank you!
[QUOTE=Grabigel;31751966][CODE]
char f;
cin >> f;
[/CODE]
[/QUOTE]
You can do cin.get() and not have to create the char
[QUOTE=Richy19;31753310]You can do cin.get() and not have to create the char[/QUOTE]
I was told that since I am using cin >> playerGuess; I have to use char f; along with cin >> f; instead of cin.get();
Is that true?
[QUOTE=Grabigel;31753918]I was told that since I am using cin >> playerGuess; I have to use char f; along with cin >> f; instead of cin.get();
Is that true?[/QUOTE]
only if you want to store the input, but at the end of main your just allowing the player to read the screen so you can use get()
I'm about to tear my hair out trying to write this program that identifies palindromes (words that are the same backwards as forwards) and prints them.
I've pretty much devised a way to print the palindromes, but I'm having trouble figuring out how to change a bool to print only when it knows for a fact the given string is a palindrome.
Any thoughts on how to effectively identify strings or words like rotator or aaddaa as palindromes?
[QUOTE=RiceWarrior;31756671]I'm about to tear my hair out trying to write this program that identifies palindromes (words that are the same backwards as forwards) and prints them.
I've pretty much devised a way to print the palindromes, but I'm having trouble figuring out how to change a bool to print only when it knows for a fact the given string is a palindrome.
Any thoughts on how to effectively identify strings or words like rotator or aaddaa as palindromes?[/QUOTE]
if(reverse(string1) == string1)
{
// is a palindrome
}
[QUOTE=Jookia;31756711]if(reverse(string1) == string1)
{
// is a palindrome
}[/QUOTE]
Ugh, I feel like a moron now. But that will make things a lot easier thanks.
[QUOTE=RiceWarrior;31756671]I'm about to tear my hair out trying to write this program that identifies palindromes (words that are the same backwards as forwards) and prints them.
I've pretty much devised a way to print the palindromes, but I'm having trouble figuring out how to change a bool to print only when it knows for a fact the given string is a palindrome.
Any thoughts on how to effectively identify strings or words like rotator or aaddaa as palindromes?[/QUOTE]
What I did (when I did this) was to create two pointers - one to point to the start of the string, and one to point to the end. Have a for loop running which checks to see if the letters are the same.
Example (letters highlighted in bold is what the pointer is pointing to):
[b]R[/b]ACECA[b]R[/b]
R == R so yes, continue the loop.
R[b]A[/b]CEC[b]A[/b]R
A == A so yes, continue the loop.
Keep doing this till the two pointers are pointing to the same characters or whatever. :smile:
If the two chars are not the same, break the loop and return false (assuming you've implemented this as a function which returns true or false).
That's essentially what I did to determine the palindrome (only inverse, starting from middle out), except I'm having a separate function move the pointers from left to right and scanning as it goes and verifying if a palindrome is existing, printing it, and moving on. I'm pretty much trying to get it to return every instance of a palindrome within a string. However, I think factoring this in is a bit too much for me, considering I'm still a pretty noob programmer.
EDIT:
It's all a bit of a mindfuck to me right now, I think I'm going to try and finish this tomorrow morning.
[cpp]#include <string>
#include <iostream>
using namespace std;
bool isPalindrome(string text)
{
string::iterator forward = text.begin();
string::reverse_iterator back = text.rbegin();
do
{
if(*forward != *back)
{
return false;
}
++forward;
++back;
}
while(forward != text.end());
return true;
}
int main(void)
{
cout << "racecar: " << isPalindrome("racecar") << endl
<< "noimnot: " << isPalindrome("noimnot") << endl;
return 0;
}[/cpp]
Sorry for the spoilers, but I made that. It's hilariously unoptimized for learning purposes.
[editline]16th August 2011[/editline]
[cpp]#include <stdio.h>
#include <string.h>
int is_palindrome(const char* text)
{
/* Set the forward and backward iterators to outside
the string as they move in the first iteration. */
const char* forward = &text[0] - 1;
const char* backward = &text[strlen(text)];
/* Loop until the iterator has passed the middle,
or until the characters don't match. */
while(!(forward > backward || *(++forward) != *(--backward)));
/* If the middle has been passed, that means that all of the
characters up to (and including) the middle match.
Thus it's a palindrome. */
return (forward >= backward);
}
int main(void)
{
printf("racecar: %i\nnoimnot: %i\n",
is_palindrome("racecar"),
is_palindrome("noimnot"));
return 0;
}[/cpp]
Just for fun I made a heavily optimized one in C.
[QUOTE=Jimbomcb;31743630]Was thinking about a byte or integer or something at the start of the packet but wasn't sure if it was a bad thing to do for reasons beyond me.[/QUOTE]
Make sure you don't send() the ID byte/int and then send() the actual data. This [i]may[/i] cause two packets to be sent so you'd be better off buffering and send()'ing once
[editline]16th August 2011[/editline]
[QUOTE=Jookia;31757293]Just for fun I made a heavily optimized one in C.[/QUOTE]
heavily optimized
While I do enjoy being riffed on, it is heavily optimized in comparison.
[QUOTE=Chrispy_645;31756972]What I did (when I did this) was to create two pointers - one to point to the start of the string, and one to point to the end. Have a for loop running which checks to see if the letters are the same.
Example (letters highlighted in bold is what the pointer is pointing to):
[b]R[/b]ACECA[b]R[/b]
R == R so yes, continue the loop.
R[b]A[/b]CEC[b]A[/b]R
A == A so yes, continue the loop.
Keep doing this till the two pointers are pointing to the same characters or whatever. :smile:
If the two chars are not the same, break the loop and return false (assuming you've implemented this as a function which returns true or false).[/QUOTE]
Keep doing it until the first pointer is higher than or equal the second instead of only until they are equal, because otherwise it will loop forever on words with an even amount of characters :v:
Sorry, you need to Log In to post a reply to this thread.