gerard4143 371 Nearly a Posting Maven

I looked at your header file and you need to add std:: to string so that it reads

std::string

Remember your not using namespace std here because its a header file.

gerard4143 371 Nearly a Posting Maven

Hi guys,
quick question. Does the structure

try
{

}

catch

{

exit(0);
}

require the preprocessor directive

#include<cstdlib>

to be declared at the beginning of the program? The thing is if I don't declare it the visual c++ compiler works fine but if I try in Unix with g++ it doesn't compile...
thanks

You only need cstdlib for exit(0);

gerard4143 371 Nearly a Posting Maven

Why? Memory exhaustion is a very real possibility in a system with finite resources.

gerard4143 371 Nearly a Posting Maven

do you mean this array?

double * dSM = NULL

It depends on the size you pass to new.

gerard4143 371 Nearly a Posting Maven

Those #define true 1 and #define false 0 are just preprocessor macros which will find and replace all true and false and replace them with 1 and 0 respectively.

gerard4143 371 Nearly a Posting Maven

Use either

int* p1 = &p0[0];

or

int* p1 = p0;
gerard4143 371 Nearly a Posting Maven

Your function needs a return type.

double sum_funk(n1,n2,n3,n4)
{}

endl is a stream manipulator not a variable.

gerard4143 371 Nearly a Posting Maven

Look at this line

cin>> n1>> n2 >> n3 >> n4;// >> endl;

Notice what I remarked out.

gerard4143 371 Nearly a Posting Maven

O.K...What weird errors. Could you post them?

gerard4143 371 Nearly a Posting Maven

Are you trying to display the character's value as a hexadecimal?

#include <iostream>

int main()
{
  unsigned char myc = 'o';
  
  std::cout << (int)myc << std::endl;
  std::cout << std::hex << (int)myc << std::endl;
  std::cout << std::oct << (int)myc << std::endl;
  
  return 0;
}
gerard4143 371 Nearly a Posting Maven

You are kidding, where is your code tags?

gerard4143 371 Nearly a Posting Maven

I would loop through your string like below

#include <iostream>
#include <string>

int main()
{
  std::string the_string;
  
  std::cout << "enter your number->";
  std::cin >> the_string;
  std::cout << std::endl;
  
  
  for (unsigned int i = 0; i < the_string.size(); ++i)
  {
    //do your comparisons here
    // (the_string[i] == 'I')
    std::cout << the_string[i] << std::endl;
  }
  return 0;
}

I find it cleaner but maybe that's the C in me talking. Also note that my way uses characters in the comparison not strings.

gerard4143 371 Nearly a Posting Maven

I'm only guessing here but should you be using some sort of trig functions to get your points...

gerard4143 371 Nearly a Posting Maven

I assume your talking about a system monitor that displays your processes and its resources. To know how and what its displaying you'll have to get a look at its source code.

gerard4143 371 Nearly a Posting Maven

Well I can't declare and initialize in the class constructor or else the vector will only exist for the scope of the constructor(instead of the class). I would still have to declare the object in the class and then either push_back objects after calling reserve (or something similar) in the class constructor.

////////////////////////////////////////////////
This is actually part of a larger problem. I was trying to boil it down to its essence.

I am trying to create a vector of objects. Speed is the issue. It is very fast when I can simply do:

vector[myclass] v1(10);

This creates and initializes a vector of ten myclass objects. The first myclass object is created using the myclass constructor and then the myclass copy constructor is used to fill the remaining 9 elements. It is lightning fast, but it doesn't work when I encapsulate the line within another class.

This does not work either.

vector<int> myVofInt;
myVofInt(10);

Are you referring to a static class member?

gerard4143 371 Nearly a Posting Maven

I would use the class constructor to initialize your vector to the appropriate number of elements.

Jsplinter commented: thank you +1
gerard4143 371 Nearly a Posting Maven

I'm not really sure what you mean by 'all of a'....

a[0] is equal to 0.
a is the address of the first element or &a[0].

gerard4143 371 Nearly a Posting Maven

A "default" is just a value or action that happens automatically if some other action or value is not specified in its place.

The switch "default case" is just one example. It is probably the only one example that actually uses the keyword "default". It's the case that executes if no other case matches the value of variable used as a selector. But there are other types of "defaults" in different contexts.

Default function parameters:

int someFunc(int aNumber, int defaultParam = 10) {
  /* ... */
}

In this example, you can call the function as either someFunc(int) or as someFunc(int, int). This is permissible because the second argument/parameter has a default value of 10 if no second argument is provided to the function call.

int multiply(int aNumber, int multiplier = 10);

int main() {
  int product1(0), product2(0);

  product1 = multiply(1);      //calls multiply() with default second argument of 10
  product2 = multiply(2, 20);  //calls multiply(), does not use default value of second argument

  return 0;
}

int multiply(int aNumber, int multiplier) {
  return (aNumber * multiplier);
}

In that sample, product 1 will be 10, and product2 will be 40.

Default class constructors:

class sampleClass {
 public:
   sampleClass();   //"default" constructor
   sampleClass(int);//overloaded or "specified" constructor
};

The default constructor allows declarations such as this:

someClass defaultSomeClassObj;        //declares a "default" "someClass" object
someClass specifiedSomeClassObj(2);   //declares a "specified" "someClass" object

Nice list here's another one to add

What are the default values of the fundamental types?

gerard4143 371 Nearly a Posting Maven

Could you put your question in context by giving us an example...

gerard4143 371 Nearly a Posting Maven

Number one I would change your expression to:

char expression;

and then call a %c in your scanf.

#include <stdio.h>

