• 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.

Zeppu

Member
True, it may be unneeded for class assignments and the like, but it really requires zero effort and makes everything clearer at a glance. Again, it's to add finesse to the program really.
 
When I put:

inputFile.open("numberData.txt");

In a function I get an undeclared identifier (because of the inputFile).

Is there no way to put it in a function? My professor's not answering his email. :( The instructions say to build a function that reads the file (among a lot of other things). Any help would be appreciated.
 

Slavik81

Member
Mr Sandman said:
When I put:

inputFile.open("numberData.txt");

In a function I get an undeclared identifier (because of the inputFile).

Is there no way to put it in a function? My professor's not answering his email. :( The instructions say to build a function that reads the file (among a lot of other things). Any help would be appreciated.
I'm presuming that inputFile is a variable declared outside of your function. In order to access it within the function, you need to pass a reference to it in as a parameter.

I'm going to guess that it's an std::fstream, so what you need is something like this:

Code:
void readFile(std::fstream& file)
{
   file.open("numberData.txt");
   if(!file.is_open())
   {
      std::cout << "Could not read file: numberData.txt" << std::endl;
      return;
   }

   // todo: reading stuff.
}

int main()
{
   std::fstream inputFile;
   readFile(inputFile);
   return 0;
}

The example above is stupid and pointless, but it illustrates how 'file' in readFile and 'inputFile' in main can both be the same object.

Of course, why you'd be declaring a stream outside a function and passing it in is unclear. The fstream contains no information until its opened, so you might as well create one within your function, rather than passing it in.

Without knowing more information, it's hard to say what you should be doing.
 

Slavik81

Member
Actually, thinking about it more, you probably did give me enough information if I make a few guesses.

I'll guess that you need to read a bunch of numbers from a text file called numbers.txt. For that, you'll need an fstream. Create one in your function. Open it in text mode. Then, use the extraction operator (>>) to take formatted data from it.

You can find an example here, though it uses the cin stream rather than your inputFile:
http://www.cplusplus.com/reference/iostream/istream/operator>>/
 
Hey, thanks for the replies.

I have fstream and here's my function:

Code:
	//loads the numbers into the array
void loader(int numbers[], const int SIZE, int count)
{
		inputFile.open("numbData.txt");

	for (count = 0; count < SIZE; count++)
		inputFile >> numbers[count];

	inputFile.close();
}

Excuse me if it's a bit newbish (and I haven't cleaned up the formatting lol).

So should include it in the parameter as:

std::fstream& inputFile

If so, what about the prototype and when I call it?
 

Zeppu

Member
Are you using the fstream elsewhere? If you aren't you can just declare inputFile there within the function since you're opening, reading and then closing.

Code:
void loader(int numbers[], const int SIZE, int count)
{
	[B]std::fstream[/B] inputFile.open("numbData.txt");

	for (count = 0; count < SIZE; count++)
		inputFile >> numbers[count];

	inputFile.close();
}
 
When I do that I get this error:

...error C2143: syntax error : missing ';' before '.'

It's talking about the period before open. Any ideas?
 

Zeppu

Member
Mr Sandman said:
When I do that I get this error:

...error C2143: syntax error : missing ';' before '.'

It's talking about the period before open. Any ideas?

Whoops of course. Sorry. Declare the file first then call open.

std::fstream inputFile;
inputFile.open("numbData.txt");
 

hemtae

Member
Anybody here experienced in FLTK?

Code:
#include <sstream>
#include "std_lib_facilities.h"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Output.H>
#include <FL/fl_draw.H>

using namespace std;

int BUFFER = 5;
int time_left = 60;
int score_total;
Fl_Output* time_display;
Fl_Output* score_display;
Fl_Window* win;
stringstream buffer;

void time_cb(void*) 
{	
	buffer.str(""); buffer << --time_left;
	time_display->value(buffer.str().c_str());
	if (time_left > 0)
		Fl::repeat_timeout(1.0, time_cb);
}
int main() 
{
    win = new Fl_Window(600, 600, "WAM!");
    win->begin();
	time_display = new Fl_Output(BUFFER + 35, BUFFER, 25, 16, "Time:" );
    score_display = new Fl_Output(BUFFER*2 + 105, BUFFER, 50, 16, "Score:");
	fl_rectf(300,300,20,20,FL_BLACK);
    win->end();
    Fl::add_timeout(1.0, time_cb);
    win->show();
    return Fl::run();
}

