C/C++ Thread

kobaj

VIP Member
I was able to build and link to a DLL using the steps here but a console application using DevC++:
http://msdn.microsoft.com/en-us/library/ms235636(v=VS.90).aspx

How big is your project? Are you able to post your code?

I'm able to build a DLL and link it within my solution. But I'm trying to link to the DLL without it being within my solution. Does that make sense? Or are you supposed to add all DLLs to your solution in order to use them? (ie, there is no way to just use references unless I can get the mythical .lib file working?) I found a way to link DLLs during runtime without a .lib, but I don't have that requirement on this project (ie, it can be done and is preferred at compile time).

Anyway, I'll 2shared the project. DynamicLibrary is the DLL I've made, while Homework4 is the project trying to use DynamicLibrary's DLL. I've deleted all the appropriate .user files so it won't dick with your Visual Studio files (if you use Visual Studio that is...) I know its not commented yet and probably doesn't make a /lick/ of sense, I apologize but I found out it was due Tuesday and was in a HUGE rush...and then I missed the deadline...so I figure I might as well finish it to learn something.

Long story short, I'm sorry for its un-professional polish and completely understand if you don't want to deal with trying to figure it out.

/rambling

Anyway, Thank you :).
 

Cromewell

Administrator
Staff member
Unfortunately I don't have visual studio in my development VM. I've opened it in Dev C++, looks like it's close enough for what I need to see :)

The general process is build your DLL, add it as a reference to your project and (this is the step I suspect you may be missing) add bst.h to your project to access your DLL class. This header, or cpp file I guess as you could put it anywhere, is where you'd have __declspec(dllimport).

I'll show you my code, the actual process of linking will be a little different than what I need to do though.

DLL File:
Code:
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


// MathFuncsDll.h

namespace MathFuncs
{
    class DLLIMPORT MyMathFuncs
    {
    public:
        // Returns a + b
        double Add(double a, double b);

        // Returns a - b
        double Subtract(double a, double b);

        // Returns a * b
        double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        double Divide(double a, double b);
    };
}



#endif /* _DLL_H_ */

Code:
// MathFuncsDll.cpp

#include "MathFuncsDll.h"
#include <stdexcept>

using namespace std;

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}

Project Referencing DLL:
Code:
// MyExecRefsDll.cpp

#include <iostream>

#include "MathFuncsDll.h"

using namespace std;

int main()
{
    MathFuncs::MyMathFuncs *mathInstance = new MathFuncs::MyMathFuncs();
    double a = 7.4;
    int b = 99;

    cout << "a + b = " <<
        mathInstance->Add(a, b) << endl;
    cout << "a - b = " <<
        mathInstance->Subtract(a, b) << endl;
    cout << "a * b = " <<
        mathInstance->Multiply(a, b) << endl;
    cout << "a / b = " <<
        mathInstance->Divide(a, b) << endl;

    delete mathInstance;

    system("PAUSE");
    return EXIT_SUCCESS;
}

Hope that helps a little.
 

kobaj

VIP Member

Very peculiar. My professor was saying to reference the header file with brackets like #include <bst> (perhaps thats for those magical .lib libraries.) I'll play around with it some more this weekend.

I think the cruel irony is referencing things in c++ visual studio is a joke. You have to add half a million PATH="5$.903" bull declarations to its build. Whereas C# visual studio its right click > add reference > done.

But yes, I'll try making a mini project this weekend with math functions, slowly getting it working and building up before trying to get this dll working with my main program again.

On a completely unrelated note (homework for my next class): does COFO have an assembly thread? :D
 

Cromewell

Administrator
Staff member
Nope, if you have questions you can make a thread and I'll add it to the sticky. It has been a while since I've written any assembly though.
 

Strokes

New Member
C++ help

Hey guys. I have been working on a project that asks the user for a file destination, reads in letters, most characters and punctuation, sorts from greatest to least occurring, then prints out the number of that occurring character (or whatever) and the percent:

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct valDatFor
	{
	public:	
		double num;
		char ch2;
		double average;
	};

const int fortySix = 46;

void getFileName(ifstream& inFile)
{
	
	cout << "Enter the location of the '.txt' file to be opened:";
	char filename[301];
	cin.getline(filename, 300);

	inFile.open(filename); 
	
	if(inFile.fail() )
	exit(0);
}

