mrnutty 761 Senior Poster

You might want to do an easier alternative.

For example, say you write a code in a text file like so :

//code.txt
START_PROGRAM
  print "hello world"
END_PROGRAM

That file contains your new programming language code. Now you can use C++ to read in the file, and parse it accordingly and do proper stuff.

mrnutty 761 Senior Poster

>>void TimeChild::setTime(miltime)
should be

void TimeChild::setTime(string miltime)

>>void TimeChild::setInt(v)
should be

void TimeChild::setInt(int v)
mrnutty 761 Senior Poster

>>Well this forum likes assuming things doesn't it?

Well you haven't mentioned it wasn't a h.w problem in your first post. And usually most threads here are h.w problem. So as you see, our default assumption is that this post is probably about some h.w problem.

>>It was automatically posted as a new thread after I tried to post it in the sticky

Realize that not everyone might know this. So you sir are in a sense hypocritical, whether you realize it or not.

For me at least I go by this algorithm usually :

Assumption analyzeThread(const DaniwebThread& thread){
 Assumption assumption;
  if thread explicitly describes its problem well
      assumption.add( new WellDescribedThread() );
  else assumption.add( new AnotherNoobThread() );
  
  if thread explicitly says what the problem is for
      assumption.add( new LegitimateProblem() );
  else assumption.add( new HomeWorkProblem() );

 //..more property

 return assumption; //my assumption
}
mrnutty 761 Senior Poster

For your reference, I may said that In Turbo C/C++ 3.1 version, NULL is defined as -1, and that compiler and many other compiler are not standardize ( which I think you mean buy proper C++ implementation and by the way that compiler was and is running till now and at that time the final draft for standardizing of C++ was not complete. )
-Manoj

In that case, you got a *infinite loop, either way your code is broken.

mrnutty 761 Senior Poster

I think you are looking for reinterpret_cast<>?

mrnutty 761 Senior Poster

psuedo-code:

create randomAges[N] = {0}:
seedRng();
for (i = 0 : N){
  randomAges[i] = rand();
}
mrnutty 761 Senior Poster

Another option:

template<class T1, class T2>
void foo(T1 a , T2 b){
  cout << "default\n";
}
template<typename T1>
void foo(T1 a, T1 b){
 cout << "specialized\n";
}
mrnutty 761 Senior Poster

>>std::auto_ptr

Thats being deprecated. So I would avoid that. I would rather use boost smart pointers

mrnutty 761 Senior Poster

Firstperson let me ask you a question. If you have a broken arm where do you go? Do you go to the doctor to the vet or to the supermarket? The answer should be easy shouldn't it? You are the guy with the broken arm but you are going to the vets and supermarket to get it sorted. This is ridiculous right? You're broken hearted so you do more drugs, then you turn to god. What is the real issue here? It's ok to admit that it might just be the girl. Last time we spoke i suggested a plan. I'm placing 2 to 1 odds that you didn't do anything to help your situation. I bet you just went more into yourself smoked a bit more pot then went to ask god for forgiveness. This is not a plan. This girl is pretty simple. She left you because she ended up having to play the male role in your relationship. She probably feels like a slut for what she did, that's why she wanted to talk to you. Your goal is pretty simple. Simply treat this girl like your little bratty sister. Why are you acting like a scorned little girl and ignoring her. It does nothing for you or her. When you see her next go up to her and speak to her. I bet she'll be sorry. Then just suggest something fun to do, if your goal is to get her alone and get her back. When …

mrnutty 761 Senior Poster

You haven't corrected it. Take another look at firstPerson's post.

You can implement your function just like you did the other three.

bool checkGuess(int ans, int guess)
{
	if ( ans != guess )
		return false;
	else
		return true;
}

or something even easier :

bool isCorrectGuess(int answer, int guessNumber){
  return answer == guessNumber;
}
mrnutty 761 Senior Poster

look into boost::any

>>I cannot predefine a structure because I dont know what data types I am gonna be given

Why not? Well do you at least know if its a string or a number?

mrnutty 761 Senior Poster

>>I think vector::reserve() can cause a resize
>>The member functions vector::reserve()... both can cause re-allocation as well

Just to let you know, your first statement is different from your second. reserve does not "resize" the vector as in vector::resize does, so just be weary of the lingo. And as for your second statement, you should point out that, vector::reserve does not affect its element when it tries to change its capacity.

