gerard4143 371 Nearly a Posting Maven

Jeez I tried googling LUA and C++ and found this

http://csl.sublevel3.org/lua/

gerard4143 371 Nearly a Posting Maven

Can we see the define of the structure?

gerard4143 371 Nearly a Posting Maven

so .. are you saying the const statement is in the wrong place in the parameter list of the function title?

No not at all...Passing the object as const informs the compiler that this reference won't change the object.

Does printSummary() change the object?

gerard4143 371 Nearly a Posting Maven

Const iterators are used to indicated that the underlying object data members will not change.

gerard4143 371 Nearly a Posting Maven

Here's a quick and incomplete solution to what I was referring to in the above posting:

#include <iostream>
#include <cstdlib>


using namespace std;

int main()
{
	int myNumber = 100;
	int min = 0;
	int max = 100;
	int guess = 0;
	int hint = 0;
	int count = 1;

	cout << "Input a number between 1 and 100: " << endl;
	cin >> myNumber;
	cout << "\nPress 1 if too low or press 2 if too high.\nPress 3 if correct " << endl;

	guess = ((max - min) /2) + min;
	cout << "The computer guesses: " << guess << endl;

	cout << "Please enter 1 for too low, 2 for too hign and 3 for correct->";
	cin>>hint;
	
	while (guess != myNumber)
	{
		switch (hint)
		{
				case 1:
				{
						cout << "too low" << std::endl;
						min = (max - guess + min);
					
						cout << "min set to->" << min << std::endl;
						cout << "counter->" << ++count << std::endl;
						break;
				}
				
				case 2:
				{
						cout << "too high" << std::endl;
						max = (max - guess + min);
						cout << "max set to->" << max << std::endl;
						cout << "counter->" << ++count << std::endl;
						break;
				}
				
				case 3:
				{
						cout << "correct" << std::endl;
						return 1;
				}
				
				default:
				{
						cout << "entered invalid character" << std::endl;
				}
			}
			guess = ((max - min) /2) + min;
			cout << "The computer guesses: " << guess << endl;

			cout << "Please enter 1 for too low, 2 for too high and 3 …
gerard4143 371 Nearly a Posting Maven

Try passing the address of newData...

computeFibonacci(&newData);
printFibonacci(&newData);

Also this:

int computeFibonacci();
int printFibonacci();

should be:

int computeFibonacci(shared_data*);
int printFibonacci(shared_data*);
gerard4143 371 Nearly a Posting Maven

Oh i see thank you very much! One more question if you dont mind, how can i put "@" in a line of code. That is why i have string atsym i thought that might work but it only prints @ also.

I'm not really sure what you mean by "put "@" in a line of code". Do you mean take the character '@' and embed it in a string as a sentinel?

gerard4143 371 Nearly a Posting Maven

std::cout << "char->" << ltr << std::endl;

What does that line do? I have not been shown that in my class.

I was just checking to see what the function was receiving for data...You can delete that line.

gerard4143 371 Nearly a Posting Maven

Can we see the constructors of the class Mixed?

gerard4143 371 Nearly a Posting Maven

You had a few typos in your code...You really should write a small part of the program compile/debug and then move on.

#include <iostream>
using namespace std;


int ScrabbleScore(string word)
{
	int x = 0;
	int index = 0;

	while (word[index])
	{
		char ltr = toupper(word[index]);
		std::cout << "char->" << ltr << std::endl;

		if(ltr == 'A' || ltr == 'E' || ltr == 'I' || ltr == 'L' || ltr == 'N' || ltr == 'O' || ltr == 'R' || ltr == 'S' || ltr == 'T' || ltr == 'U')
			x += 1;
		else if(ltr == 'D' || ltr == 'G')
			x += 2;
		else if(ltr == 'B' || ltr == 'C' || ltr == 'M' || ltr == 'P')
			x += 3;
		else if(ltr == 'F' || ltr == 'H' || ltr == 'V' || ltr == 'W' || ltr == 'Y')
			x += 4;
		else if(ltr == 'K')
			x += 5;
		else if(ltr == 'J' || ltr == 'X')
			x += 8;
		else if(ltr == 'Q' || ltr == 'Z')//here
			x += 10;
		++index;
	}
	return x;
}




int main()
{
	string scrab;

	cout << "This program tests the ScrabbleScore function." << endl;

	cout << "Word: ";
	cin >> scrab;

	cout << "The basic score for '" << scrab << "' is " << ScrabbleScore(scrab) << endl;

	return 0;
}
gerard4143 371 Nearly a Posting Maven

Your array is of type int not float.

Plus your for statements are wrong.

gerard4143 371 Nearly a Posting Maven

Sorry...This isn't a 'give the code kind of website'...You have to show some effort and post the code you have..

gerard4143 371 Nearly a Posting Maven

> step around the compiler protection...
... straight into the undefined behaviour.

Yes true enough..But I think we moved away from the original problem of why

#define j 2

or

enum choice {a, b, c};

works but

const int j = 2;

does not...IMHO I really feel its because the enums and the define are literals which are written into the instructions while const int j is a variable which has to be fetched from memory...

gerard4143 371 Nearly a Posting Maven

I really need some help with this. I am not looking for just the answer but an explanation of why the code is what it is.

What code?

gerard4143 371 Nearly a Posting Maven

If I was writing this this I would strive for simplicity. Have the user enter a value and then have the computer guess 50, the program should respond by indicating higher or lower(or correct). The computer then should take the set above or below and pick a middle value...etc...etc.

Example:

user enter value 35.

Computer guesses 50.(guess 1)

Program responds too high.

Computer guesses 25.(guess 2)

Program responds too low.

Computer guesses 37(guess 3).

Program responds Too high.

Computer guesses 31(guess 4).

Computer responds too low.

Program responds 34(guess 5)...etc

gerard4143 371 Nearly a Posting Maven

Well how do you define the structure in the second example? Maybe that's a little vague...How do you define the memory layout in the second example?

In the first example you have sizeof(int) plus sizeof(struct node *n1) plus padding.

In the second example what is the sizeof(struct node)? Its impossible to tell.

gerard4143 371 Nearly a Posting Maven

Because in C, const != "constant", but "read-only". That is, it is legal to initialize a const variable using

void f(int n) {
    
    const int m = n / 2;
}

but its value is dependent of n. So if we call f passing first 2 as argument, and then 4, the values of m will differ. But a switch statement requires integer constants which are known before the program executes. In the above example, this is clearly not the case.

I don't like the term "read-only" because in this case it was read/write if you knew enough to step around the compiler protection.

gerard4143 371 Nearly a Posting Maven

Thanks for the reply.

Yes. This works fine. But im wondering why the former did not work inspite of the const qualifier.

Try running this code with a 'const int'

#include <stdio.h>

int main(void)
{
	int *iptr = (int*)NULL;
	const int i = 1234;

	fprintf(stdout, "i->%d\n", i);

	iptr = (int*)&i;

	*iptr = 898989;

	fprintf(stdout, "i->%d\n", i);

	return 0;

}
gerard4143 371 Nearly a Posting Maven

Try this

#include <stdio.h>

#define j 2

int main(void)
{

        int i;
 
        switch (i) {
                case 1:break;
                case j:break;
                default:break;
        }
 
        return 0;

}

or

#include <stdio.h>

enum choices {a, b, c};

int main(void)
{

	int i = 0;

	fputs("enter a value->", stdout);
	fscanf(stdin, "%d", &i);

	switch (i) 
	{
	case a:
	{
		fputs("a\n", stdout);
		break;
	}
	case b:
	{
		fputs("b\n", stdout);
		break;
	}
	default:break;
	}

	return 0;

}
gerard4143 371 Nearly a Posting Maven

What an absolute mess...Use proper indenting and I'm sure you'll find your problem..

gerard4143 371 Nearly a Posting Maven

You haven't allocated/assigned any memory for answer...

gerard4143 371 Nearly a Posting Maven

How about giving us a hint with some code....

gerard4143 371 Nearly a Posting Maven

Um, perhaps you could elaborate on the logic of this for us slower kids? Because your idea as stated is both illogical and broken.

>@Narue
>i did not get you. It does compile.

Yes, it does compile. That was my point. gerard was wrong in saying that the code is not C++.

Please electorate I'm interested in your comments.

If it was C++ shouldn't the includes be

#include <cstdio>
#include <cstdlib>

gerard4143 371 Nearly a Posting Maven

If I was creating this simple binary tree I would create my structure like so

struct tree_el 
{
  int *val;
  struct tree_el *right; 
  struct tree_el *left;
};

With an int pointer...That way you can test if the pointer exists with a simple statement like

struct tree_el *root = (struct tree_el *)malloc(sizeof(struct tree_el));

if (root->val)
{

}
gerard4143 371 Nearly a Posting Maven

You know this is written in C not C++...

gerard4143 371 Nearly a Posting Maven

Need Help Making Keylogger

Hi everyone,

My name is hayzam and i want to make an advanced keylogger and what to put it in a USB
So when I connect my usb it will open automatically and it must be invisible
one more thing i am a newbie to C++ so try to be easy on me

Thanks In advance,
Regards hayzam

Well good luck..

gerard4143 371 Nearly a Posting Maven

You could use

p1->lastName
p1->firstname

gerard4143 371 Nearly a Posting Maven

I want to know how to create a makefile so that I dont have to type in the files I am linking together. Also is it possible to use a make file which will run two files.

I have a fraction.h file and fraction.cxx (implentation file)
I have a main.cpp file (which has the int main)
I have a main-1.cpp file which also uses (the header and implementation file)

Can the two main files be used to create one makefile

You can only have one entry point in a program and that's usually the main function. If you only define main once in both main.cpp and main-1.cpp then yes you can compile them together.


Here's a short tutorial

http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html

gerard4143 371 Nearly a Posting Maven

You need to pass a pointer to a pointer for this to work...

Your function should be declared like.

void teste(int **p_int)

and you should pass your pointer like

teste(&m);

The reasoning is simple really. When you pass your pointer to a function it copies the pointer value not the address of the pointer.

gerard4143 371 Nearly a Posting Maven

You could simplify your program like below:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
  char num[10];
  FILE *fd;
  
  if (!(fd = fopen("testdata", "r")))
  {
    fputs("could not open file!\n", stderr);
    exit(EXIT_FAILURE);
  }
  
  while (fscanf(fd, "%s", num) != EOF)
  {
    fprintf(stdout, "num->%s\n", num);
  }
  
  fclose(fd);
  exit(EXIT_SUCCESS);
}

This works for a text file not a binary file...

gerard4143 371 Nearly a Posting Maven

In my very limited view of C++, I find Accelerated C++ and Ruminations on C++ very beneficial to the newest C++'ers.

Its such a great introduction to the understanding of the principles of the language.

Thank-you Andrew Koenig and Barbara Moo..I appreciated yours words of wisdom.

gerard4143 371 Nearly a Posting Maven
CookieData* cookiedata = new CookieData(s,d,t,ipfile);
	void cookie = &cookiedata;
	setfunction(cookie);   //function where I want these values, cookie has to be a void pointer by function declaration

I'm pretty sure you want something like this:

CookieData* cookiedata = new CookieData(s,d,t,ipfile);
	void *cookie = cookiedata;
	setfunction(cookie);
gerard4143 371 Nearly a Posting Maven

You might to include just a bit more of the code...I have no idea what a obj is. Is it a variable, a reference to a variable, a pointer....

Your error. Did you try casting the void pointer to the appropriate type.

gerard4143 371 Nearly a Posting Maven

Remember that general rule...Call delete the same amount of times that you call new.

gerard4143 371 Nearly a Posting Maven

I just tired this and it works

#include <iostream>

int main()
{
    char c;
    do
    {
	std::cout << "Do you want to continue? (y for yes, n for no): ";
	std::cin >> c;

	if (c != 'y' && c != 'n' )
	    std::cout << "Invalid input, enter y or n" << std::endl;
    }
    while(c != 'y' && c != 'n');

    return 0;
}
gerard4143 371 Nearly a Posting Maven

When you enter a value for the character c you also send the newline character into the stream. Could it be that the newline character is causing the problems?

gerard4143 371 Nearly a Posting Maven

So where exactly is your 'if loop'? Which line?

gerard4143 371 Nearly a Posting Maven

Well number one...you don't deallocate both since we're only talking about one chunk of dynamic memory.

In your above example you should delete one pointer to the memory chunk and then set both pointers to NULL.

gerard4143 371 Nearly a Posting Maven

The general rule is...If you call new x times then you must/should call delete x times.

gerard4143 371 Nearly a Posting Maven

You have to, at least, save the original pointer value somewhere so that you can delete and free up the memory...otherwise you create a memory leak.

gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

I would put some std::cout's in here and check some values like degree

Poly Poly:: operator+(const Poly& p)
{
Poly temp;
for (int i=0; i<degree; i++){//look here...what's degree equal?
temp.coefs[i] =coefs[i] + p.coefs[i];
}
return temp;
}
gerard4143 371 Nearly a Posting Maven

Well the default constructor set degree to zero....the first element of the array coefs.

Poly Poly:: operator+(const Poly& p)
{
Poly temp;
for (int i=0; i<degree; i++){//look here...what's degree equal?
temp.coefs[i] =coefs[i] + p.coefs[i];
}
return temp;
}
gerard4143 371 Nearly a Posting Maven

Not really how I would do it but I might not be understanding the question...I would lean towards a solutionss like this:

#include<iostream>

class math
{
	public:
		enum shape {Circle, Rectangle};

		math(double side1, double side2, shape theshape = Rectangle):area(side1 * side2), itsshape(theshape) {}
		math(double rad, shape theshape = Circle):area(3.14 * rad * rad), itsshape(theshape) {}

		double getarea() const {return area;}
		shape getitsshape() const {return itsshape;}
	private:
		double area;
		shape itsshape;
};
                


      
int main()
{
	math shape1(1.1, math::Circle);
	math shape2(1.2, 4.5, math::Rectangle);

	if (shape1.getitsshape() == math::Circle)
	{
		std::cout<<"shape is a circle->"<<shape1.getarea()<<std::endl;
	}
	else
	{
		std::cout<<"shape is a rectangle->"<<shape1.getarea()<<std::endl;
	}

	if (shape2.getitsshape() == math::Circle)
	{
		std::cout<<"shape is a circle->"<<shape2.getarea()<<std::endl;
	}
	else
	{
		std::cout<<"shape is a rectangle->"<<shape2.getarea()<<std::endl;
	}  
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I really don't know why your using overloaded functions when you really should be using overloaded constructors like below:

#include<iostream>

class math
{
  public:
    math(double side1, double side2):area(side1 * side2) {}
    math(double rad):area(3.14 * rad * rad) {}
    double getarea() const {return area;}
  private:
    double area;
};
                


      
int main()
{
  math cir(1.1);
  math rect(1.2, 4.5);
  
  std::cout<<"rect area->"<<rect.getarea()<<std::endl;
  std::cout<<"cir area->"<<cir.getarea()<<std::endl;
  return 0;
}
gerard4143 371 Nearly a Posting Maven

Check out this link on the library cctype

http://www.cplusplus.com/reference/clibrary/cctype/

gerard4143 371 Nearly a Posting Maven

This

#include <iostream.h>

Should be

#include <iostream>

and cout should be std::cout

gerard4143 371 Nearly a Posting Maven

A constructor with NO parameters is the default constructor. So for instance

Car a = new Car(1, "Bob", 10); //uses #1
Car b = new Car(1, 10, "Bob"); //uses #2 
Car c = new Car("Bob", 1, 10); //uses #3
Car d = new Car(); //uses #4 and calls the default constructor

Hope this helps.

Don't you mean Car *a = new Car()...

gerard4143 371 Nearly a Posting Maven

Try this to start out...

#include <string>

using namespace std;

class car 
{

      private:
               int yearModel;
               string make;
               int mpg;
      public:
               car();                  
};

car::car ()
{
       yearModel = 0;//zero is a good value to set numeric primary data types
       mpg = 0;//zero is a good value to set numeric primary data types
       //make string object will call its default constructor automatically 
}

int main()
{
	car mycar;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

That's because you need a separate thread of execution to execute the timers..I would investigate threads.