gerard4143 371 Nearly a Posting Maven

Not sure why you added <int> here?

template <typename T>
Test<int>::Test(T num1)
{
number = num;
}
 
template <typename T>
Test<int> operator+(Test<T> &a, Test<T> &b)
{
return Test(a.number + b.number);
}

I'm no template expert but you can take a look at my code below

#include <iostream>
#include <cstring>

struct mys
{
  double a, b;
};

std::ostream& operator <<(std::ostream & out, const struct mys & s)
{
  return out << s.a << " " << s.b;
}

struct mys operator +(const struct mys & a, const struct mys & b)
{
  struct mys temp;
  memcpy(&temp, &a, sizeof(struct mys));
  temp.a +=  b.a;
  temp.b += b.b;
  return temp;
}

template <typename T>
class my_t
{
  
public:
  
  my_t():the_value() {}
  my_t(T val):the_value(val) {}
  
  my_t operator +(const my_t & a) const
  {
	return this->the_value + a.the_value;
  }
  
  my_t& operator =(const my_t & a)
  {
	if ( this != &a )
	{
	  this->the_value = a.the_value;
	}
	return *this;
  }
  
  T getitsvalue() const { return the_value; }
  
private:
  
  T the_value;
  
};
template <typename T>
std::ostream& operator <<(std::ostream & out, const my_t<T> & m)
{
  return out << m.getitsvalue();
}

int main()
{
  struct mys f = {123.0, 234.0}, g = {34.0, 45.0};
  my_t<struct mys> one(f);
  my_t<struct mys> two(g);
  my_t<struct mys> ans;
  
  ans = one + two;
  
  std::cout << ans << std::endl;
  
  return 0;
}

Note I have a assignment operator=().

gerard4143 371 Nearly a Posting Maven

You can use your first example

#define NINE 0x39
gerard4143 371 Nearly a Posting Maven

If the error is in your test.h file, maybe you should post it. Oh you did. Why do you have the main function in your header file? My mistake, I didn't realize you grouped the header and object file together.

gerard4143 371 Nearly a Posting Maven

If the memory manager can fulfill the malloc request it will return a memory pointer to the allocated memory...if it fails it returns NULL.

If malloc returns a pointer other than NULL then you have your requested memory.

gerard4143 371 Nearly a Posting Maven

The problem is line 13

_matrix.GetM(i, k)

The vector container has no method called GetM().

gerard4143 371 Nearly a Posting Maven

Wow what compiler are you using? I can't believe this wasn't flagged.

string input(string a[], int &cnt)
{
    for (int i = 0; !infile.eof(); i++)
    {
        getline(infile, a[i]);
        cnt = i;
    }
}

string copy(string a[], string b[], int cnt)
{
    for (int i = 0; i < cnt; i++)
    {
        b[i] = a[i];
    }
}

Both functions expect a return type of string but both functions return nothing.

gerard4143 371 Nearly a Posting Maven

This link may give you some preliminary information.

http://en.wikipedia.org/wiki/64-bit

gerard4143 371 Nearly a Posting Maven

You posted your homework assignment? Wow, read the forum rules.

gerard4143 371 Nearly a Posting Maven

Sure, can you tell use what the problem is?

gerard4143 371 Nearly a Posting Maven

You should be able to or your conditions together.

gerard4143 371 Nearly a Posting Maven

Pointers that are not set to NULL or a valid memory address are said to be wild pointers, they may be pointing at anything so are very dangerous.

The general rule is - Set a pointer to a valid memory address or set it to NULL...Always keep your pointers in a known state.

gerard4143 371 Nearly a Posting Maven

I see. How exactly do I sum up all the scores for average?

Try something like below...Note array elements start at zero not 1.

#include <iostream>

#define ARR_SIZE 5

int main()
{
  int totnum[ARR_SIZE];
  signed long sum = 0;
  
  for (size_t i = 0; i < ARR_SIZE; ++i)
  {
	std::cout << "Enter element [" << i << "]->";//note array elements start at 0
	std::cin >> totnum[i];
  }

  for (size_t i = 0; i < ARR_SIZE; ++i)
  {
	std::cout << "Element [" << i << "]->" << totnum[i] << std::endl;
	sum += totnum[i];
  }
  
  std::cout << "sum->" << sum << std::endl;
  return 0;
}
gerard4143 371 Nearly a Posting Maven

And array is just a contiguous block of memory. Its doesn't support the division operator.

gerard4143 371 Nearly a Posting Maven

Well here's a gimme

double studentAverage(const std::vector<struct Student> & vec, int student_index_postion)
{
  double average = 0.0;
  unsigned long count = 0;
  
  for (unsigned long i = 0; i < vec[student_index_postion].exams.size(); ++i)
  {
	++count;
	average += vec[student_index_postion].exams[i];
  }
  
  return average/count;
}