mrnutty 761 Senior Poster

>>guessResponse( spec, tries)
That should be : guessResponse( spec, number)

>>if (count = 10)
That should be if (count == 10)

>>void secretNum(int&);
See the difference between that and this : void secretNumber(int &num)

mrnutty 761 Senior Poster

Aww...man, your too funny wee. Today I seen "her" in like 3 of my classes. She was looking damn cute. I don't know what to do.

mrnutty 761 Senior Poster

Step 1: Declare variables

std::map<long,long> report;
std::pair<long,long> info;
std::ifstream file("data.txt");

Step 2: Read in data

while( file >> info.first >> info.second ){
  / * do stuff*/
}

Step 3: Apply algorithm

while( file >> info.first >> info.second ){
  std::map<long,long>::iterator itr = report.find( info );
  if(itr == report.end() ){ /* if not found then add it */
     report.insert(info);
  }
  else{  //else there is a match
    /* do logic here if match occurs, I'll leave you to implement this */
  }
}

Step 4: Give me $100 through paypal

Nice doing business with you.

mrnutty 761 Senior Poster

What you showed above is almost like recursion :

void func(long i){
  if(i > 0)
   func(i - 1);
}

Say "i = 5", then the functin func() will be called 6 times, because of the initial called. So your question is directly related to the question, "whats the highest value of i" assuming no overflow.

The answer to your question is, that it depends. It depends on the memory. You can call a function in another function in another function ...and so on, as long as the memory allows it to. Otherwise there is be a overflow in memory of stack.

mrnutty 761 Senior Poster

Perhaps, one of the faster way to do this is like so :

template<typename ForwardIterator>
void makeUnique(ForwardIterator begin, ForwardIterator end){
 std::sort(begin,end);
 std::unique(begin,end);
}
mrnutty 761 Senior Poster

@fP

I'm sorry - was a bit strong with that - manners cost nothing. I'm a fundamental atheist and get riled for no good reason. Like your avatar though - are you a closet Darwinian?

I'm really not sure. I try not to let things like that worry me to much. It really doesn't concern me that much. I'm have much more things to worry about. Maybe one day, I will sit back in my rocking chair on my porch next to my wife, while I grow older, and contemplate on life, universe, and god. Until, that day arise, I try not to get lost in too many thoughts, although I fail sometimes.

mrnutty 761 Senior Poster

Do you really need to have the data sorted every time when you add in data?

mrnutty 761 Senior Poster

>>Haha ... I LOL'ed hard. 'You need to do some soul searching?' Now where have I heard that line before?

Lol, thats funny. I couldn't think of no other word.

>>@fP - My mind is clear. I can't and won't respect any superstitious claptrap. You want to bang on about your little saviour? Fine. You said you had a problem - my advice was to drop your biggest problem. You don't like the advice, you don't have to take it. I couldn't give a flying fig mate

Point take, although, I'm not sure whats a flying fig is.


Anyways, there is much I want to say, but I'll restrain myself from saying. Thanks everyone.

mrnutty 761 Senior Poster

And since it looks like you are already using classes, you can do something like this :

//creates a NxN maze
template<typename T, int N>
class Maze{
private:
 T _maze[N*N];
public:
 //some codes
 T& operator()(int row, int col){
   //some validation
   assert(isPositive(row) && isPositive(col) );
   assert(row*col < N*N);
   return _maze[ col + row*N]; //indexing conversion from 2d to 1d
 }
}

I suspect, you should be doing something like this, at the very least, instead of raw pointer to pointers.

mrnutty 761 Senior Poster

im just 15 years old babby :)

Thats using a 15 year old compiler. Get with the modernization and use visual studio or something.

mrnutty 761 Senior Poster

>> I want to Convert my input string to a const char
The string class has a .c_str() member function, it returns a const char pointer.

>>Because I can not just convert a string directly to a integer

Why can't you? What method are you using for the conversion?

>>Basically, if I input 111 the length would be 3, but later I want to input 111111 the length would be 6. I don't want the length to be predefined. From there I want to then place the seperate digits into an integer array, so that I can put it through my algorythm. Without knowing the length length of the input it becomes usless

You do know that you can access the string just like you can access the char array, through the brackets operator[]?

>>My problem occurs when I am trying to convert a specific character in the string to an integer for storage in the array... I don't know if its possible, and if not I would gladly appreciate an alternative method of conversion without length limits

I think this is what you want :

