• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C++ Help?

Status
Not open for further replies.
Naka said:
I'm really glad I learned C++ in college as my foundation for programming. When I graduated I got a job doing C#/ASP.NET. When I first started learning C# it seemed so much easier after having a solid grasp on C++. I've been doing it for 5 years professionally now and I think I'd have a hard time going back haha.
I had a similar experience and I think I agree with you. I think understanding what is really going on "under the hood" is important before you start dealing with high-level languages.
 

usea

Member
My schools teaches the absolute fundamentals in java (variables, functions, loops, OO concepts), and after that there are no classes on a specific language. You learn more concepts, not necessarily a language. Lots of times you're given a choice on which language to use, and sometimes it's whatever the professor prefers.

I think it's a great approach. Languages are not really a big deal. Honestly the best thing you can learn from an undergrad in CS is how to learn more things on your own. You can't even learn 1% of what you need to know by the time you graduate; you need to develop the skills to absorb new material, new languages and tools etc.
 

teh_pwn

"Saturated fat causes heart disease as much as Brawndo is what plants crave."
Naka said:

C and C++ combined are 27%. Sure they aren't the same language, but there aren't that many differences between them (malloc, casting, structural programming focused versus classes, free, templates, object focused). To say that C++/C are dead and that C# is dominating seems a bit off. If you're in school and all of the schools are pushing these newer languages despite over 1/4 of the market, I'd go with C++ (or Java). Seems to be where the monies are.

And really you don't have to limit yourself to one language. Just pick one that you can learn the concepts from, learn the syntax after you get the job if needed.
 

ultron87

Member
Ugh, I wish my job was in a man's programming language instead of Coldfusion. Oh well, can't complain that much, since it's a job.
 

X05

Upside, inside out he's livin la vida loca, He'll push and pull you down, livin la vida loca
ZealousD said:
Real men program in ML.

Oh dear god never program in ML.
Truth.

real men program in Haskell
 

AcciDante

Member
New question! I'm not sure if I can articulate this properly, but here I go :lol I'll try to write an incomplete example.

Code:
struct Student
{
	char name[NAME_SIZE];
	int idNum;
	int *tests;
	double average;
	char grade;
};

int main()
{
	Student *stdPtr;

	stdPtr = new Student[10];  //ten students

	stdPtr[0]->idNum = 789234; //how to assign values to struct variables?

	stdPtr[0]->tests = new int[5]; //5 tests? Is this how I would make a new int
								//array for the # of tests?
}

So, is how I made an array of Student correct? Is stdPtr[0] how you access the individual things I just created in that array? (changing the subscript, of course) Is that last part how I would then create an int array for tests for each student?

I wasn't too clear on this from the book examples.
 

soc

Member
AcciDante said:
New question! I'm not sure if I can articulate this properly, but here I go :lol I'll try to write an incomplete example.

Code:
struct Student
{
	char name[NAME_SIZE];
	int idNum;
	int *tests;
	double average;
	char grade;
};

int main()
{
	Student *stdPtr;

	stdPtr = new Student[10];  //ten students

	stdPtr[0]->idNum = 789234; //how to assign values to struct variables?

	stdPtr[0]->tests = new int[5]; //5 tests? Is this how I would make a new int
								//array for the # of tests?
}

So, is how I made an array of Student correct? Is stdPtr[0] how you access the individual things I just created in that array? (changing the subscript, of course) Is that last part how I would then create an int array for tests for each student?

I wasn't too clear on this from the book examples.

I believe you should be using
Code:
stdPtr[0].idNum = 789234;
stdPtr[0].tests = new int[5];

Remember that stdPtr[0] is the same as saying *(stdPrt+0), so you don't need to use the pointer access operator.
 

AcciDante

Member
Oh, I thought I had to dereference it... I need to go to bed now, but I'll try fixing it up tomorrow and seeing what happens. Thanks!
 

Undubbed

Member
Man, C++ is crazy. It was my second language after Q-Basic, but it took forever for me to actually finish a project with C++. That's more my fault than C++ though. I did finish something and that was Tic-Tac-Toe using Allegro. After that I've decided to just program in Python until I hit a significant speed snag.

Before Tic-Tac-Toe, I kinda stupidly tried to make an NES emulator, but due to bugs that I had no idea how to fix and the fact that I did virtually no planning or source control, I had to stop. I did get a few games working(Donkey Kong and Balloon fight most notably) so it wasn't totally fruitless but yeah...

Now, I want to make a program that will help you plot out maps either for hardcore games that don't have maps or to help conquer a game without using a guide. It's going to be big!
probably, not
 

Zeppu

Member
AcciDante said:
New question! I'm not sure if I can articulate this properly, but here I go :lol I'll try to write an incomplete example.

