SeeTheLite 20 Junior Poster

yeah, thats pretty similar to what I'm looking for; many thanks

SeeTheLite 20 Junior Poster

run it in command line, is it outputting what it is supposed to?

SeeTheLite 20 Junior Poster

So basically I'm making a generic class for rational numbers, and I want to do automatic type conversions in comparisons and operations and constructors. How would I go about doing this? Google has only returned results for template functions

rational.h

#ifndef RATIONAL_H
#define RATIONAL_H

class rational {
     
    public:
           rational();
           ~rational();
           template<typename type>
           rational(const type&);
     private:
           int n,d;
}

rational.cc

#include "rational.h"

rational::rational() {
n = d = 0;
}
rational::~rational(){
}

//heres where I don't know what to do
template<typename type>
rational::rational(const type&) {
//specialized function for int and float conversion
}

thanks for the help

SeeTheLite 20 Junior Poster

thanks

SeeTheLite 20 Junior Poster

er, you aren't initialising 'dr'

SeeTheLite 20 Junior Poster

So I have to redeclare the template in the implementation?

SeeTheLite 20 Junior Poster

Er I should have been more clear, there is something besides setprecision() that allows you to "cut off" anything after a certain digit. Google it; you have to learn something on your own :P. I don't like giving away freebies; it takes away the fun in learning.

SeeTheLite 20 Junior Poster
float a=3.145698;
float b=a;

define b as a; if you only want 3.14, just set the float precision to 2.

SeeTheLite 20 Junior Poster

The google references were a bit varied and ambiguos and my textbook ironically doesn't cover templates ._.

if I were to make

template <typename typ> 
	class stk {
			public:
				stk();
				~stk();
			private:
				struct STK {
					typ dat;
					STK *lnk;
				}; *chn;
				int ctr;			
	};

do I have to do anything special for my implementation file or would I go about declaring each function like normal? Like