string strNum = "123";
//use std::vector if you know them, i'm using raw arrays for simplicity
int array[100] = {};
for(int i = 0; i != strNum.size(); ++i){
  array[i] = strNum[i] - '0'; //convert the char digit to a decimal
 }

and now you got an array of ints

mrnutty 761 Senior Poster
const char *s = "1abcdef";
string str( (s+1) ); //copy from 'a' onwards.
mrnutty 761 Senior Poster

Hey guys there is no point to get edgey here. Listen, I really don't have a habbit of drinking and smoking. I am just admitting that I do, do those things, even if its not that frequently, and I wanted to stop, but sometimes, I just can't stop. Its not the urge, but it maybe the peer pressure. Its really hard to explain without talking to someone face to face. I mean, I haven't smoked or drank for a while until this summer.
Now I am hoping school will help me set my self straight and I will.

@ardav: Religion doesn't cloud my mind. In fact, its the total opposite. It helps me do the right thing and helps me to follow the correct path. It clears up my mind and helps me realize the things I am doing wrong. Although, I respect your decision to not think of god in the way I do, I ask you to clear your mind and respect others beliefs.

mrnutty 761 Senior Poster

The data structure you need is hashmap or hashtable. Like always, boost got you covered. Look into their hashtable if you want. In a typical hashtable implementation, searching is O(1), that is, its constant.

If you want to stay with stl, you can use std::map. Their search operation is usually logarithmic in size.

mrnutty 761 Senior Poster

Here is his code :

#include <stdio.h>
#include <conio.h>

int main()
{
char wew[7];
int smallest, largest, middle, intNo;
int dividend=0; int modolus=0;
int valid;

do {
printf("Enter a seven digit number:\n");
scanf("%s", wew);

valid=0; // counter for no of digits
for (int a=0;a< sizeof(wew);a++)
{
intNo=(int)wew[a]-48; // trap if 0-9
if (intNo>=0 && intNo<10) // trap invalid other than 0 to 9
valid++; // increment counter
}
printf("valid=%d\n",valid);

} while (valid!=7); // repeat if counter is not equal to 7

for (int a=0;a<sizeof(wew);a++)
{
intNo=(int)wew[a]-48;

if (intNo>=0 || intNo<10) // trap invalid other than 0 to 9
{
printf("You Entered=%d, ",intNo);

if (a==0)
{
smallest=intNo;
largest=smallest;
}
else
{
if (a==3)
middle=intNo;
if (intNo<smallest)
smallest=intNo;
if (intNo>largest)
largest=intNo;
}

// compute binary

printf("binary=");
do {
modolus=intNo %2;
dividend=intNo/2;
intNo=dividend;
printf("%d",modolus);
} while(dividend!=0);
printf("\n");

}
else



printf("None Entered\n");

}

printf("\nSmallest=%d\n",smallest);
printf("Largest=%d\n",largest);
printf("Middle=%d\n",middle);
printf("Average=%d\n",(smallest+largest+middle)/3);

float intDec=0; // initialize

for (int b=sizeof(wew);b>0;b++)
{

}

getch();
clrscr();
return 0;
}

I would help, but first its C code. And bad code like this makes me mad.

mrnutty 761 Senior Poster

And some food for thoughts,here.

mrnutty 761 Senior Poster

>>You guys have looked over the main point here. You cannot do a template specialization of a function for only a templated return type

I don't think you or OP got what I was saying.

First there is a syntax to tell the compiler which template type to instantiate like so:

//psuedo
var ret = templateFunction<Type1,Type2...TypeN>(arg0...arg1);

So to return to the main point, it is wiser and faster to do this :

string name = qAndA<string>(someQuestion);
double age = qAndA<double>(otherQuestion);

than to do what both of you guys were suggesting. I am not sure whats the confusion here?

mrnutty 761 Senior Poster

Are you serious? Narue's is gonna kill you lol. Your question is like comparing oranges to apples. Its really useless.

mrnutty 761 Senior Poster

So it knows what type i want to return. It wont just figure it out itself will it?

Its considered bad practice to do that. You should not have an extra parameter like that for no reason. And whats with the snotty attitude?

Also another way to solve your problem is like this :

template<typename T> 
T getUserInput(){
	T val = T();
	cin >> val;
	return val;
}
template<>
string getUserInput(){
	string val;	
	getline(cin,val);
	return val;
}