When I run that bit of code, I can't get the rectangle to show up, just the timer and the score display
 
rooflapimp said:
incorrect.size() returns an unsigned integer. You could either cast it to an int or just change the loop variable i to be unsigned as well, if you want to get rid of the warning.

I know I'm late to the thread, but that's not correct. It returns a size_t type, which is a memsize type. An unsigned int is not the same size as size_t for 64 bit platforms.
 

AcciDante

Member
Learning about linked lists, having a slight problem with one of the examples.

Code:
#ifndef NUMBERLIST_H
#define NUMBERLIST_H

class NumberList
{
private:
	struct ListNode
	{
		double value;
		struct ListNode *next;
	};

	ListNode *head;

public:
	NumberList()
	{	head = NULL;	}       //NULL is undeclared identifier?

	~NumberList();

	void appendNode(double);
	void insertNode(double);
	void deleteNode(double);
	void displayList() const;
};

#endif

It tells me NULL is an undeclared identifier. What's wrong with it?
 

poweld

Member
AcciDante said:
Learning about linked lists, having a slight problem with one of the examples.

Code:
#ifndef NUMBERLIST_H
#define NUMBERLIST_H

class NumberList
{
private:
	struct ListNode
	{
		double value;
		struct ListNode *next;
	};

	ListNode *head;

public:
	NumberList()
	{	head = NULL;	}       //NULL is undeclared identifier?

	~NumberList();

	void appendNode(double);
	void insertNode(double);
	void deleteNode(double);
	void displayList() const;
};

#endif

It tells me NULL is an undeclared identifier. What's wrong with it?
Try setting head = 0
 

AcciDante

Member
At the bottom of this code, my displayPostOrder function gets an error saying it doesn't take one arguments, but the two functions above it are pretty much the same, yet they don't get the error. Help me, gaf! (last chapter of the book, btw!)

Code:
#ifndef INTBINARYTREE_H
#define INTBINARYTREE_H
#include <iostream>

class IntBinaryTree
{
private:
	struct TreeNode
	{
		int value;
		TreeNode *left;
		TreeNode *right;
	};

	TreeNode *root;

	void insert(TreeNode *&, TreeNode *&);
	void destroySubTree(TreeNode *);
	void deleteNode(int, TreeNode *&);
	void makeDeletion(TreeNode *&);
	void displayInOrder(TreeNode *) const;
	void displayPreOrder(TreeNode *) const;
	void displayPostOder(TreeNode *) const;

public:
	IntBinaryTree()
	{	root = NULL;	}

	~IntBinaryTree()
	{	destroySubTree(root);	}

	void insertNode(int);
	bool searchNode(int);
	void remove(int);
	void displayInOrder() const
	{	displayInOrder(root);	}

	void displayPreOrder() const
	{	displayPreOrder(root);	}

	void displayPostOrder() const        // function does not take 1 arguments
	{	displayPostOrder(root);	}
};
#endif
 

Magni

Member
Not technically C++, but still, maybe someone can help..

Code:
printf("Please type in a big integer: x1 = ");
gets(s1);
        
printf("Please type in another big integer: x2 = ");
gets(s2);

This used to work perfectly, but now the first gets call doesn't work properly.. I haven't touched that bit of code, I just shifted it: it used to be in main(), and I've placed it in another function. The variables are global so no there's no problem on that end.

Here's the program's output (in red is user input):

Code:
[u]currently[/u]

Please type in a big integer: x1 = Please type in another big integer: x2 = 125841652116311361116163114

[u]used to be[/u]

Please type in a big integer: x1 = 146815684163514861365841634
Please type in another big integer: x2 = 125841652116311361116163114

What's wrong? I feel like I'm missing something very obvious..
 

poweld

Member
MagniHarvald said:
Not technically C++, but still, maybe someone can help..

Code:
printf("Please type in a big integer: x1 = ");
gets(s1);
        
printf("Please type in another big integer: x2 = ");
gets(s2);

