tux4life 2,072 Postaholic

Add extern "C++" int addition(int a, int b); to main.cpp (outside your main function), delete the instruction external int addition(a, b); from inside your main function :) ...

main.cpp

#include <iostream>

using namespace std;

// use the external addition function
extern "C++" int addition(int a, int b);

int main()
{
    int summation;
    int a, b;
    cout <<"Please Enter the two digits to add"<<endl;
    cin>>a>>b;
    summation = addition(a, b);
    cout<<"The sum is "<<summation;
    return 0;

}

Now just compile and link them (main.cpp and functions.cpp) togheter ...

tux4life 2,072 Postaholic

I don't get your question, can you please explain it in a way I can understand ?

By the way, If you've some additional time please format your code properly (using code tags was the first (and very good) step, formatting your code is the second one) :) ...

Edit:: Avoid using the system(); command (look here)...

tux4life 2,072 Postaholic

I can't see the link with sprintf , but if you want to know more about it, just google on it (e.g.:http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/) :) ...

Edit:: What do you mean with "sever", it's an English verb, but I think you meant the noun "server" :) ...

tux4life 2,072 Postaholic

Here is my work, can you help me with the second requirement!

Sure we can help you, but that's such a vague question ...
Where are you having problems with ?

tux4life 2,072 Postaholic

I don't get your question ...

tux4life 2,072 Postaholic

What do you actually expect from us? We won't do your homework ...
Edit:: But we can still give you some suggestions here and there :) ...

tux4life 2,072 Postaholic

He knows a lot.

That's why I called him a 'Professor' (with a capital) ...

tux4life 2,072 Postaholic

Can you please post using code tags ?
Edit:: Read this and this :) ...

tux4life 2,072 Postaholic

Ok i got 3 errors now, and im using Boreland C++. Teacher made me declare the variables globally, as im going to be using it throughout the functions.

First of all: It's Borland and not Boreland :P, secondly: "as im going to be using it throughout the functions", that's not a real reason to declare your variables globally as you can still pass it to the function as an argument (or pass a pointer to it to a function) :) ...

tux4life 2,072 Postaholic

Rep++ for ArkM :P !!

tux4life 2,072 Postaholic

And we're getting programming lessons from Professor Ancient Dragon :P ...

tux4life 2,072 Postaholic

I've built a small program around your (fixed) search function and the situation you described:

#include <iostream>

using namespace std;

struct student
{
	int studentID;
};

int searchAr(student studentar [], int size, int searchid)
{
    for(int i = 0; i<size; i++)
          if (studentar[i].studentID == searchid) return i;
    return -1;
}

int main()
{
	student stud[5];
	stud[4].studentID = 2;
	cout << "Student with ID \'2\' found at place: " << searchAr(stud,5,2) << " (in the array of students)" << endl;
	return 0;
}

I have to say that it's working in the way it was intended or am I wrong ?

tux4life 2,072 Postaholic

I've played around with it more and it seems like this would make more sense but now it returns nothing.

int searchAr(student studentar [], int size, int searchid)
{
    int i;
    for(i = 0; i<size; i++)
    {
          if (studentar[i].studentID == searchid)
             {
             return i;
             }
    }
    return -1;
}

>>but now it returns nothing: that's not true, the function is always returning a value :) ...

tux4life 2,072 Postaholic

ok makes sense Tux.. now before i let go of the char* ... when i try using it without "delete" it works fine .... but when i use the delete keyword .. it gives me a heap corruption detected debug error

The code is:

void Card::setFirstName(){
	char* inputFirstName = new char;
	cout << "\n\nEnter First Name - ";
	cin >> inputFirstName;
	firstName = inputFirstName;
	cout << firstName;
	delete inputFirstName;
}

The instruction: char* inputFirstName = new char; does actually the same as char inputFirstName; but without cleaning up the memory automatically :) ...
If the user enters more than one character ( cin >> inputFirstName; ) this will crash your program !!
(as there was reserved memory for storing one character only :))
If you really want to use the new operator, then try the following: char* inputFirstName = new char[20]; (remember only an input of 19 characters is allowed), if you use a string ( string inputFirstName; ) you'll have less trouble :) ...

tux4life 2,072 Postaholic

Did you read this ? (probably not)

tux4life 2,072 Postaholic

Hey Thanks...

Ok I am getting it to work using strings ... but i am trying to learn using char*

and if I don't initialize it by doing char* varName = "/0" .. it gives me the variable is used without being initialized