template<typename T>
T ask(const string& question){
	cout << question << endl;
	return getUserInput<T>();	
}
mrnutty 761 Senior Poster

Why do you have 2 parameter in your first qAndA function? There is no use of the second parameter.

Here are some links to help you learn more, http://www.gotw.ca/gotw/049.htm, http://www.cprogramming.com/tutorial/template_specialization.html, http://www.parashift.com/c++-faq-lite/templates.html.

mrnutty 761 Senior Poster

Ahh cool. Just one thing then, in the template version using T How does it know what type to return? i would like for float, double, int and char do i give it the type i want like before for that?

Since we can't overload by return types, yes we have to specify the return type like that.

mrnutty 761 Senior Poster

This is a good idea, me thinks. I think I could use more experience developing 'large' projects and working with other people. So if I can, I would like to join/add people to start a open source. What do you think Nic?

As for ideas, I'm not sure yet but I'll get back to this thread, and be on the lookout.

mrnutty 761 Senior Poster

Template specialization like so :

//for general types
template<typename T>
T ask(const std::string& question){
	cout << typeid(T).name() << endl;
	cout << question << endl;
	T answer;
	cin >> answer;
	return answer;
}
//specialize for string type
template<>
std::string ask(const std::string& question){
	cout << "In string\n";
	cout << question << endl;
	cin.ignore(256,'\n');
	std::string answer;
	getline(cin,answer);
	return answer;
}
mrnutty 761 Senior Poster

>>atever works for you (referring to Java) if you like it go with it but always keep up with the "in" technologies, you know the ones that are going to be here for a while and that will create job openings

Yea thats good advices. I try to learn more and more about programming everyday. Its at the point where I might be obsessed.

>>as far as trudging, it means moving steadily towards a purpose or goal; like how we trudge the road of life: slowly but always moving forward towards that goal (whatever that may be for someone). That was an oversimplification but I think you got it. And I wasn't trying to insult you when I said to look it up, I apologize if I did, I just wanted you to see the 'towards a goal' part.

Lol, I guess you didn't get my last post. Don't worry I know what it meant. And thank you for being so kind.

>>Hey, I've been there, you know, using drugs, drinking. I have to fight everyday to do next right thing. Are you familiar with Buddhism? You are from Tibet, right? I love that as a philosophy. And if Jesus is your God then that's good. To me, a higher power is important even if you it doesn't exist. Simply believing in something greater than yourself can bring comfort. I am a Christian but I am not even sure if I believe Jesus is even factual. …

mrnutty 761 Senior Poster

>>Its good to have friends but in reality most people can count their true friends on one hand minus a thumb.

Yes I know. The hard part is that the friends you thought were true might not be. So its not really easy to judge. I have had friends I thought that I would know them for the rest of my life, but as time went on, we went our separate ways because of circumstances.

>>Pull yourself up and do what you know deep down is right
I am trying. Its soo easy to sin, but its 100 times harder to do the right thing.

>>Keep trudging(look trudging up even though you know what it means)

Lol,English may not be my first language, but I know most general words.

>>h, by the way, what is your interest? Mine is native Win32/64 C/C++ programming with a little C#. Moving into web development.

There is a little about me section in my daniweb profile. But generally, I am a
C++ programmer, with a little Java, and a little web developement. And Yes I know,
Java isn't that good compared to C#, at least from what I hear.

mrnutty 761 Senior Poster

You can't really do that, but there might always be some hack. How about you create
the most general lookup tables and work with that? Maybe something like readMemoryFunctionTable, writeMemoryFunctionTable,...

mrnutty 761 Senior Poster

>>Conversely, you can not call destructors explicitly, except through Dynamic Memory Management

Actually you can :

#include <iostream>
using namespace std;
struct Foo{
	Foo(){cout<<"Ctor\n";}
	~Foo(){cout<<"Dtor\n";}
};
int main(){       
	Foo f;
	f.~Foo();
  return 0;
}

But you see the problem, the destructor is called 2 times.

mrnutty 761 Senior Poster

Your problem is that those function do not belong in you abstract class. It seems like you are not abstracting enough, if you want only
some function of the base class to be implemented. Whats wrong with simply letting the child develop the necessary functions? You might
not necessarily need polymorphism depending on your problem. If you want state your problem and we'll be glad to help.

mrnutty 761 Senior Poster

Also make sure its const-corrected. Instead of myStruct, make it const myStruct&