This used to work perfectly, but now the first gets call doesn't work properly.. I haven't touched that bit of code, I just shifted it: it used to be in main(), and I've placed it in another function. The variables are global so no there's no problem on that end.

Here's the program's output (in red is user input):

Code:
[u]currently[/u]

Please type in a big integer: x1 = Please type in another big integer: x2 = 125841652116311361116163114

[u]used to be[/u]

Please type in a big integer: x1 = 146815684163514861365841634
Please type in another big integer: x2 = 125841652116311361116163114

What's wrong? I feel like I'm missing something very obvious..
Do not use gets. Ever. Try fgets and report back.
 

AcciDante

Member
Does anyone know about iPhone development? I'm going through a book I got, and am stuck. Is there a specific thread or can I just ask here?
 

poweld

Member
AcciDante said:
Does anyone know about iPhone development? I'm going through a book I got, and am stuck. Is there a specific thread or can I just ask here?
Go ahead and ask here since this is the biggest programming thread. Soon enough we should probably make a Programming OT.
 

Magni

Member
poweld said:
Do not use gets. Ever. Try fgets and report back.

Yeah I hate gets but we were told to use it for this project. Though I suppose I won't get in trouble for using fgets..

Well, it still doesn't work. The only difference is that I get -38 instead of 0 for the first big integer now.
 

AcciDante

Member
Alright, so I'm getting an 'Error: MapKit/MapKit.h: No such file or directory.' when I try to use the MapKit framework. I added the framework into the project the same way I added corelocation, which worked fine, but MapKit won't seem to work. When I open the MapKit framework in Finder, there's only a unix executable file in its folder, and no MapKit.h. If there is supposed to be more files in there, I don't know how I've lost them, seeing as this is the first time I've been in there. Any solutions?

I'm on Xcode 3.1.2 If it has anything to do with that. I can't upgrade because I don't have snow leopard :|
 

Slavik81

Member
MagniHarvald said:
Yeah I hate gets but we were told to use it for this project. Though I suppose I won't get in trouble for using fgets..

Well, it still doesn't work. The only difference is that I get -38 instead of 0 for the first big integer now.
IIRC, you can have problems if you mix scanf and gets. Do you call scanf some time before that first gets?
 

Magni

Member
Slavik81 said:
IIRC, you can have problems if you mix scanf and gets. Do you call scanf some time before that first gets?

I call scanf in two other functions, and these functions are called by main() both before and after my function using fgets.

edit: and I also call scanf in main(). fgets however is by itself in that function.

A bit of Googling tells me it has to do with the left over "Enter" from the scanf screwing with the fgets.. I'll try to get it fixed on my own first now, thanks for pointing me in the right direction =)

edit2: mugu: 10^99
 

Chichikov

Member
AcciDante said:
Alright, so I'm getting an 'Error: MapKit/MapKit.h: No such file or directory.' when I try to use the MapKit framework. I added the framework into the project the same way I added corelocation, which worked fine, but MapKit won't seem to work. When I open the MapKit framework in Finder, there's only a unix executable file in its folder, and no MapKit.h. If there is supposed to be more files in there, I don't know how I've lost them, seeing as this is the first time I've been in there. Any solutions?

I'm on Xcode 3.1.2 If it has anything to do with that. I can't upgrade because I don't have snow leopard :|
Either your include statement is wrong or you didn't "installed" mapkit the right way (I know nothing about mapkit).
It just can't find that h file.
 
MagniHarvald said:
Yeah I hate gets but we were told to use it for this project. Though I suppose I won't get in trouble for using fgets..

Well, it still doesn't work. The only difference is that I get -38 instead of 0 for the first big integer now.

How large are your characters buffers, s1 and s2?

nvm huge buffers.
 

AcciDante

Member
Chichikov said:
Either your include statement is wrong or you didn't "installed" mapkit the right way (I know nothing about mapkit).
It just can't find that h file.

Yeah, that's what I figured. I don't know where the h file went, or if it was ever there. I can really reinstall the tools because there's a new version out that requires snow leopard. Damn apple.
 

Slavik81

Member
MagniHarvald said:
I call scanf in two other functions, and these functions are called by main() both before and after my function using fgets.