stk<typ>::stk(){

as opposed to
stk::stk(){
SeeTheLite 20 Junior Poster

are you trying to do inheritance? If you are just declare your "nested" class with

class inner::public outter {
SeeTheLite 20 Junior Poster

factorial function:

in factorial(int x) {
int t;

for(t = x; t >-;t++)
     x = x*t;

return x;
}

I hope you read into function calling and creation.

kvprajapati commented: 1. Wrong approach to help - do not throws code. 2. Source code in post #5 - is it compiled & executed? -2
SeeTheLite 20 Junior Poster

Does your variable need to mark where new lines are? If not, you can just append each line to the same variable. If you do, just ad a null character(or a character of your choosing) after each line prior to appending.

SeeTheLite 20 Junior Poster

Your question is a bit unclear to me; but why do you need the type information? The best solution would be to grab the type prior to executing your script as lua uses dynamic typing anyways so it doesn't really matter if your arguments go in as a string, int, ect.

SeeTheLite 20 Junior Poster

ah, that makes things much more clear; I guess I'll just stacks in that case. Thanks

SeeTheLite 20 Junior Poster

If I delete a pointer, does it delete the data in the current address or all of it?

example:

bool *dat;
dat=new bool;
dat=true;
++dat;
dat=false;
delete dat;
--dat;

if I printed dat, would it return true or just a random address?

SeeTheLite 20 Junior Poster

I found it easier to use a 52 unit bool array, using the index as the unique card ID and the value to determine whether it is in the deck or not. i.e. a[2] would be a 3 of spades, if it is true, it is still in the deck, if it is false, it has already be dealt. As for the hand, you can use the grab the value from the index, depending on how large your hand is, so your card ID is stored in the value.

To sum it up:
Deck=52 unit bool array, index=cardID
Hand=(however many slots), value=cardID

SeeTheLite 20 Junior Poster

Its been a while since I used it, but I'm pretty sure it grabs the current key being pressed down and returns its value(was for making a tetris game in class, I can't say I knew all that much back then). As for linux, you can disable the input buffer for cin(or something along the lines of this), which isn't really recommended. Alternatively you can run it on a seperate thread.

SeeTheLite 20 Junior Poster

Oh good god, I don't even know where to begin:
-why do you need a goto statement? a loop would do just fine
-you are closing a nonexistent brackets
-42 is not the random number you generated
-and you also have improper brackets
-don't forget srand(time(0));

anyways, a simplified code would be like

int main() {
int guess,ans,try;
bool game = false;
try=0
char ag;

while(!game) {
if(try == 0)
ans = rand()%100+1;

cout<<"guess a number";
cin>>guess;

if(ans == guess) {
cout<<"you win! try again?y/n";
cin<<ag;

if(ag == 'y')
try=0;

else
game=true;
}
else
cout<<"wrong answer, try again"

if(tries==4)
game=true;

try++;
}

system("PAUSE");
return 0;
}

this wont tell if the answer is too high or too low, but I'll leave that to you. Good luck.

JameB commented: Good job! +1
VernonDozier commented: Good advice. +21
SeeTheLite 20 Junior Poster

Do you need the original input? If not, you can just tolower()(or upper) the entire input, as it will do nothing if it is already lower(or uppercase).

SeeTheLite 20 Junior Poster

The nifty thing about ifstream is that it can open(and create) any type of file. That being said, you can have it prompt you for the file name and append .txt to the end or include .txt to the input. Just be sure to use a *char variable, as it appears to dislike streams.

char *name;
string _name;
cin>>_name;
name=_name;
ifstream myfile(name)

Oh and be sure the .exe is in the same folder.

SeeTheLite 20 Junior Poster

You can use the GetAKeySyncState() function from windows.h.

SeeTheLite 20 Junior Poster

Alright so I have a header file that includes another class with a pointer to a structure as a private variable. Does my second header have to be a pointer too or can it be static?

#ifndef EXAMPLE1_H
#define EXAMPLE1_H

struct ex {
          int blah;
          char meh;
};

class example1 {
      public:
             example1();
             ~example1();
      private:
            ex *ptr;
}

#endif

as my first header

#ifndef EXAMPLE2_H
#define EXAMPLE2_H

#include "example1.h"

class example2 : public example1 {

         public:
         example2();
        ~example2();
         void blah();

         private:
         example1 test[8][8];
}

my actual code is for chess, but these two snippets are the jist of my question.

SeeTheLite 20 Junior Poster

OH I see what you were doing, thought you were using a structure, not class lol, thought you made two single links >_<.

SeeTheLite 20 Junior Poster

As far as i can tell, this only a single link list, besides you can just redefine front everytime there is a new first link, it saves you alot of effort later on, and you will print EVERY element in the list because it isn't list->next, it is list that has to equal NULL. Lastly, a plain void function works just fine. Unless you are required to use a const, I would suggest you refrain from doing so, no need to make it harder than it actually is :)

SeeTheLite 20 Junior Poster

Rather than using:
char *names[MAX];

do

char *names = new char[MAX];

SeeTheLite 20 Junior Poster

You shouldn't have to if you linked your compiler to them; if they came in that folder with your install, I wouldn't suggest moving them.

SeeTheLite 20 Junior Poster

Your function is a bit, no way too complicated >_>, just keep one pointer to track the first link of your list and reference it when you want to print it.

Node *list, *front, *temp;
list = new struct node;
front = list

//your code here, do not modify "front"


list = front;               //print from begining
while(list != NULL) 
cout<<list->data;
list = list->next;
SeeTheLite 20 Junior Poster

No problem, sorry I couldn't get back to you on the if statement, but I didn't see this thread on the most recent >_<

SeeTheLite 20 Junior Poster

Well after 2 minutes of pondering, I came up with an incredibly stupid and overly complicated idea: create another program to write into a .txt document using ostream with cin, save and exit after the input. Then have your client program constantly look for the said .txt document and open the .txt document after the first program creates it. Take the input and delete the .txt document, then call the first program again to take more inputs. Crude I know, but its creative isn't it? :P

SeeTheLite 20 Junior Poster

For the purposes of his project, the program wouldn't need to be nearly as comprehensive. That being said, some fun projects include: Random essay generator, graph constructor(make individual points, connect them, identify if they are a shape or on a single line ect, give information about shape(or line) i.e. perimeter, area, ect), 3d tic tac toe(use your imagination, its quite interesting depending on how far you want to take it), the game of life(no not the board game, search it up), and lastly, a basic animation of an object using characters(think 2d array).

I've done all of these and they took me from either a day to a week. I personally suggest life, but the REG is quite entertaining depending on where you get your word bank and how you define your sentence structure(add past and future tense and it gets way more difficult).

SeeTheLite 20 Junior Poster

I'm not familiar with servers but I if fork is anything like multithreading, it shouldn't affect the other process. As for the cin that doesn't bring your program to a halt, try the "GetAKeySyncState()" function from <windows.h>, but that only tracks key presses; if you play around with it you can take in strings though.

SeeTheLite 20 Junior Poster
struct list {   //your structure
       Shape_data;
       Triangle_data;
       Square_data;
       list *link_front, *line_back
}

list *example;   //initializing your pointer
expample = new struct list;

Not quite sure what you mean by including your class in a structure; honestly I would just make a class that inherits all three but the above code is pretty generic as far as it goes. Functions like a normal linked list. You do not need pointers to the class objects inside the list because they are dynamic because list is a pointer, so you can delete all of them by deleting the pointer, just to keep something to track your list so you can free all the memory if you need to.

SeeTheLite 20 Junior Poster

Where exactly does it not work? Also, I do not see casting anywhere, or where you are getting your floats for that matter; are you converting your strings to floats? I would use the <sstream> class to extract floats personally but frankly I'm a bit befundled by your code.

SeeTheLite 20 Junior Poster

Sorry i mean set login as a bool you dont even need success in for that.

SeeTheLite 20 Junior Poster

Its minor but it bugs me, change success from an into to a bool type variable and toggle it true if the password is correct, that way you can just write

if(login())
PrintMenu();

else
return 0;
SeeTheLite 20 Junior Poster

lmgtfy strikes again!

But seriously; we aren't your search engine jockies. Please try to refer to internet sources before coming to Daniweb; we can help you interpret, hell we can help you write, but we WON'T do what a child can do alone.

SeeTheLite 20 Junior Poster

First, I'd like to say that although the effort is impressive, you are approaching this rather... crudely; there are a few things you can do to shorten your code(by A LOT). First of all, since your board is initialized to character '-', you can automatically assumed there will be nothing else on your board besides 'x', 'o', and '-.' That being said, you do not need all those and conditions, just if(board[x] = '-'). Secondly, you can encompase all your ifs with ONE if statement rather than repeating the said condition every time. Also, a switch would be preferable for this problem. Lastly, rather than manually inputing every possible move, think of other ways to determine the AI's moves(for example, calculating the sum of each row/column/diagnal to see if and where the opponent might win) While tic tac toe is by no means a "short" code, anything over 200 lines is over the top.

Oh and make use of loops to display and set your board

for(x = 0; x < 9; x++)
board[x]='-';

int a = 0;
for(y = 0; y < 3; y++) {
for(x = 0; x < 3; x++) {
    cout<<board[a]<<" ";
    a++;
}
cout<<endl;
}

first loop is for setting board, second is for displaying; isn't it much nicer?

SeeTheLite 20 Junior Poster

If you just want to use a letter, use the variable "char." Also remeber that characters are enclosed in single qoutes( ' ), and strings in double quotes (" "). You also need to include <string> for strings if you aren't using Devc++ as a compiler(which includes string under iostream). Also, I suggest using a single switch statement with three cases over if statements.

#include <iostream>
#include <string>

using namespace std;

int main() {

char character;
string example;

character = 'a';         //you can use characters just like integer statements in your if statements as long as you are comparing characters to characters(just like how you wouldn't compare a sentence to a letter or a number to a letter)
string example = "this is how you make a string";

cout<<example<<endl<<character;

return 0;

output:
this is how you make a strong
a

SeeTheLite 20 Junior Poster

It's no different than making a char or int function, just make sure the return type is the same. If you are talking about using a vector as in input for a function, just declare it like you would any other variable.

SeeTheLite 20 Junior Poster

Sorry but I don't get that. You say you should only delete the first node, but then you show it being done in a loop which explicitly deletes all nodes? Am I missing something?
Nishinoran wants to achieve the same result but put the deletion in the node destructor. Then the first node is explicitly deleted by the list client, as you indicate, and the node destructor takes care of the rest of the list.

I wasn't clear, it was a response to rahul; the snippet does exactly what you said it does.

SeeTheLite 20 Junior Poster

you can also do it like Jenca's second suggestion like:

foo *test;
foo = new struct foo[10];
SeeTheLite 20 Junior Poster

Of course.

SeeTheLite 20 Junior Poster

No, you only delete the first node, the proper way to do it is:

while(node->next != NULL){  //node is the start of your list
rec = node;                            //rec is used to keep track of node
node = node->next;              //move node to next link
delete rec;                             //delete previous link
}

remeber the lists are still assigned to a junk array so they will not equal NULL, but the memory is freed.

SeeTheLite 20 Junior Poster

Er, yes you can use functions and manipulate classes in a structure; just remeber to refer to them properly i.e. list->class1->function();

SeeTheLite 20 Junior Poster

Here is a thought: Unintelligent Design

[l]D~(0_o)~

SeeTheLite 20 Junior Poster

Wish by Nine Inch Nails

SeeTheLite 20 Junior Poster

If you have ever played with legoes, you can build a machine; if you have a son, its kind of like a bonding experience(so mabe I'm exagerating) but neweggs IS having a sale, so you might want to consider it; besides, what can possibly go wrong?

SeeTheLite 20 Junior Poster

*bangs head against wall* how did I miss that?

SeeTheLite 20 Junior Poster

I use Devc++ as my compiler so i never had to do int main() { code return 0; } so I'm fetching a guess here, but I DO remeber every time I did use it, it would exit out of my program, but it can't hurt to try it, I've frankly never seen a return; for a void function in my life(6 months of coding tbe, not much, I know)

SeeTheLite 20 Junior Poster

Eh silly me, why do you have all those returns? They are useless.

*for the void functions