So, is how I made an array of Student correct? Is stdPtr[0] how you access the individual things I just created in that array? (changing the subscript, of course) Is that last part how I would then create an int array for tests for each student?

I wasn't too clear on this from the book examples.

Quick question? Do you need to create the array on the heap? Usually the reason to do such a think is to allow for dynamic size for the array.

If you don't then just do:

Code:
int main()
{
	Student stdPtr [10]; //ten students

	stdPtr[0].idNum = 789234;
	stdPtr[0].tests = new int[5];
}
 

dogmaan

Girl got arse pubes.
could someone explain to me step by step how the fastSqrt function works by bit shifting a float as an integer?
 
I guess a general question. So I declared a variable in the private part of my header file. And in the source (whatever the .cpp file is called), I try referencing it in a method declared in the public part but it keeps saying it's inaccessible. I'm not really sure what the issue is. Can't a source file access all variables, regardless of where they were declared in its header file (well, within limit. Shouldn't be able to access those in public and private)?
 

AcciDante

Member
I finished my program, but it crashes after data is inputted. I've been piecing it together function by function, trying to figure out which one is broken, and haven't been able to find what's crashing it, but my average isn't working. Here's the code I've been piecing together, and my average just returns some weird numbers. What's wrong with it?

Code:
#include <iostream>
#include <iomanip>
using namespace std;

const int NAME_SIZE = 25;

struct Student
{
	char name[NAME_SIZE];
	int idNum;
	int *tests;
	double average;
	char grade;
};

void getData(Student *, int, int);
void getAverage(Student *, int, int);
void assignGrade(Student *, int);
void displayData(Student *, int, int);


int main()
{
	int people,
		numTests;

	Student *students;

	cout << "This program will gather names, ID numbers, and test scores, of students\n"
		<< "and display the averages and grades of those students in\n"
		<< "a handy table\n\n";

	do{
		cout << "How many students are there?(positive values only): ";
		cin >> people;
	}while (people <= 0);

	do{
		cout << "How many test scores per student?(positive values only): ";
		cin >> numTests;
	} while (numTests <= 0);

	cout << endl;

	students = new Student[people];

	for (int i = 0; i < people; i++)
	{
		students[i].tests = new int[numTests];
	}

	getData(students, people, numTests);

	cout << fixed << setprecision(2);

	displayData(students, people, numTests);

	delete [] students;
	students = 0;

	return 0;
}

void getData(Student *s, int people, int numTests)
{ 

	for (int i = 0; i < people; i++)
	{
		cout << "What is the name of student " << (i + 1) << "?: ";
		cin.ignore();
		cin.getline(s[i].name, NAME_SIZE);

		cout << "What is " << s[i].name << "\'s ID Number? (4 digits)?: ";
		cin >> setw(4) >> s[i].idNum;

		do{
			cout << "What is " << s[i].name << "'s score on test #"
				<< (numTests - (numTests - 1)) << "(positive values only): ";
			cin >> s[i].tests[0];
		}while (s[i].tests[0] < 0);

		for (int count = 1; count < numTests; count++)
		{
			do{
				cout << "Test #" << (count + 1) << "? (positive values only): ";
				cin >> s[i].tests[count];
			}while (s[i].tests[count] < 0);
		}

	}
}

void displayData(Student *s, int people, int numTests)
{
	
	for (int i = 0; i < people; i++)
	{
		cout << endl;
		cout << s[i].name << endl
			<< setw(4) << s[i].idNum << endl;

		for (int count = 0; count < numTests; count++)
		{
			cout << s[i].tests[count] << endl;
		}

		cout << "Average is " << s[i].average << "%\n";

	}
}

void getAverage(Student *s, int people, int numTests) //something wrong in here?
{
	for (int i = 0; i < people; i++)
	{
		int total = 0;

		for (int count = 0; i < numTests; count++)
		{
			total += s[i].tests[count];
		}

		s[i].average = total / numTests;
	}
}

edit: Is there a better way to post my code?
 

Zoe

Member
AcciDante said:
edit: Is there a better way to post my code?

Nope, that's exactly the way you want to post it.

Just gotta say off the bat, it's pretty odd to see a do...while statement. I don't think I even used it once as a student.
 

dogmaan

Girl got arse pubes.
Zoe said:
Nope, that's exactly the way you want to post it.

Just gotta say off the bat, it's pretty odd to see a do...while statement. I don't think I even used it once as a student.

They use it in the C++ class in my HND, I don't use it, I just use while(something)
 

Zoe

Member
Edit: whoops, didn't read the next block

Edit2:

Code:
		do{
			cout << "What is " << s[i].name << "'s score on test #"
				<< [B](numTests - (numTests - 1))[/B] << "(positive values only): ";
			cin >> s[i].tests[0];
		}while (s[i].tests[0] < 0);

