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.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.
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;
}
//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();
}
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();
}
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?
Sweet, I think it worked! Thanks for your help guys.josephdebono said:Whoops of course. Sorry. Declare the file first then call open.
std::fstream inputFile;
inputFile.open("numbData.txt");
#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();
}
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.
#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
Try setting head = 0AcciDante 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?
#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
Chairman85 said:Your private function displayPostOrder is spelled displayPostOder
printf("Please type in a big integer: x1 = ");
gets(s1);
printf("Please type in another big integer: x2 = ");
gets(s2);
[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
Do not use gets. Ever. Try fgets and report back.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..
Go ahead and ask here since this is the biggest programming thread. Soon enough we should probably make a Programming OT.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?
poweld said:Do not use gets. Ever. Try fgets and report back.
IIRC, you can have problems if you mix scanf and gets. Do you call scanf some time before that first gets?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.
Slavik81 said:IIRC, you can have problems if you mix scanf and gets. Do you call scanf some time before that first gets?
Either your include statement is wrong or you didn't "installed" mapkit the right way (I know nothing about mapkit).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 :|
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.
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.
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.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.
Just search for it on your hard drive, it should be fairly easy to fix.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.
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/
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
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. :lolAcciDante 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?
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?
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.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.
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 said:I guess I'll just get it from the library then :lol
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);
You have a sweet username.d[-_-]b said:Pretty sure you don't need the * infront of test to access it.Code:vector<int> *test; test = new vector<int>; *test.push_back(1); //error: test must have class type
The dereferencing operator (*) has a really low priority in order of operations.d[-_-]b said:Pretty sure you don't need the * infront of test to access it.Code:vector<int> *test; test = new vector<int>; *test.push_back(1); //error: test must have class type
(*test).push_back(1);
*(test.pushback(1));
test->push_back(1);
Thanks guy, on other note guess I need some practice too with C++~Kinggi~ said:You have a sweet username.
Vim!d[-_-]b said:Thanks guy, on other note guess I need some practice too with C++. So I guess I should subscribe to this thread.
Which IDE do you guys use?