edit: and I also call scanf in main(). fgets however is by itself in that function.
The problem is probably that scanf is leaving junk in your keyboard buffer. It's a really complex function that you're better off avoiding. I've heard there are some good ways of doing things by just reading the entire input line with fgets and then parsing the string with sscanf, so it can't screw up your input buffer... but I'm really not a C guy.

Common problems:
http://www.gidnetwork.com/b-59.html
(Though I wouldn't worry about performance overhead like he was.)

Duoas in this thread details a potential way of avoiding scanf entirely:
http://www.cplusplus.com/forum/beginner/9462/
 

Chichikov

Member
AcciDante said:
Yeah, that's what I figured. I don't know where the h file went, or if it was ever there. I can really reinstall the tools because there's a new version out that requires snow leopard. Damn apple.
Just search for it on your hard drive, it should be fairly easy to fix.
 

Magni

Member
Slavik81 said:
The problem is probably that scanf is leaving junk in your keyboard buffer. It's a really complex function that you're better off avoiding. I've heard there are some good ways of doing things by just reading the entire input line with fgets and then parsing the string with sscanf, so it can't screw up your input buffer... but I'm really not a C guy.

Common problems:
http://www.gidnetwork.com/b-59.html
(Though I wouldn't worry about performance overhead like he was.)

Duoas in this thread details a potential way of avoiding scanf entirely:
http://www.cplusplus.com/forum/beginner/9462/

Alright I'll be reading those links, thanks.

Right now I have:

x1 = -38 (regardless of what I type)
x2 = 10* (what I type) - 38 (as in 25002 instead of 2504)

Gonna try to get rid of scanf, I miss the days of c++, cin, and cout..

=================

Alright, I've tried many many things, I've settled on three temporary string variables for the three different scanf's I had, tmp2 (waiting for 0 or 1), tmp3 (waiting for a value from 0 to 10), and tmp11 (waiting for an unsigned long int).

I've replaced
scanf("%_", &var);
with
fgets(tmpX, X, stdin);
sscanf(tmpX, "%_", &var);

It ALMOST works. But I still have that weird "-38" I was talking about (the 10* however is gone). However, instead of printing a value 38 lesser than expected (see above), it'll print "value-38".

When I do the sum of my two values, it'll print "valueOfSum-76". Where does this -38 come from?

Even that isn't a "stable" error actually, here's an output for you guys in you can make any more sense of it than I can..

Code:
BIG INTEGER LIBRARY TEST PROGRAM
================================
1 : addition
2 : subtraction
3 : multiplication
4 : euclidian division
5 : modulo
6 : greatest common divisor
7 : least common multiple
8 : factorial
9 : binomial coefficient

10 : all

0 : quit


test chosen : 1

Please type in a big integer: x1 = 25000
Please type in another big integer: x2 = 50000

x1 = 25 000-38 what?
x2 = 50 000-38 again

sign of x1: 1
sign of x2: 1

x1 != x2
x1 < x2

x1 + x2 = 75 000-76 and again


Repeat?
=======
1 : Yes
0 : No

answer : 1
Please type in a big integer: x1 = Please type in another big integer: x2 = 38 once again back to the first error now

x1 = -38 -38 ?
x2 = 342 10 * 38 (what I typed for x2) - 38

sign of x1: 1 ???
sign of x2: 1

x1 != x2
x1 < x2

x1 + x2 = 304 


Repeat?
=======
1 : Yes
0 : No

answer : 0


BIG INTEGER LIBRARY TEST PROGRAM
================================
1 : addition
2 : subtraction
3 : multiplication
4 : euclidian division
5 : modulo
6 : greatest common divisor
7 : least common multiple
8 : factorial
9 : binomial coefficient

10 : all

0 : quit


test chosen : goes right back to the first test
Please type in a big integer: x1 = 38
Please type in another big integer: x2 = -38

x1 = 342 10 * 38 - 38 ??
x2 = -342 -||10 * (-38)| - 38| ?? 

sign of x1: 1
sign of x2: -1

x1 != x2
x1 > x2

x1 + x2 = 342 sum for BI's of different sign not implemented yet, so normal


Repeat?
=======
1 : Yes
0 : No

>< Thanks for the help guys..
 

jvalioli

Member
AcciDante said:
Any pro tips on getting textbooks for cheap? I need to get this for next semester...
http://www.amazon.com/How-Program-7th-Paul-Deitel/dp/0136117260/ref=sr_1_1?ie=UTF8&qid=1292983536&sr=8-1

I'll probably rent it if I can't find it super cheap. Anyone used that book?
I wouldn't buy that book based on the cover alone. :lol

Taking a C++ class next semester? Honestly, you probably won't need to book at all. Haven't used any of my CS books yet (and I'm stupid enough to keep buying them).
 

Zoe

Member
If you just need to know syntax and stuff, you should be able to find all you need online.

If you have to do problems out of the book for class... try to borrow the book from the library.
 

Big Chungus

Member
I need to know more about Test Driven Development programming.

Does anyone know much about it or maybe some sites that deal with it using C++

The only sources i've found have them using a different language or they're using some sort of funky software.
 

Slavik81

Member
fna84 said:
I need to know more about Test Driven Development programming.

Does anyone know much about it or maybe some sites that deal with it using C++

The only sources i've found have them using a different language or they're using some sort of funky software.
What exactly do you need to know? TDD is really a philosophy. You shouldn't really need examples in any particular language or with any particular framework to understand it.

It's very simple. You start with your test first. You stop writing your test when you have the minimum amount of code that will cause the test to fail. At that point, you start writing the implementation such that you have the minimum amount of code required to get the test to pass. Then back to the test to find a case that will make it fail. Then back to the implementation to make that pass. Etc.

Generally, you'll use a test library, which for C++ probably means QTest, Google Test, CppUnit, CxxUnit, CppUnitLite, etc. They just make writing tests a lot easier. If you just want to learn how to write tests in C++, you'll want resources that cover your specific framework.

So are you looking for an introduction to C++ testing frameworks, or the philosophy of TDD? Bridging the two should be easy once you understand them both.
 

survivor

Banned
AcciDante said:
I guess I'll just get it from the library then :lol
Does your professor actually use the book? In my Java class the books were there as a reference if we ever needed them and we were even allowed to use any book we wanted since they weren't used by the professor at all.
 

AcciDante

Member
A new semester bumparoo.

Our first project is massive in comparison to anything I did last semester, and I feel like the teacher I had last semester was awful in comparison to the course my current professor taught. I think he covered a lot more then what we did in our class, and I could also use some refreshing, so I'm feeling a little behind.

I still have ten days to work on my project, so I'm not going to ask too much yet. EDIT: fixed it!!! I'm currently trying to figure out how to make a pointer to a vector and then manipulate the vector through that pointer. So far I've tried:

Code:
	vector<int> *test;
	test = new vector<int>;

	*test.push_back(1); //error: test must have class type

//the right way is
(*test).push_back(1);

Now, from looking at the debugger, it looks like the vector is being created. I just can't seem to access/manipulate the elements! Can someone point me in the right direction? (pointer puns are too easy)
 

d[-_-]b

Banned
Code:
	vector<int> *test;
	test = new vector<int>;

	*test.push_back(1); //error: test must have class type
Pretty sure you don't need the * infront of test to access it.
 

~Kinggi~

Banned
d[-_-]b said:
Code:
	vector<int> *test;
	test = new vector<int>;

	*test.push_back(1); //error: test must have class type
Pretty sure you don't need the * infront of test to access it.
You have a sweet username.
 

Slavik81

Member
d[-_-]b said:
Code:
	vector<int> *test;
	test = new vector<int>;

	*test.push_back(1); //error: test must have class type
Pretty sure you don't need the * infront of test to access it.
The dereferencing operator (*) has a really low priority in order of operations.

He thinks he's doing
Code:
(*test).push_back(1);
But he's actually doing
Code:
*(test.pushback(1));

Instead, use the -> operator to automatically dereference and call a function at the same time.

Code:
test->push_back(1);
 

Slavik81

Member
Visual Studio 2008 /w VisualAssistX. I loved Visual Studio for C#, but found it was garbage for C++. However, VisualAssistX fixes it, making it a great tool. Such an amazing plug-in.

On my laptop, I generally just use Notepad++ or gvim with some sort of build system. I haven't settled on one. Any recommendations? My only real experience has been with boost-build Jamfiles, and I hate them with such a passion.
 
Status
Not open for further replies.
Top Bottom