This will calculate the average of the student at student_index_postion in the vector.

gerard4143 371 Nearly a Posting Maven

I have to ask, are you taking the average of each student or are you taking the average of the group of students?

gerard4143 371 Nearly a Posting Maven

I would try something like below

double studentAverage(const std::vector<struct Student> & vec, int student_index_postion)
{
  double average = 0.0;
  unsigned long count = 0;
  
  /*loop through exam scores*/
  
  return average/count;
}
gerard4143 371 Nearly a Posting Maven

It would help if you indicated which function your having problems with.

gerard4143 371 Nearly a Posting Maven

All I can tell you is, get a good book on the subject.

Here's a website, check some of the titles.

http://www.cs.purdue.edu/homes/dec/netbooks.html

especially - Internetworking With TCP/IP Volume III: Client-Server Programming and Applications, Linux/POSIX Socket Version (with D. Stevens)

gerard4143 371 Nearly a Posting Maven

I would investigate the do-while loop

http://msdn.microsoft.com/en-us/library/b0kk5few.aspx

gerard4143 371 Nearly a Posting Maven

Try using std::string.

ben1996123 commented: Simple working answer, fast reply. +3
gerard4143 371 Nearly a Posting Maven

Your using a string format specifier %s for a character %c. Line 12 and 14.

gerard4143 371 Nearly a Posting Maven

I know the GNU g++ complier has limited support with the -std=gnu++0x switch.

gerard4143 371 Nearly a Posting Maven

Maybe if you tried

if ((scode == "r") || (scode == "R"))
gerard4143 371 Nearly a Posting Maven

I am really sorry! This is my call to Initialize.

List *callInitialize () {

    List *L;

    List studentList;
    L = &studentList;

    Initialize(L);

return L;
}

Your function callInitialize is returning a pointer to a local variable studentList? When callInitialize returns that variable will be invalid memory.

gerard4143 371 Nearly a Posting Maven

I see where your calling callInitialize() but not where your calling Initialize().

gerard4143 371 Nearly a Posting Maven

How and what are you calling Initialize (List *L) with.

Note: You have this

L->items[i].name[MAXNAMESIZE - 1] = '\0';

Shouldn't it be

L->items[i].name[strlen(initialize)] = '\0';

...After checking the docs I found strncpy pads the destination with '\0' so my point is null void.

gerard4143 371 Nearly a Posting Maven

you can do like this: char[] values = {'a','b','c','d','e','f','g'};
then address to each value using the vector(array) like this:

values[0],values[1],etc. (a,b,etc.)
you might want to see this tutorial that has also some detailes about char array: http://goldwin-advertising.ro/index.php?option=com_content&view=article&id=10:splitstring&catid=3:howto&Itemid=5

Yeah they probably figured that out 3 years ago when the question was posted.

gerard4143 371 Nearly a Posting Maven

Your not passing the rent array to calculateRental...Also the first array element starts at zero not 1.

gerard4143 371 Nearly a Posting Maven

I can't believe you compiler didn't warn you about this line..

char test = "I like you";

Is this what your trying to do?

#include <stdio.h>

#define ARR_SIZE 3

struct mys
{
  char * my_arr[ARR_SIZE];
};

int main()
{
  size_t i = 0;
  struct mys thes;
  
  char * test1 = "I like you";
  char * test2 = "I like you...not";
  char * test3 = "Hello, world!";
  
  thes.my_arr[0] = test1;
  thes.my_arr[1] = test2;
  thes.my_arr[2] = test3;
  
  for (i = 0; i < ARR_SIZE; ++i)
	fprintf(stdout, "ans->%s\n", thes.my_arr[i]);
  return 0;
}
gerard4143 371 Nearly a Posting Maven

You forgot.

The main function should return an int.

gerard4143 371 Nearly a Posting Maven

How are you calling - void populate_bricks(sprites &game_data)?

gerard4143 371 Nearly a Posting Maven

I just tried your code an it worked for me.

test.c

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

extern int addus(int,int);

int main() 
{

	int a;
	int b;
	int result;
	a=5;
	b=6;

	result = addus(a,b);
	printf("Your value is: %d", result);
	return EXIT_SUCCESS;
}

asmfile.s//note function label, I changed it to addus

.globl addus
addus:

pushl %ebp
movl %esp, %ebp

movl 8(%ebp), %ecx
addl 12(%ebp), %ecx
movl %ecx, %eax

movl %ebp, %esp
popl %ebp
ret

Compile lines

as asmfile.s --32 -o asmfile.o
gcc test.c -c -m32
gcc test.o asmfile.o -o test -m32

Note I had to use the -m32 and --32 switches because my machine is a Intel 64 bit.

