I didn't look closely, but my first thought is to try std::vector<std::string> - is there a reason you need it to be std::vector<char*> ?
Dave
I didn't look closely, but my first thought is to try std::vector<std::string> - is there a reason you need it to be std::vector<char*> ?
Dave
Can you show us your readin function and then the function that is not retrieving the correct values?
It is very unlikely that someone is going to look through your 300 lines of code. Please whittle the code down to a ~20 line compilable example of the problem, along with sample input, expected output, and current (incorrect) output.
Dave
With reinterpret_cast it compiles, but the output is garbage.
What do you mean an overflow test? I'm not concerned about the loss of precision - 1.2 = 1.2 as a float or a double :)
Dave
Consider these functions:
void OperateOnDoublePointer(double* a)
{
std::cout << a[0] << std::endl;
}
void OperateOnFloatPointer(float* a)
{
std::cout << a[0] << std::endl;
}
This works as expected:
double* c = new double[1];
c[0] = 3.3;
OperateOnDoublePointer(c);
but when I try to do this:
double* c = new double[1];
c[0] = 3.3;
OperateOnFloatPointer(c);
It complains that it can't convert double* to float*.
I tried to tell it that was ok:
OperateOnFloatPointer(static_cast<float*>(c));
but it said can't cast double* to float*.
If I do this, it works:
float* d = new float[1];
d[0] = c[0];
OperateOnFloatPointer(d);
but I'd have to loop over all elements. Is there a single line way to do this (pass a float* to a function expecting a double*, or visc versa)?
Thanks,
Dave
To delete an element from anywhere, you'll want to use a linked list (std::list) instead of an array. Google "data structures" and you should find some overviews of when/why to use lists/maps/vectors/queues/stacks. Each has advantages and disadvantages.
Hope that helps,
Dave
Ah, there is a missing bracket:
for(int i=0;i<10;i++)
{
if (a[i]<10)
small+=a[i];
else
big+=a[i];
cout<<big;
}
should be:
for(int i=0;i<10;i++)
{
if (a[i]<10)
small+=a[i];
else
{
big+=a[i];
cout<<big;
}
}
That's why I thought it was a nested loop.
Dave
You need to change
int a[]= new a[10];
to this:
int* a = new int[10];
also, you have "i" as the counter variable in both the outer loop and inner loop, that is definitely going to cause problems.
Dave
If you don't want to do your homework, why would we? Give it your best shot and someone will help you if you get stuck on a particular problem.
You're right, compilers do ignore white space, but they do not ignore symbols - I think it interprets >> as the instream operator.
vector<pair<double,double>> *point; //points = x,y respectivly
produces
error: '>>' should be '> >' within a nested template argument list
on g++4.4.1.
Just because it works on your compiler doesn't mean it is right.
Dave
First, you need to put a space between the two angle brackets:
vector<pair<double,double> > *point; //points = x,y respectivly
instead of
vector<pair<double,double>> *point; //points = x,y respectivly
It should not only compile (which it doesn't):
(.text+0x18): undefined reference to `main'
but also have a simple use case - an example where we can compile, run, and see the error.
Can you post a compilable example of your problem?
Can you post a bit more code (the shortest compilable example)? Personally, I anytime I see **, I think there is a better way to go. Maybe you could use an std::vector<int*> or something like that?
In general, you should post only code relevant to the question you are asking. For example, here you basically just want to know how to read words from a file, and that has nothing to do with hangman.
This is how I would do it:
std::ifstream fin(Filename.c_str());
if(fin == NULL)
std::cout << "Cannot open file." << std::endl;
std::vector<std::string> Lines;
std::string line;
while(getline(fin, line))
{
Lines.push_back(line);
}
for(unsigned int i = 0; i < Lines.size(); i++)
{
std::cout << Lines[i] << std::endl;
}
Dave
Can you post a compilable example of your problem?
Ok, interesting. I'll definitely look into XML parsing and using Beautiful Soup.
Thank all,
Dave
By putting an output statement like this:
void displayCard(const Card c)
{
cout << "c.f -2 : " << c.f -2 << " , " << " c.s - 1 : " << c.s -1 << endl;
cout << faceNames[c.f - 2] << " of " << suitNames[c.s - 1];
}
You can see that your indices are out of range:
c.f -2 : 122 , c.s - 1 : 2
*** Process aborted. Segmentation fault ***
Also, you should not use these 1 letter variable names ("f" and "s"). It makes the code very hard to read.
Dave
You should definitely use std::vector instead of basic arrays.
No one is going to help you unless you show that you have tried yourself first.
Dave
Is this what you're looking for?
#include <iostream>
#include <map>
int main(int argc, char *argv[])
{
std::multimap <int, double> MyMap;
//create a mapping from "testone" to 111
MyMap.insert(std::pair<int, double>(1, 1.2));
//create an iterator
std::map<int, double>::iterator iter;
iter = MyMap.find(1);
if(iter == MyMap.end())
{
std::cout << "Not found." << std::endl;
}
else
{
std::cout << "Found: " << iter->second << std::endl;
}
return 0;
}
Dave
Please use code tags when you post code.
It looks like you need to change this:
void myfunc(char scrpt[i])
to this:
void myfunc(char* scrpt)
and this:
myfunc(scrpt);
to this:
myfunc(&scrpt);
Dave
So you WANT to wrap around? Or you don't?
Please post your code, input, expected output, and current (incorrect) output.
Dave
Cool, thanks.
That didn't work right out of the box (why would the index be -1?) but it got me closer to the solution:
MyString = "VTK/Examples/Test"
PathSplit = os.path.split(MyString)
PathName = PathSplit[0]
ExampleName = PathSplit[1]
However, what I'm really trying to do is get the string that comes after "VTK/Examples/" even if there is more of a path, i.e. if the string is
VTK/Examples/IO/Python/Test
I want to know that yes, it is in VTK/Examples, and then that it's path is IO/Python/Test
Can os.path do something like this?
Thanks!
Dave
I am trying to strip a prefix from a string, e.g. turn
VTK/Examples/Test
Into
Test
Sometimes the string does not contain the prefix, in which case I would expect nothing to happen. However,
>>> S="Main Page"
>>> S.strip("VTK/Examples/")
'Main Pag'
You can see that the 'e' on the end of "Main Page" has been removed.
I even tried to escape the slashes
>>> S.strip("VTK\/Examples\/")
'Main Pag'
but it still strips the 'e'. Can anyone explain this behavior?
Thanks,
Dave
Please boil your question down to a 20 line, compilable piece of code that we can help you with. If you don't want to do your homework, why would we??
Dave
In SubAbstractSort.cpp, you need to change
void sort(int*arr, int size){
to
void SubAbstractSort::sort(int*arr, int size){
You also need to define the implementation in SubAbstractSort.h
class SubAbstractSort: public AbstractSort {
public:
SubAbstractSort();
int getNumComparisons();
virtual ~SubAbstractSort();
void sort(int*arr, int size);
};
Hope that helps,
Dave
It works fine for me:
std::string MyString = "hello,world123";
std::cout << "Original: " << MyString << std::endl;
//Remove all punctuation
MyString.erase(
std::remove_if(MyString.begin(), MyString.end(), &ispunct),
MyString.end());
std::cout << "Punctuation removed: " << MyString << std::endl;
//Remove all numbers
MyString.erase(
std::remove_if(MyString.begin(), MyString.end(), &isdigit),
MyString.end());
std::cout << "Numbers removed: " << MyString << std::endl;
the end result is simply
helloworld
Dave
Here is an example of how to use a struct:
#include <iostream>
struct Card
{
int id;
};
int main (int argc, char *argv[])
{
Card c;
c.id = 3;
return 0;
}
It is exactly like a class, but the default member level is public.
I think here your struct should be called Card instead of Cards - then when you have an array of a Card object, that is a bunch of "cards".
Dave
People are not going to do your homework for you. Please figure out a concise statement of a problem you are having and you'll get a lot more help here.
Dave
You'll have to forgive me, I'm pretty new to Python. How would I traverse the string to find the starting tag?
Then, I can add things to a list:
list.append(6)
but how do I get characters/words/strings from the big string in a sequential fashion to add them to this list?
Thanks,
Dave
Sounds like that will definitely help down the road, but it still didn't work in this case:
>>> import re
>>> MyStr = "<test>some text here</test> <other> more text </other> <test> even more text</test>"
>>> m=re.compile('<test>(.*?)</test>', re.DOTALL).search(MyStr)
>>> print m.group(1)
some text here
>>> print m.group(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: no such group
Dave
I am trying to parse the content of a wiki page.
In a string like this:
==Heading1==
<test>
some text here
</test>
==Heading2==
<test>
even more text
</test>
I need to obtain "Heading1", "some text here", "Heading2" and "even more text".
I got this to work:
import re
MyStr = "<test>some text here</test>
m=re.compile('<test>(.*?)</test>').search(MyStr)
print m.group(1)
it produces "some text here".
But I tried this:
MyStr = "==some text here=="
m=re.compile('<test>(.*?)</test>').search(MyStr)
print m.group(1)
and it had an error.
I also tried this:
MyStr = "<test>some text here</test> <other> more text </other> <test> even more text</test>"
m=re.compile('<test>(.*?)</test>').search(MyStr)
print m.group(1)
print m.group(2)
and it had an error getting group(2) (which I was hoping was the second occurrence of the matching string?)
Can anyone point me in the right direction?
Thanks,
Dave
I'm not sure which grid/cells you are talking about?
To your title, here is how to draw text in opengl:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <GL/glut.h>
using namespace std;
void display(void);
void polygon(int a, int b, int c , int d);
void DrawCube();
int WindowHeight = 1000;
int WindowWidth = 1000;
void printtext(int x, int y, string String)
{
//(x,y) is from the bottom left of the window
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, WindowWidth, 0, WindowHeight, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
glRasterPos2i(x,y);
for (int i=0; i<String.size(); i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, String[i]);
}
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WindowWidth, WindowHeight);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Text Example");
glutDisplayFunc(display);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 1, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2, 2, 10, 2, 0, 0, 0, 1, 0);
glutMainLoop();
return 0;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
char string[64];
sprintf(string, "something");
printtext(10,10,string);
glutSwapBuffers();
}
I recommend simplifying the problem - definitely remove all of the user input and hard code some values that you want to write. 130 lines of code is very hard to debug. 20 is much more manageable.
Dave
Right, but no one else will learn anything by learning how to speed up your player - on a forum like this, ideally (at least in my opinion) the problem should be abstracted as far as possible so that it will help in as many cases as possible once it gets to the archives. So my point is, if someone helps you figure out how to get the hold down time of a key (a general thing), then it would be up to you to apply it to your specific problem (moving the player).
See what I mean?
Dave
If you want to use the enums in the other functions, they need to be global:
enum MAKERS {FORD,BMW,VOLVO,CHEVY,CADILLAC,HYUNDAI};
enum COLORS {RED,BROWN,BLACK,WHITE,GREY};
int main () {
What are these lines supposed to do?
MAKERS = maker;
COLORS = color;
Here is a simple example of using enums:
#include <iostream>
enum PointType { GOOD, BAD, UNINFORMATIVE };
int main(int argc, char *argv[])
{
PointType A = GOOD;
if(A == GOOD)
std::cout << "good" << std::endl;
else
std::cout << "not good" << std::endl;
if(A == BAD)
std::cout << "not working" << std::endl;
else
std::cout << "working" << std::endl;
return 0;
}
It demonstrates how create a variable of the enum type as well as perform some comparisons.
Hope that helps,
Dave
I would suggest keeping your questions and related code as short and sweet as possible. For example, in this case, all you want to know is how to tell how long a key has been held down for, correct?
Unfortunately I don't know how to do that! (sorry)
Dave
On line 142 you are trying to assign something to getOwner. I don't see a variable called getOwner declared in function newg() or as a member of class ReversiGame, so that is likely where your problem lies.
Dave
You mean you want a user to login to your program? A simple string input and compare should do the trick, unless you are looking for something actually secure. You need to let us know more details about what you're trying to do.
Dave
Can you describe what you did to figure it out since you marked the thread as solved?
Dave
Sorry, I only work with c++ - I believe there is a separate subforum for c programming.
Dave
I would get the whole line and then split it at the spaces.
This is a pretty fancy way I found, but it works:
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
int main(int argc, char *argv[])
{
std::string sentence = "hello world test 1 2 3";
std::istringstream iss(sentence);
std::vector<std::string> words;
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter<std::vector<std::string> >(words));
for(unsigned int i = 0; i < words.size(); i++)
{
std::cout << words[i] << std::endl;
}
return 0;
}
Dave
I would do it like this:
std::ifstream fin(Filename.c_str());
if(fin == NULL)
std::cout << "Cannot open file." << std::endl;
std::vector<std::string> Lines;
std::string line;
while(getline(fin, line))
{
Lines.push_back(line);
}
for(unsigned int i = 0; i < Lines.size(); i++)
{
std::cout << Lines[i] << std::endl;
}
Dave
Add a
cout << "I received a packet from Node " << i << "\n";
right in front of where you do
f << "I received a packet from Node " << i << "\n";
If you see the output to the terminal, then the file is definitely being written. My guess is you are looking in the wrong place for the output files? They will probably be in the "bin" directory - or wherever the executable is being created.
Also, NEVER use 'goto'. Ever!
Dave
Sorry, my fault - you need to do:
f.open (ssFileName.str().c_str());
Let me know if that doesn't work.
Dave
This doesn't really seem like a c++ question to me.
Dave
If the error happens at runtime (as you say "when the balanced() function calls stack.push"), then it is not a linker error. But the errors you posted ARE linker errors. I don't use visual studio, so unfortunately I can't tell you how to tell it that you want to link to all of the object files generated by all of your source files. Maybe someone else can.
Good luck.
Dave
What command are you using to compile/link? What are the names of those files posted above? Is this the absolutely smallest code that will demonstrate your problem?
Dave
In a real situation, you would definitely want to use an std::vector and sort() from STL algorithm:
http://www.cplusplus.com/reference/algorithm/sort/
I'm not sure if that is allowed in your assignment, though.
Dave
Can you please specify your problem a little bit better? Are there errors? What is the expected output vs the current output?