Vector fstream outputting

This is a discussion on Vector fstream outputting within the C++ Programming forums, part of the General Programming Boards category; So i would set the limit of the vector to 100 members (since 10x10 is 100) by doing something like ...

  1. #16
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    So i would set the limit of the vector to 100 members (since 10x10 is 100) by doing something like this vector <char[100]> Map and this->loadmap() should work right?

  2. #17
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    I'm getting a bunch of errors that deal with the vector<char> Map. It seems that it won't recognize the vector Map in the .cpp file. Can someone please help me out?

  3. #18
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    Currently i've figured out the vector problems i hope. But now i am getting errors about my loadmap. When i use this->loadmap(); it gives me a error that i dont have any arguments, can someone tell me what this means?

    I have also attached the CMap codes with some corrections instead of posting the code. Again thank you for any help.
    Attached Files Attached Files

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I was leaning more along the lines of placing the push_back code into the for loop so you push back each character you find. In the code you attached all you do is read in a single character and then put that single character in your board.

    Make sure you spend some time trying to understand or figure out what's going on before you try to implement it. The vector starts off empty. Then you use push_back to add something to the end of your vector. Since your vector holds char values, you read in a character and add it to the end of your vector. That will load the vector with the characters from the file.

    BTW, your save function looks good.
    Last edited by Daved; 06-12-2005 at 10:55 AM.

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    First, I suggest you quit using the term "map". A map is a C++ entity, and your program is not using a C++ map. I would change all the "map" names in your program to "maze". With the names you have now, it's confusing to even talk about your program.

    You should try playing around with vectors outside the context of your program. Make a small test program, and practice adding things to a vector and then retrieving them and displaying them using cout<<. Vectors can be very simple--for your purposes you can treat a vector just like an array and you can access the elements with array notation, e.g.

    cout<<myVector[10];

    So i would set the limit of the vector to 100 members
    A vector has no limits. It is expandable, which is not true for an array. However, expanding a vector requires some processing time which slows your program down, so you can avoid having to constantly expand the size of a vector by specifying a vector size to begin with, e.g.:

    vector<int> myVector(100);

    However, if you specify a starting size for your vector, you have to be aware that using:

    myVector.push_back(someInt);

    will add someInt to the end of the vector, which happens to be after 100 default ints(=0) that were inserted in the vector when it was created. Try this program and examine the output:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    int main()
    {
            vector<int> myVector(3);
    
    	//add the int 27 to the vector:
    	myVector.push_back(27);
    
    	for(int i=0; i<myVector.size(); i++)
    	{
    		cout<<myVector[i]<<endl;
    	}
    	
            return 0;
    }
    You should also consider this: if your maze is always going to be a fixed 10x10 size, why are you using an expandable vector instead of an array?
    Last edited by 7stud; 06-12-2005 at 12:52 PM.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by azndragon247
    Currently i've figured out the vector problems i hope. But now i am getting errors about my loadmap. When i use this->loadmap(); it gives me a error that i dont have any arguments, can someone tell me what this means?
    It means you aren't sending the arguments to loadmap() that you said were required:
    Code:
    void loadmap( istream &inStream );
    ...
    ...
    this->loadmap()
    Last edited by 7stud; 06-12-2005 at 12:57 PM.

  7. #22
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    Yea i understand that i'm missing arguments, otherwise it would compile, but i don't know exactly what i'm supposed to put in the parentheses

  8. #23
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm starting to get the feeling you didn't write much of this program.

    Look at your function definition here:
    Code:
    void CMap::loadmap()
    {
    	ifstream loadfile;
    	loadfile.open("Level1b.txt");
    	for ( int i = 0; i < 100; i++ )
    	{
    		 loadfile >> Map[i]; 
    	}
    }
    Does it use any parameters?? Are you aware that a function declaration and a function definition have to have matching parameter types and return types? So, if your function definition doesn't use any parameters and the function declaration has to match the function definition, what should you do?
    Last edited by 7stud; 06-12-2005 at 03:50 PM.

  9. #24
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    I did write the program but as for the istream &inStream i got that from one of my teacher's examples, which i didn't understand too well to begin with.

  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    istream& inStream
    inStream is the parameter name. Call it myVariable instead. That gives you:

    istream& myVariable

    When you send a value to that function, it will be stored in myVariable, and you can use that variable inside your function. "istream" is the type of myVariable. If you have a function that looks like this:

    void myFunc(int number);

    number is the variable name and int is the type. Similarly, with this function:

    void loadmap(istream& myVariable)

    myVariable is the variable name, and istream is the type. Most likely the only variable you've seen that is of type istream is cin, e.g.:
    Code:
    cout<<"enter some data: ";
    cin>>input;
    In the word "istream", "i" stands for input, and "stream" is a confusing C++ term for "data". The variable "cin" represents "input" "data" entered from the keyboard.

    The final thing you need to know about is the "&" symbol. That is also part of the type of myVariable. It stands for "reference". So, the full type of the MyFunction parameter is: "reference to an istream". The reference designation is curious because it doesn't affect the type of the value you send to the function. For instance, if you have two functions:

    void myFunc1(int number);
    void myFunc2(int& number);

    you can send either of those functions an int. The difference is that when a function parameter is a reference, the value you send to the function is not copied. In the first function, when you send an int to the function, a copy of the int is made for the function, and the function uses that copy. In the second function, the integer you send to the function isn't copied. The difference is very important and it is one of the keys to understanding C++. To see the difference, try executing this program:
    Code:
    #include <iostream>
    using namespace std;
    
    void myFunc1(int number)
    {
    	number += 100;
    }
    
    void myFunc2(int& number)
    {
    	number += 100;
    }
    
    int main()
    {
    	int myNumber = 5;
    	myFunc1(myNumber);
    	cout<<myNumber<<endl;  //5
    
    
    	myFunc2(myNumber);
    	cout<<myNumber<<endl; //105
    
    	return 0;
    }
    The first function changes the copy of the int that was made for the function, so in main() the variable myNumber is unaffected. However, in the second function the function parameter 'number' becomes a synonym(i.e. an alternate name for) myNumber, and any changes made to number change myNumber. The '&' symbol makes all the difference in the world. It is also more efficient not to have to copy objects when you send them to a function.

    Now, back to your program. You want to read stuff from a file, so your loadmap() function can be written a couple of ways:

    1) Don't have any function parameters--like it's defined now.
    Yea i understand that i'm missing arguments, otherwise it would compile, but i don't know exactly what i'm supposed to put in the parentheses
    The error you are getting is because your function declaration does not match your function definition--it doesn't matter whether you send your function an argument or not. If the function declaration does not match the function definition, you will get a compiler error even if you send your function an argument. No matter how you decide to proceed, you have to make sure your function declaration matches the function definition.

    2) You can make your function more general so it isn't hard coded to only read from the file "Level1b.txt". The way you would do that is to declare and define your function with a parameter of type ifstream. To avoid inefficient copying of the argument, make the type: ifstream&. Then, in main() you can create an ifstream object for any file name, and send the ifstream object to your function as an argument which will cause the maze to be read from the file.
    Last edited by 7stud; 06-12-2005 at 10:06 PM.

  11. #26
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    All i can say is WOW, i didn't expect a whole explanation on that.
    But thanks a lot 7stud, my teacher doesnt really explain it all too well.

  12. #27
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    great explaination and demonstration of patience.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash Table outputting incorrect number
    By Paul Skinner in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:19 AM
  2. Problem with outputting to a file
    By LowlyIntern in forum C Programming
    Replies: 6
    Last Post: 08-14-2008, 02:55 PM
  3. Inputting and outputting from structs
    By ashcan1979 in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2006, 04:31 AM
  4. Multiple structs and outputting them
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-17-2005, 08:26 AM
  5. Outputting a string to the screen
    By Soopafly in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2002, 02:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21