void countWords(ifstream& file)
{
	int words = 0;
	const char space = ' ';
	const char tab = '\t';
	const char nully = '\0';
	
	string real;
	
	
	do
	{
    file >> real; 

		if (space || tab || nully)
			words++;

	}	
	while(!file.eof() );
	
	
	cout << "There are " << words << " words in this file\n";
	
	file.clear();
	file.seekg(0, std::ios::beg); // Moves to begining of file

	cout << "Pass return to BoF\n";
}

void countChars(ifstream& file, valDatFor lett_punc_symb[fortySix])
{

	char ch;

	int chCounter = 0;
	
	for(int i = 0; i < fortySix; i++)
	{
		lett_punc_symb[i].num = 0;
	}
	
	lett_punc_symb[0].ch2 = 'a';
	lett_punc_symb[1].ch2 = 'b';
	lett_punc_symb[2].ch2 = 'c';
	lett_punc_symb[3].ch2 = 'd';
	lett_punc_symb[4].ch2 = 'e';
	lett_punc_symb[5].ch2 = 'f';
	lett_punc_symb[6].ch2 = 'g';
	lett_punc_symb[7].ch2 = 'h';
	lett_punc_symb[8].ch2 = 'i';
	lett_punc_symb[9].ch2 = 'j';
	lett_punc_symb[10].ch2 = 'k';
	lett_punc_symb[11].ch2 = 'l';
	lett_punc_symb[12].ch2 = 'm';
	lett_punc_symb[13].ch2 = 'n';
	lett_punc_symb[14].ch2 = 'o';
	lett_punc_symb[15].ch2 = 'p';
	lett_punc_symb[16].ch2 = 'q';
	lett_punc_symb[17].ch2 = 'r';
	lett_punc_symb[18].ch2 = 's';
	lett_punc_symb[19].ch2 = 't';
	lett_punc_symb[20].ch2 = 'u';
	lett_punc_symb[21].ch2 = 'v';
	lett_punc_symb[22].ch2 = 'w';
	lett_punc_symb[23].ch2 = 'x';
	lett_punc_symb[24].ch2 = 'y';
	lett_punc_symb[25].ch2 = 'z';
	lett_punc_symb[26].ch2 = '!';
	lett_punc_symb[27].ch2 = '?';
	lett_punc_symb[28].ch2 = ',';
	lett_punc_symb[29].ch2 = '.';
	lett_punc_symb[30].ch2 = ':';
	lett_punc_symb[31].ch2 = ';';
	lett_punc_symb[32].ch2 = '\'';
	lett_punc_symb[33].ch2 = '\"';
	lett_punc_symb[34].ch2 = '@';
	lett_punc_symb[35].ch2 = '#';
	lett_punc_symb[36].ch2 = '$';
	lett_punc_symb[37].ch2 = '%';
	lett_punc_symb[38].ch2 = '&';
	lett_punc_symb[39].ch2 = '*';
	lett_punc_symb[40].ch2 = '(';
	lett_punc_symb[41].ch2 = ')';
	lett_punc_symb[42].ch2 = '+';
	lett_punc_symb[43].ch2 = '=';
	lett_punc_symb[44].ch2 = '/';
	lett_punc_symb[45].ch2 = '-';

	lett_punc_symb[46].num = 0;
	lett_punc_symb[46].ch2 = '~';

	cout << "Pass struct assignment\n";
	
	do
	{
		file >> ch;
		
		switch(ch)
		{
			case 'a':
			case 'A':
			lett_punc_symb[0].num++;
			break;

			case 'b':
			case 'B':
			lett_punc_symb[1].num++;
			break;
			
			case 'c':
			case 'C':
			lett_punc_symb[2].num++;
			break;

			case 'd':
			case 'D':
			lett_punc_symb[3].num++;
			break;

			case 'e':
			case 'E':
			lett_punc_symb[4].num++;
			break;

			case 'f':
			case 'F':
			lett_punc_symb[5].num++;
			break;

			case 'g':
			case 'G':
			lett_punc_symb[6].num++;
			break;

			case 'h':
			case 'H':
			lett_punc_symb[7].num++;
			break;

			case 'i':
			case 'I':
			lett_punc_symb[8].num++;
			break;

			case 'j':
			case 'J':
			lett_punc_symb[9].num++;
			break;

			case 'k':
			case 'K':
			lett_punc_symb[10].num++;
			break;

			case 'l':
			case 'L':
			lett_punc_symb[11].num++;
			break;

			case 'm':
			case 'M':
			lett_punc_symb[12].num++;
			break;

			case 'n':
			case 'N':
			lett_punc_symb[13].num++;
			break;

			case 'o':
			case 'O':
			lett_punc_symb[14].num++;
			break;

			case 'p':
			case 'P':
			lett_punc_symb[15].num++;
			break;

			case 'q':
			case 'Q':
			lett_punc_symb[16].num++;
			break;

			case 'r':
			case 'R':
			lett_punc_symb[17].num++;
			break;

			case 's':
			case 'S':
			lett_punc_symb[18].num++;
			break;

			case 't':
			case 'T':
			lett_punc_symb[19].num++;
			break;

			case 'u':
			case 'U':
			lett_punc_symb[20].num++;
			break;

			case 'v':
			case 'V':
			lett_punc_symb[21].num++;
			break;

			case 'w':
			case 'W':
			lett_punc_symb[22].num++;
			break;

			case 'x':
			case 'X':
			lett_punc_symb[23].num++;
			break;

			case 'y':
			case 'Y':
			lett_punc_symb[24].num++;
			break;

			case 'z':
			case 'Z':
			lett_punc_symb[25].num++;
			break;

			case '!':
			lett_punc_symb[26].num++;
			break;
			
			case '?':
			lett_punc_symb[27].num++;
			break;

			case ',':
			lett_punc_symb[28].num++;
			break;

			case '.':
			lett_punc_symb[29].num++;
			break;

			case ':':
			lett_punc_symb[30].num++;
			break;

			case ';':
			lett_punc_symb[31].num++;
			break;

			case '\'':
			lett_punc_symb[32].num++;
			break;

			case '\"':
			lett_punc_symb[33].num++;
			break;

			case '@':
			lett_punc_symb[34].num++;
			break;

			case '#':
			lett_punc_symb[35].num++;
			break;

			case '$':
			lett_punc_symb[36].num++;
			break;

			case '%':
			lett_punc_symb[37].num++;
			break;

			case '&':
			lett_punc_symb[38].num++;
			break;

			case '*':
			lett_punc_symb[39].num++;
			break;

			case '(':
			lett_punc_symb[40].num++;
			break;

			case ')':
			lett_punc_symb[41].num++;
			break;

			case '+':
			lett_punc_symb[42].num++;
			break;

			case '=':
			lett_punc_symb[43].num++;
			break;

			case '/':
			lett_punc_symb[44].num++;
			break;

			case '-':
			lett_punc_symb[45].num++;
			break;
			
			default:
			break;
		}
	}
	while(!file.eof() );
	
	cout << "Pass switch\n";
}