Why do you go through the trouble of doing that? It has to be 1 no matter what.
 

AcciDante

Member
I don't know why I did that, haha. I used a do-while, because I was lazy and didn't feel like writing a statement that explicitly said it had to be positive :lol See anything wrong with the averaging?
 

Sharp

Member
Zoe said:
Nope, that's exactly the way you want to post it.

Just gotta say off the bat, it's pretty odd to see a do...while statement. I don't think I even used it once as a student.
Really? I use it all the time when the circumstances are right. There's tons of shit built into C++, why not use it when it's convenient?
 

Zoe

Member
AcciDante said:
I don't know why I did that, haha. I used a do-while, because I was lazy and didn't feel like writing a statement that explicitly said it had to be positive :lol See anything wrong with the averaging?

It's in your iterator.
 

Doytch

Member
Sharp said:
Really? I use it all the time when the circumstances are right. There's tons of shit built into C++, why not use it when it's convenient?
I think it's dependant on whatever you're used to. When I picked up Python, I wanted to use a dowhile loop and Google had to tell me that it doesn't exist. If I'd learned Python as my first language, I doubt I'd ever use a dowhile loop.
 

jvalioli

Member
AcciDante said:
I don't know why I did that, haha. I used a do-while, because I was lazy and didn't feel like writing a statement that explicitly said it had to be positive :lol See anything wrong with the averaging?
Add some cout's to help yourself with debugging. For example, if I were trying to debug that, I would print out all the test scores(assuming its not super big?) and see if there is anything odd in that.


Zoe said:
It's in your iterator.

Haha, really common mistake.
 

phalestine

aka iby.h
anyone know any good tutorials on classes?

I am having a hard time with printing the content of the private part of the class. I know you have to create a function to get the values.

Code:
class Apartment
{
public:
	bool set_Building(int b); // used to set values to buildingNumber and apartmentNumber
	
private:
	int buildingNumber;
	int apartmentNumber;
	
};

int main(){
Apartment track[100];

.
.
.// already added stuff to the array, just dont know how to print a list of whats inside
}
 

rinker

Member
main doesnt have access to apartments' private data, so have the apartments class do the printing

make a public method in your apartment class with just 1 line to print the private data members, and then in main you can print it by calling the public method like:

track.print();

edit: or what he said ^, might as well get used to using getters and setters

also, to answer your first question.. its probably not the best, but i like cplusplus.com
 

phalestine

aka iby.h
jvalioli said:
Write the getter function for those values and do print(track[index].getterFunction());


rinker said:
main doesnt have access to apartments' private data, so have the apartments class do the printing

make a public method in your apartment class with just 1 line to print the private data members, and then in main you can print it by calling the public method like:

track.print();

edit: or what he said ^, might as well get used to using getters and setters

also, to answer your first question.. its probably not the best, but i like cplusplus.com



Thanks guys it worked :)

Yeah I use cplusplus.com its actually not a bad site.
 

Magni

Member
So is this the official Programming Help thread? I've got a problem in C where I'm just confusing myself with structs of structs with some pointers mixed in ><