int main()
{
    
    char expression;

    while ((scanf("%c",&expression)) != EOF)
    {
      fprintf(stdout, "char->%c\n", expression);
    }

    return 0;
}
gerard4143 371 Nearly a Posting Maven

The general rule is, for every time you malloc you have to call free..

Why set a pointer to NULL? Because if you call free on a NULL set pointer nothing happens, also its easy to check if a pointer is NULL by examining its value..

int *ptr = (int*)NULL;

if (!ptr)/*check if pointer is NULL*/
{
/*do something*/
}

Pointers, like everything else in programming should be left in a known/safe state.

gerard4143 371 Nearly a Posting Maven

Ok those both look real simple, but I really need a return value of type string so I can call the function and set it equal to a variable to use in the API Dialog code.

I should have specified but I am still learning.

std::string ftostr(float myfloat)
{
  char ans[20];
  //float myf = 123.0 + (1.0 / 3.0);

  snprintf(ans, 20, "%f", myfloat);

  //std::cout << "ans->" << ans << std::endl;
  return ans;
}

I tried that but my Code::Blocks compiler does not recognise snprintf. I know I am just fumbling around but at least I'm trying.

Did you include the cstdio library? If you want to return a string then cast ans as a string.

gerard4143 371 Nearly a Posting Maven

Did you forget your semi-colon after your structure?

gerard4143 371 Nearly a Posting Maven

Remember your advancing your pointers as you copy into them...Your asking printf to print the end of your string.

gerard4143 371 Nearly a Posting Maven

It really depends, are you talking about a string object or are you talking about a c-string?

gerard4143 371 Nearly a Posting Maven

Try changing your function to:

int burcin(int x)
{
     if (x==1)  return 1; 
     else
     return (x+ burcin(x-1));
     
}
gerard4143 371 Nearly a Posting Maven

In the second example your creating an array of pointers which in your case are 8 bytes.

gerard4143 371 Nearly a Posting Maven

Your copying your string over to a NULL pointer...It points to nothing.

gerard4143 371 Nearly a Posting Maven

Here's a simple way to make the conversion using snprintf().

#include <iostream>
#include <cstdio>

int main()
{
  char ans[20];
  float myf = 123.0 + (1.0 / 3.0);
  
  snprintf(ans, 20, "%f", myf);
  
  std::cout << "ans->" << ans << std::endl;
  return 0;
}
gerard4143 371 Nearly a Posting Maven

In C structures can hold pointers which can point to functions...So in a way, C structures can hold(point to) functions..

gerard4143 371 Nearly a Posting Maven

Hmm thanx gerard I get it . But why their is an extra h with quotes in the output.
If i write char *c="hello" . Their is no extra h and " in the output.
Can you please give the reason for this.Thank a lot in advance.

When you call c with std::cout << c << std::endl; it will try to keep reading/displaying until it finds a '\0'. Why is it displaying the extra 'h'? Its probably the next printable character it found....On my system it printed a whole string of nonsense before it terminated with a '\0'.

Here's my output

0x7fff8fbb9faf A
A����� A

gerard4143 371 Nearly a Posting Maven

std::cout is interpreting c as a character string...You know something that ends with a '\0'.

If you want the address try this:

std::cout<<(void*)c<<"     "<<*c<<std::endl;
gerard4143 371 Nearly a Posting Maven

Take this out of your header file

long addresses[5];

and put it in your main executable...

When you compile def.c you create a label called addresses and when you compile mainfile.c you create another label addresses. When you bring both files together for the final executable it fails because addresses is defined in both def.o and mainFile.o.

gerard4143 371 Nearly a Posting Maven

It means

(*filterdata).Q_angle = Q_angle;

gerard4143 371 Nearly a Posting Maven

thanks for your help, could you reccomend any good c++ books?

Depends...Are you a complete programming rookie or do you know how to program in other languages besides C++.

gerard4143 371 Nearly a Posting Maven

this

'monday'

should be

"monday"

Strings are enclosed with double quotes not single quotes...

If your learning C++ then please get yourself a good book on the subject...Tutorials just don't cut it.

gerard4143 371 Nearly a Posting Maven

Try

ReadFile.read((char*)p_ReadBuff, 1);
gerard4143 371 Nearly a Posting Maven

Yeah I would try

unsigned char m_ReadBuff;
unsigned char *p_ReadBuff = &m_ReadBuff;
gerard4143 371 Nearly a Posting Maven

Try using unsigned int...How is ReadBuff defined?

gerard4143 371 Nearly a Posting Maven

If your using strings...why won't you just

string contentsToAdd += inventory[counter].itemName + " ";
gerard4143 371 Nearly a Posting Maven

Try using something like:

std::string mystr = "the string";
mystr.c_str();//to return a C style string
gerard4143 371 Nearly a Posting Maven

Are you talking about something like below?

#include <iostream>
#include <string>
#include <cstdio>

#define ARR_SIZE 4

struct mystr
{
  std::string mystr;
  int myint;
};

int main()
{
  char *str = new char[10];
  str[0] = '\0';
  std::string ans;
  struct mystr thestr[ARR_SIZE];
  
  thestr[0].myint = 1;
  thestr[1].myint = 2;
  thestr[2].myint = 3;
  thestr[3].myint = 4;
  
  thestr[0].mystr = "the first";
  thestr[1].mystr = "the second";
  thestr[2].mystr = "the third";
  thestr[3].mystr = "the fourth";
  
  for (std::string::size_type i = 0; i < ARR_SIZE; ++i)
  {
    sprintf(str, "%d", thestr[i].myint);
    ans += (std::string)str + " ";
  }
  
  std::cout << "ans->" << ans << std::endl;
  return 0;
}
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

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

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

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

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

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

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>