void sortChars(valDatFor lett_punc_symb[fortySix])
{
	int out, loc, temp;

	for(out = 1; out < 45; out++)
		if(lett_punc_symb[out].num < lett_punc_symb[out - 1].num)
		{
			temp = lett_punc_symb[out].num;
			loc = out;

			do
			{
				lett_punc_symb[loc].num = lett_punc_symb[loc - 1].num;
				loc--;
			}
			while (loc > 0 && lett_punc_symb[loc - 1].num > temp);

			lett_punc_symb[loc].num = temp;
		}

}

void printCount(valDatFor lett_punc_symb[fortySix])
{
	for(int i = 0; i < 46; i++)
	{
		lett_punc_symb[46].average += lett_punc_symb[i].num;
	}
	
	ofstream outFile;
	
	outFile.open("C:\\Users\\Strokes\\Desktop\\Results.txt");

	outFile << "Letter:		Number:		  Percent:		\n";
	
	for(int i = 0; i < 46; i++)
	{
		outFile << "        " << lett_punc_symb[i].ch2 << "		    ";
		outFile << lett_punc_symb[i].num << "			" << lett_punc_symb[i].num / lett_punc_symb[46].average << "%" << endl;

	}

	cout << "Letter:		Number:		  Percent:		\n";
	
	for(int i = 0; i < 46; i++)
	{
		cout << "        " << lett_punc_symb[i].ch2 << "		    ";
		cout << lett_punc_symb[i].num << "			" << lett_punc_symb[i].num / lett_punc_symb[46].average << "%" << endl;

	}

}

int main()
{
	ifstream file;
	
	getFileName(file);
	
	countWords(file);
	
	valDatFor lett_punc_symb[fortySix];
	
	countChars(file, lett_punc_symb);

	sortChars(lett_punc_symb);

	printCount(lett_punc_symb);
	
	return 0;
}