My question is where exactly can we/should we use char* instead of strings.

C++ strings are much easier and comfortable to use/work with, char* is used much more in C than in C++ I think, so if you want to make it yourself difficult, use char* otherwise: just use a string (if your compiler supports it)

tux4life 2,072 Postaholic

char* is just declaring a pointer to a character (or an array of characters), and what do you do? You're just assigning a value to it, that's a strong mistake !
You first have to reserve/assign memory to that pointer before you can store a value at the memory address where the pointer points to, you assign memory to a pointer using the new or new[] operator, and when you dynamically assign memory to a pointer using these operators, you also have to release it, this can be done using delete and delete[] :) ...

Edit:: new and delete ; new[] and delete[]
Edit:: Or just do what siddhant said:

Better change to this:

const int MAX=20;
char userData[MAX];

I am smelling that you are using an old compiler. If not, why not using std::strings?

tux4life 2,072 Postaholic

Look carefully at the code of your searchAr function: the for-loop is executing one time only :) ...

int searchAr(student studentar [], int size, int searchid)
{
    int i;
    for(i = 0; i<(size-1); i++)
    {
          /* This for-loop is executing only one time */
          if (studentar[i].studentID == searchid)
             {
             return i;
             }
             else
             {
             return -1;
             }
    }
}

Actually this code is wrong ...

int searchAr(student studentar [], int size, int searchid)
{
    for(int i = 0; i<size; i++)
          if (studentar[i].studentID == searchid) return i;
    return -1;
}

This is already a lot better :) ...

Edit:: for(i = 0; i<(size-1); i++) why is the condition i<(size-1) , I would make i<size of it ...

tux4life 2,072 Postaholic

> There's only one place in your function where it explicitly returns -1, (in the else code block) ...

> Did you set the studentID variable after you declared a struct-variable from it ?
If no, that's the problem :)

tux4life 2,072 Postaholic

>>And what if you consider the following ?
Tell me if this work:

int array[5]={1,23};
//this var will still tell you 5 while the OP wants 2
int element_count = sizeof(array)/sizeof(int);

Oh, he meant it like that, he only wants to count the elements which aren't zero I assume then ?

Edit:: You can create a simple object which uses methods to add and remove data from the array and which keeps a counter of elements in the array, and If I'm not wrong you can also use the STL vector container ...

tux4life 2,072 Postaholic

To create a linked list it might be easier to use the list-container from the Standard Template Library, it's still a suggestion ...

By the way, Turbo C Compilers are already quite old, I recommend you to switch to a new one like MinGW (which comes with Code::Blocks and Dev-C++) or the Free Borland Compiler (but it starts getting dated :))

tux4life 2,072 Postaholic

And what if you consider the following ?

int array[5]={1,23,45,7,8};
int element_count = sizeof(array)/sizeof(int); /* now this variable holds the value '5' ... */

Edit:: This won't work with multidimensional arrays :) ...

tux4life 2,072 Postaholic

>>There are several free compilers, such as Code::Blocks, Dev-C++, and VC++ 2008 Express.
( Note to OP: I am talking to Ancient Dragon, this has got nothing to do with you)
I never knew Code::Blocks and Dev-C++ are compilers!! I thought they were IDEs ;)
Or am I sensing it right?

Yes you're right, but I think Ancient Dragon actually wanted to simplify it for the OP :P ...

tux4life 2,072 Postaholic

What??????

COBOL came out in like the fifties/sixties and it didnt get Object-Oriented features added in until the late 90s/early 2000s..

That's true, COBOL was invented to improve the readability of a program (however unlikely) but it still belongs to the non-structured languages ...

To the OP: If C++ s*cked, why are there then so much professional programmers which prefer it ?

tux4life 2,072 Postaholic

> int examgrd[MAX][3] you forgot to put a semicolon ';' at the end, change it to int examgrd[MAX][3]; > I get 9 other errors also :), what compiler are you using and under which filename did you save this stuff ? (As it's a C program it has to have the '.c' file-extension ...

> Why are you declaring all your variables global ? This isn't a good practice, it's really bad when it's actually not needed :) ...

tux4life 2,072 Postaholic

I wrote something equal using strings, look at this snippet :) so you can see how it' done ...

Hope this helps !

tux4life 2,072 Postaholic

it also supports the idea of how c++ sucks.