gerard4143 371 Nearly a Posting Maven

I would check what you calling the function name in your asm file. Your calling it _addus in the asm file and calling it addus in your main executable. The linker won't be able to resolve the names if they don't match.

gerard4143 371 Nearly a Posting Maven

PS How would I setup virtual memory in linux?
Muffins

Your in luck, Linux already comes with virtual memory set up.

gerard4143 371 Nearly a Posting Maven

DON'T follow Yashwant Kanetkar (as suggested by cse.avinash)...its a popular book in India (and I'm from India, so its a valid suggestion) but its a complete mess...and wrong...and outdated...and non-standard...and i can go on and on...

A better book to follow(for beginners) would be:
A modern approach to C programming by K. N. King

You can also check out the sticky as advised by gerard4143

Happy Learning...:)

I never heard of this book so I check Amazon's reviews - 29/36 reviews 5/5 stars.

http://www.amazon.ca/Programming-Approach-K-N-King/dp/0393969452

gerard4143 371 Nearly a Posting Maven

Sounds like your trying to inject a piece of code somewhere. I don't think the forum encourages hacking practices.

gerard4143 371 Nearly a Posting Maven

It means the function returns a integer reference for a return value. In your example prototype the function probably returns a reference to either a or b.

gerard4143 371 Nearly a Posting Maven

You could check the sticky in this section 'Starting C'. I just noticed it today.

gerard4143 371 Nearly a Posting Maven

Line 12.

Why are you doing this

cin >> calc >> end;

Why >> end;

Is this what your trying to accomplish?

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main () 
{
	string calc;

	cin >> calc;

	if(calc == "calc") 
	{
		system("calc");
	} 
	else 
	{
		cout << "Error..." << endl;
	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Re: Gerard...

Yes, thank you. Is it possible to access the members int y,z of class B? Or would I need a pointer of class B to do that?

If your object is created like

A * pAB = new B;

Then yes you can access members y and z.

If your object is created like

A * pAB = new A;

Then you can't access members y and z because they don't exist.

gerard4143 371 Nearly a Posting Maven

Do you mean something like below?

#include <iostream>

class A
{
public:
  virtual void display_it() const { std::cout << "class A" << std::endl; }
protected:
	int x;
};
class B : public A
{
public:
  void display_it() const { std::cout << "class B" << std::endl; }
	int y, z;
};

class C
{
public:
	C(bool derivedClass)
	{
		if (derivedClass)
		{
			pAB = new B;
		}
		else
		{
			pAB = new A;
		}
	}
	
	void whoami() const { pAB->display_it(); }
	
	A* pAB;
	// other members
};

int main(int argc, char**argv)
{
  C one(true);
  C two(false);
  
  one.whoami();//class B
  two.whoami();//class A
  
  return 0;
}
Jsplinter commented: Thank you. Well explained. +3
gerard4143 371 Nearly a Posting Maven

Correction: class B is derived from class A, so creating an object of type B will automatically create an internal object of A. Object of type C contains a pointer to object of type A

Yes, thank-you for the correction.

gerard4143 371 Nearly a Posting Maven

Class C is derived from class A, so creating an object of type C will automatically create an internal object of A. Object of type C contains an object of type A

gerard4143 371 Nearly a Posting Maven

A simple method would be

char aChar;
char nline;
printf("Please enter a character: ");
scanf("%c%c",&aChar, &nline);
gerard4143 371 Nearly a Posting Maven

aahhh, i see '\n' has an ascii value of 10.
so when an input is given say 'e', '\n' tags along.
now to find a way to either remove it or to consume it with the user input??

Well almost. '\n' doesn't tag along, its entered by the user to indicate he's done inputing values..

Your example - When a user enters a value 'e' he/she really enters/types 'e' and then hits/types the enter key which is the '\n'.

gerard4143 371 Nearly a Posting Maven

Try running this code

#include <stdio.h>
 
int main()
{
    char aChar;
    printf("Please enter a character: ");
    scanf("%c",&aChar);
 
    while (aChar!='x')
    {
        printf("The Letter is %c or the integer is->%d\n", aChar, aChar);
 
        printf("Please Enter another character: ");
        scanf("%c", &aChar);
    }
    return 0;
 
}

Can you figure out what's going on? Hint 10 is new line.

gerard4143 371 Nearly a Posting Maven

try

g++ Numbers.cpp -c
g++ Main.cpp -c
g++ Numbers.o Main.o -o main

gerard4143 371 Nearly a Posting Maven

Huh, what's the question?

gerard4143 371 Nearly a Posting Maven

Try using(including) the limits.h header file into your program.

gerard4143 371 Nearly a Posting Maven

The easiest way, allocate a new larger memory block(array) and then copy the array into it.