Now the trouble is it crashes in the middle of running. I think it makes it to the switch structure, but crashes there. There are no syntax errors because it complies fine. Is there anything I'm missing in that switch structure?
 

Cromewell

Administrator
Staff member
I've moved your post to the C++ thread.

It's failing on line 125 (file >> ch, just before the switch). It's a segmentation fault, calling certain file member functions seems to cause it, I can't get anything about the location of the file pointer. The file reports that it is open but I can't get anything else from it.
 

mihir

VIP Member
Very peculiar. My professor was saying to reference the header file with brackets like #include <bst> (perhaps thats for those magical .lib libraries.) I'll play around with it some more this weekend.

I think the cruel irony is referencing things in c++ visual studio is a joke. You have to add half a million PATH="5$.903" bull declarations to its build. Whereas C# visual studio its right click > add reference > done.

But yes, I'll try making a mini project this weekend with math functions, slowly getting it working and building up before trying to get this dll working with my main program again.

On a completely unrelated note (homework for my next class): does COFO have an assembly thread? :D


Do you already know a bit of assembly??
Which processor will you be working on?(32bit or 16bit)
Any modern Day processor or something like 8086/8085.
The best way to learn Assembly is writing programs in C/C++ and saving the temporary files at the time of compilation and then studying those files with respect to the original .c file.

And how much do you already know about the segment registers?
 

Bananapie

New Member
Maybe my topic belonged more here.

I am Computer Science major who is working in JAVA right now. Learned GUI and up to Linked List and some other stuff. Classes and whatnot.

I assume as I progress, it will be required to know C++.

This summer, I am wanting to teach myself some C++, and try and get a small grasp, or as much as I can get, before I transfer this coming fall.

Does anyone have any books, or tutorial pages to work off of?
As well, what good compilers would one use for C++?

I work with NetBeans in JAVA.
 

Dystopia

Active Member
Maybe my topic belonged more here.

I am Computer Science major who is working in JAVA right now. Learned GUI and up to Linked List and some other stuff. Classes and whatnot.

I assume as I progress, it will be required to know C++.

This summer, I am wanting to teach myself some C++, and try and get a small grasp, or as much as I can get, before I transfer this coming fall.

Does anyone have any books, or tutorial pages to work off of?
As well, what good compilers would one use for C++?

I work with NetBeans in JAVA.

Bloodshed works good for Java, though I really like Visual Studio 2010 if you can afford it.

This is the book I used, really liked it:http://www.amazon.com/Starting-Out-...=sr_1_3?ie=UTF8&s=books&qid=1305063802&sr=8-3

If you are interested, I'd sell you my book for cheap, its got some water damage and applesauce damage....but all the pages are there. PM me if you are interested.
 
Last edited:

wolfeking

banned
Ok. Getting a little confused here. Working in simple C++. Trying to use a online compiler to convert the simple program "hello world", but I am getting a bunch of errors with it.

The compiler I am using right now (at school) is http://www.comeaucomputing.com/tryitout/.

The original program code is:
Code:
#include <iostream.h>
// main: generate some simple output
void main ()
{
cout << "Hello, world." << endl;
return 0
}
the first error says the <iostream.h> isn't valid, and to use <iostream>. Ok, so I corrected that to
Code:
 #include <iostream>
// main: generate some simple output
void main ()
{
cout << "Hello, world." << endl;
return 0
}
and now I am getting an error that says
"ComeauTest.c", line 3: error: return type of function "main" must be "int"
So use int main() OR int main(int argc, char *argv[])
void main ()
^

"ComeauTest.c", line 5: error: identifier "cout" is undefined,
Perhaps use "std::cout", or "using namespace std;"?
Did you #include <iostream>?"
cout << "Hello, world." << endl;
^

"ComeauTest.c", line 5: error: identifier "endl" is undefined,
Perhaps use "std::endl", or "using namespace std;"?
Did you #include <iostream>?"
cout << "Hello, world." << endl;
^

"ComeauTest.c", line 6: error: return value type does not match the function type
return 0
^

"ComeauTest.c", line 7: error: expected a ";" (perhaps on the previous statement)
}

^cout << "Hello, world." << endl;
return 0
}
can someone help me fix this, so that I can actually have achieved something today?
 

Cromewell

Administrator
Staff member
Ok. Getting a little confused here. Working in simple C++. Trying to use a online compiler to convert the simple program "hello world", but I am getting a bunch of errors with it.