> C++ doesn't suck, in fact it has inspired the languages Java and C#, do these also s*ck then ?
> C++ has inspired the creation of those two other languages, I don't think this is because C++ s*cks :) ...

tux4life 2,072 Postaholic

tux> If your computer has more than a GB of RAM that wouldn't be a problem

I tried this on a computer with 1.5GB RAM, it compiled and ran without crashing, I tried to put a value in the array, that did also work, but displayin that value: result was just nothing :) ...

tux4life 2,072 Postaholic

Is this program compiling/working ? or are there still compile errors/bugs in it ?

tux4life 2,072 Postaholic

WH: You're saying that the code below does not crash on your system?

#include <iostream>
int main() {
    int matrix[512][512][512];
    matrix[0][0][0] = 1;
    std::cout << matrix[0][0][0] << '\n';
}

Assuming 32-bit ints, that's half a gigabyte!

If your computer has more than a GB of RAM that wouldn't be a problem I think :) (it may also depend on which compiler he's using)

tux4life 2,072 Postaholic

Please tell me you are not trying to do .NET in visual studio 6 or something....

I hope he isn't :P

tux4life 2,072 Postaholic

I have got to say this .NET stuff is very frustrating.

This frustrating .NET stuff is actually much easier to design a program's GUI in :) ...

tux4life 2,072 Postaholic

Or this but it's pretty much the same :) ...

tux4life 2,072 Postaholic

With me using a vector worked and int matrix[512][512]; didn't (but it still compiled correctly :))

tux4life 2,072 Postaholic

Output stream named infile... That's cool! ;)

That was just a logical mistake from me :P ...

tux4life 2,072 Postaholic

ofstream infile; // create a new output stream infile.open ("[I]yourfile[/I]", fstream::app); // open the file for appending

tux4life 2,072 Postaholic

Yeah, I tried this, it was compiling ...
I stored a value in it but when displaying with cout it didn't display anything ...

You're probably better off with a vector:

// Create
vector< vector<int> > vec(512, vector<int>(512));
// Write
vec[2][3] = 10;
tux4life 2,072 Postaholic

No it didn't. There are some new functions used that aren't present in previous versions.

So because of some new functions you can't use the old ones anymore and it doesn't compile?
Can you post your compiler's output please ?

tux4life 2,072 Postaholic

But as the input stream still has characters in it, you will have to call it twice to freeze the program before it closes, like this:

cin.get();
cin.get();

Or you could use cin.ignore to clean the input buffer, but cin.get is less typing :P !

tux4life 2,072 Postaholic

OK, but your first step should actually be to draw the playing field, then you program the mouse events (when a place on the field is clicked), and after that the actual game :)

By the way, please post using code tags ...

tux4life 2,072 Postaholic

???!
I have no comments...

It's just easier to understand :P

tux4life 2,072 Postaholic

Please avoid using system("PAUSE"); (here are the reasons), use cin.get(); instead ...

tux4life 2,072 Postaholic

Does the following work ?

bool is_valid (char& s)
{
    if('1' <= s || '2'>= s) return true;
    return false;
}

Edit:: No it doesn't change something :)

tux4life 2,072 Postaholic

Try doing it in the following way:

struct ROPES
{
    /* members */
};

int fillStruct (ifstream& fin, ROPES ropes[], int n);

int main()
{
    /* Your code */
    return 0;
}

int fillStructs (ifstream& fin, ROPES ropes[], int n) {
    /* Your code */
}
tux4life 2,072 Postaholic

I know. It's optional though, and I usually do it. But that doesn't really help with my problem. It's still saying ROPES was not declared

Try declaring ROPES globally :)

tux4life 2,072 Postaholic

You can't figure out where you go wrong ?

What is © cplusplus.com, 2000-2009 - All rights reserved - v2.2.1 this ?

tux4life 2,072 Postaholic

Please post using code tags !

tux4life 2,072 Postaholic

You don't have to specify an array's upperbound if you pass it to a function int fillStruct (ifstream& fin, ROPES ropes[MAXROPES], int n) change it to int fillStruct (ifstream& fin, ROPES ropes[], int n)

tux4life 2,072 Postaholic

Trust me, the program I have in my compiler right now.
Is correct, I already changed the errors already.

But when I hit 2, and then x.
Nothing happens.

Can you please post us a copy of your fixed code (without typos this time) so we can test it and give a suggestion about how to fix this ...

tux4life 2,072 Postaholic

Maybe this code snippet might be helpful too :) ...