(ignore the missing return value in main(), Num Lock issue, as well as the line 35 n++ while loop, I'd totally forgotten about string.h and strlen() before taking the screens..)

The code:

UvP6j.png


The gcc error output:

QYTXF.png


There are lots of errors, but they're all linked, I just need to understand how to initialize nbBI correctly at line 37 and where to put *'s and &'s..

Sorry for having pics instead of text, I'm coding this on Fedora in a VM and I'm having trouble C&P-ing over into W7 (mouse is acting all weird in the VM and Ctrl-A won't work obviously).

edit: The gist of the function is to convert a string representing a "big" integer into a new type: BigInteger, consisting of its sign as an int and its absolute value as a doubly linked list of blocks of four digits, with "19201452210" becoming NULL <- 2210 <-> 145 <-> 192 -> NULL for example.
 

jvalioli

Member
nbBi.sign = 0; is how you would change the variables in the struct.

I'm not entirely sure what you're trying to do, but besides the syntax there seems to be a lot of other things wrong with your program. The newBigInteger method is really weird and I don''t think it does what you want it to do.

edit:gah just saw your edit. Ignore what I said.

You're going to want to be returning a bigInteger* and mallocing like your commented out code. use nbBi->sign =0 to assign values.
 

Magni

Member
jvalioli said:
nbBi.sign = 0; is how you would change the variables in the struct.

I'm not entirely sure what you're trying to do, but besides the syntax there seems to be a lot of other things wrong with your program. The newBigInteger method is really weird and I don''t think it does what you want it to do.

edit:gah just saw your edit. Ignore what I said.

You're going to want to be returning a bigInteger* and mallocing like your commented out code. use nbBi->sign =0 to assign values.

>< I'd been doing algo for three hours and wanted to test the C code to see if it worked at all before continuing, and forgot to switch from algo syntax to C syntax.. Thanks, trying it out now =)
 

teh_pwn

"Saturated fat causes heart disease as much as Brawndo is what plants crave."
MagniHarvald said:

Your code is very hard to read. Spell out your variables so that anyone can understand it. Whenever you look at it in 2 weeks or if you work at a company and someone looks at it in 5 years, it's going to be insanely confusing.

I think you need to review dynamic memory allocation and how that lives in the heap while function return values exist on the stack. You've got malloc in comments that would be copied to the stack, and returned in the stack as a return value to the calling function. That would be a memory leak - there would be this malloced chunk of memory just floating in the heap.

Line 38 is exactly what the compiler says. You've got your lvalue backwards.
nbBI.sgn because nbBI is the struct and sgn is the data member contained.
 

teh_pwn

"Saturated fat causes heart disease as much as Brawndo is what plants crave."
jvalioli said:
I know you said to ignore the while loop, but just for reference it will never terminate. '\0' is null.

Indeed. That's what the compiler is complaining about in the very first line of compiler log. Backslash initiates an escape sequence making '\0' a single character. In C, single quotes is for single characters. That's why it doesn't like '/0' because that's two characters.

Style wise
while(nbS != '\0')
could be
while(nbS)
or
while(nbS != NULL)
 

Magni

Member
Thanks for the replies guys. Sorry if the code is hard to read, I haven't added the doc to the C code yet, this is just the very start of a project due early January..

And that while loop is a total clusterfuck, I didn't spend any time on it during the algo phase and hadn't done any work on strings in C since last term so I kinda did "n'importe quoi" ><

nbBI.sign = 0; instead of sign(nbBI) = 0; is an easy switch, I just messed up the syntaxes going back and forth between algo and C. And so forth for pretty much everything else.

So now the only problems left are "warnings" :

The updated code (full function this time since I haven't touched the typedef's) :

LwBTv.png

(ignore the double i = 0; assignment)

The new gcc error output (much shorter already thanks to you guys :D ) :

YCxbi.png


I know gets is "dangerous", but we're allowed to use it for this course. Is there a simple alternative?

The pow error is because gcc doesn't link math.h automatically, so no biggie, but what is up with the other errors? Thanks again guys =)
 

jvalioli

Member
All your variables are allocated on the stack and won't exist when the function exists. I also think the logic of your program is wrong but haven't taken a good look at it. edit: Yeah logic is wrong.

atoi error needs a pointer so you want atoi(&nBS) or atoi(nBS+i)
 

teh_pwn

"Saturated fat causes heart disease as much as Brawndo is what plants crave."
Yeah, he's getting stack addresses in this doubly linked list. It *might* work if your project is small enough and you're lucky, but you don't own that memory. As soon as the scope of that nested Block variable ends in the loop, that memory is recycled.

You'll need to use malloc & free.
 

Magni

Member
jvalioli said:
All your variables are allocated on the stack and won't exist when the function exists. I also think the logic of your program is wrong but haven't taken a good look at it. edit: Yeah logic is wrong.

atoi error needs a pointer so you want atoi(&nBS) or atoi(nBS+i)


Yes I noticed the missing & afterwards. You're probably right concerning the logic, seeing as I get a "Segmentation fault (core dumped)" message when running the program..

Doesn't surprise me, this is the first time I've had to do this sort of stuff, so I was bound to mess up the first time ><

edit: and yeah, I was wondering why I wasn't getting any errors while compiling without using any malloc's or free's. It compiled all right, but it didn't run well :lol

Thanks once more, back to my lectures for me.
 

teh_pwn

"Saturated fat causes heart disease as much as Brawndo is what plants crave."
MagniHarvald said:
Yes I noticed the missing & afterwards. You're probably right concerning the logic, seeing as I get a "Segmentation fault (core dumped)" message when running the program..

Doesn't surprise me, this is the first time I've had to do this sort of stuff, so I was bound to mess up the first time ><

Yeah, seg faults can be caused by writing to memory you don't own.
 

jvalioli

Member
teh_pwn said:
Yeah, seg faults can be caused by writing to memory you don't own.
Seg fault is on nbBI.abs->prev = NULL.

But yeah, with a tiny program it might let you get away with bad memory management.
 

Magni

Member
jvalioli said:
Seg fault is on nbBI.abs->prev = NULL; actually.

While running gdb I got that the seg fault was in the gets() call, which seemed weird..

And I don't want to get away with bad memory management, I don't want to pick up bad habits when I'm just starting :lol
 
Status
Not open for further replies.
Top Bottom