The compiler I am using right now (at school) is http://www.comeaucomputing.com/tryitout/.

The original program code is:
Code:
#include <iostream.h>
// main: generate some simple output
void main ()
{
cout << "Hello, world." << endl;
return 0
}
the first error says the <iostream.h> isn't valid, and to use <iostream>. Ok, so I corrected that to
Code:
 #include <iostream>
// main: generate some simple output
void main ()
{
cout << "Hello, world." << endl;
return 0
}
and now I am getting an error that says can someone help me fix this, so that I can actually have achieved something today?

The errors are telling you exactly what is wrong:
Code:
 #include <iostream>
// main: generate some simple output

using namespace std; // <------- added namespace
int main () //<-----changed void to int
{
cout << "Hello, world." << endl;
return 0; //<---added semicolon
}
 

wolfeking

banned
thanks cromewell.
Its was a success that time.

And sorry. Sometimes I dont have the patience to read things, and my english skills arent that good either.
 

Troncoso

VIP Member
Well, I can't connect to ebay to see the book you have, but if it's more or less a c++ programming textbook the best thing to do is just read it. If you are actually interested in programming, you will absorb the information. When it tells you to do an exercise or example, go beyond that. Once you've done it, add on to it, figure out ways to make the program work the way you think it should. Learning to program is like learning a new language. You don't have to to be able to pull any function from the top of your head, but if you know how to use them and the syntax for the language, you'll have no problems. But, for real the book is best place to start. You really don't find information organized in such a way on the web.
 

Dystopia

Active Member
Newbie here. I want to learn C++, so I downloaded the Visual C++ Express 2010 from Microsoft, ordered a C++ book (http://cgi.ebay.com/ws/eBayISAPI.dl...38&ssPageName=ADME:L:OC:US:1123#ht_500wt_1202)

I just don't know where to go from here. No classes at school... not really any resources in the city I live in.

Well, you have a beginner book. I don't know what compiler that book uses, but if it isn't VS, then there is a line you need to add. Anyway, just read from page one, go from there, should get you going. Depending on how serious you are on learning C++ (hobby, or possible career?), you could look into dual enrollment so that you can also go to college for C++ at the same time.
 

S.T.A.R.S.

banned
If you want to learn C++,you must be prepared on long studying and a lot of time spent in the front of the book and the monitor lol.Same appplies for a chair lol.

Since you are a beginner,why not start with VB?
VB is not bad and you can make whatever you want in it just like in C++.And if someone tells you that's impossible,don't listen to that.I know few people who made powerful 3D applications in VB 6.0 so that tells you everything.

C++ is too hard for a beginner trust me.There are just too many things you won't understand and if you keep reading,but did not understand first 20 pages then it is a very small chance you will understand anything on the next pages since it is getting harder and harder as you are reading.

I recommend you to start learning easier language and once you start understanding the logic and syntax then you can move to harder language.
I also started with VB long time ago and even VB was hard for me back then so imaginate how C++ would be hard for me.Same applies for you.
After a year I went to C# which was harder one and after it I went to C++ which was also harder one.
And now after a long time of studying I fully understand all three of them.

I was also like you and I wanted C++ immediately at the beginning too,but as soon as started reading the book about it,I just gave up because it did not make any sense to me back then many years ago.

What I am trying to say is that it is a lot better to start with something easier made for the beginners then immediately jump to C++ and expect you will understand everything because you won't lol.

I know many people who immediately go to C++.I ask them why and they simply tell me:"I heard it is the best one."
But what they did not hear is that you need some knowledge in order to understand it and also they did not hear that simple VB can be used to make same things just like in C++ if you know how.

My recommendation for you is VB 2010.Install it and start from the stratch.And by the stratch I mean on something so simple like a message which will appear when you click the button:

When the user clicks the button,do the following action:

MessageBox.Show("Hello Mr. ScotALot and welcome to the programming world!")

As you can see above,I used words which are case sensitive,dots,brackets and quotes.And these are just SOME of the most basic things you need to understand in order to even move on.Because DOTS,BRACKETS,QUOTES and so on...are used in many other areas too.
Here we also have the key words and many many many other things I did not even mention which you need to understand in order to start udnerstanding the logic of the programming.Once you do then you will start understanding the C++ too.

Trust me C++ is just too hard to understand if you do not even know the basics.You can try of course it is your choice,but soon you will realize that it is not that easy lol.
 
Top