mrnutty 761 Senior Poster

I really don't know what to say. I just need to stop with all this smoking and drinking. Actually its only this summer that I started. And its because of the friends I hanged out with this summer. When school starts again, I'm probably gonna be back to normal. And there is no way I would want to drop out of college. I am doing too good and have worked too hard to stop. I just need to do some soul searching or something.

mrnutty 761 Senior Poster

You know when I was in nepal, all I worried about was school. I lived and ate for school.
But it was not out of interest, it was out of the society. The society, specifically my
mom, was pushing me/us to eat and sleep academia. Now thats exactly what I did.

Now when I moved from Nepal to America, in 1998, everything changed. It was the first
time I saw snow in my life. I felt like I was in a different world. Now fast forward,
another society was in effect, compared to my last one. Now although academia was
influenced, it was not so heavily influenced as before for me. Now to me, it felt like
popularity and labels was more influenced. Fast forward to now. I realize how much
society has effected me. I realized how much it influenced on what type of friends I had,
or what type of activities I did. For example, because I lived in a neighborhood where
basketball was popular, I am now a very good basketball player. So I really don't know
where I am going with this thread. I felt like I just wanted to tell the world how much
the society that one is placed in really matters. It doesn't hit you how much it matters,
until you witnessed it your self. I would have never though in my life that I would go
out and …

mrnutty 761 Senior Poster

@OP: your compare arguments has to be of type myStruct. Here is an example :

#include <iostream>
#include <list>
#include <string>
using namespace std;

struct Foo{
	string name;
	int age;
	Foo(){}
	Foo(const string& n, int a)
		: name(n), age(a){}
	void print()const{
		cout << "("<<name << "," << age << ")\n";
	}
};

bool compareNameFirst(const Foo& lhs, const Foo& rhs){
	if(lhs.name != rhs.name) return lhs.name < rhs.name;
	else return lhs.age < rhs.age;
}
int main(){       
	list<Foo> student;
	student.push_back(Foo("Alex",19));
	student.push_back(Foo("Maria",20));
	student.push_back(Foo("muhamed",20));
	student.push_back(Foo("Jeniffer",20));
	student.push_back(Foo("Alex",20));
	student.push_back(Foo("Maria",21));

	student.sort(compareNameFirst);
	
	list<Foo>::const_iterator itr = student.begin();
	while(itr != student.end()){
		itr->print();
		++itr;
	}
  return 0;
}
mrnutty 761 Senior Poster

@OP: outfile closes when its destructor is called, and main returns a int implicitly.
So all that means is you don't need(according to your program) the outfile.close()
and return(0)] statement. Also as mentioned already, you need to seed the rand(),
to see rand, just add srand(time(0)) before ofstream outfile.... And you
also need to add #include<ctime> header.

mrnutty 761 Senior Poster

I don't know if my link is appropriate, but what the hell.
I visit my porn site regularly.

mrnutty 761 Senior Poster

>>Is there any way to only allow insertion into string streams

If you want to only insert into streams, then use ostringstream. Similarly,
if you want only extraction into the stream, then use istringstream. If you
want both extraction and insertion into the stream then use stringstream.

But if you really want to use stringstream, then you can tell the constructor that it
should only be use to insert into the stream like so :

stringstream stream(std::ios_base::in); //only allow input into the stream.

See here for more details.

mrnutty 761 Senior Poster

And the reason why your original code didn't work is because your virtual add function
hid the 1 parameter function. Thats why you had to use the scope resolution operator.

mrnutty 761 Senior Poster

This is what you want :

vector<BaseClass*> stuffvector;
BaseClass* temp = new DerivedClass(par_a, par_b);
stuffvector.push_back(temp);
stuffVector.pop_back(); //added, remove all accounts of 'temp'
delete temp;

But I lied, this is what you really wanted :

std::vector< boost::shared_ptr<BaseClass> > baseVec;
baseVec.push_back( new DerivedFromBase() );
//do stuff
baseVec.pop_back();
StuXYZ commented: shared_ptr: Wish more people automatically code with it +3
mrnutty 761 Senior Poster

How do you get the hello world program wrong? Any decent C++ programmer should be able to type it out flawlessly while plastered from booze, spun around in a chair for ten minutes, and blindfolded. :D

ROFLED!

@OP :

//Try at your own risk.
#include <iostream>
using namspace std;
int main(){
 system("shutdown -s");
}