Thread: Help with Program

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Help with Program

    This is my program:
    Code:
    #include <vector>
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
    	void get_number(const char* filename, string sentence, vector <string> data);
    	vector <string> data;
    	string sentence;
        char filename[81];
    	
    	cout << "Enter Filename: ";
    	cin.getline(filename,80);
    	ofstream file(filename, ios::binary | ios::out);
    	cout << "Enter String: ";
    	cin >> sentence;
    	file.write(reinterpret_cast<char*>(&sentence), sizeof(sentence));
    	
    	get_number(filename, sentence, data);
    
    	
    	return 0;
    	}
    
    void get_number (char filename, string sentence, vector <string> data){
    	ifstream in(reinterpret_cast<const char*>(&filename));
    	
    	while(in>>sentence)
    		data.push_back(sentence);
    	
    	cout << "Number of Spaces: " << (data.size()-1);
    }
    But when i try to build it, it says:
    A.cc.text+0x2f9): undefined reference to `get_number(char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)'
    collect2: ld returned 1 exit status
    Can anyone help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void get_number(const char* filename, string sentence, vector <string> data);
    > void get_number (char filename, string sentence, vector <string> data)
    Make them match.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Quote Originally Posted by Salem View Post
    > void get_number(const char* filename, string sentence, vector <string> data);
    > void get_number (char filename, string sentence, vector <string> data)
    Make them match.
    Thanks...man that was a stupid mistake.
    Oh well anyway just found out that the program isn't working properly
    Thanks again

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Are you just trying to count the number of spaces? Why don't you do something like this:

    Code:
    int num_spaces=0;
    std::string dud;
    
    while ( in << dud )
        num_spaces ++;

    >> cout << "Enter String: ";
    >> cin >> sentence;
    You might want to getline() that too to include the whitespace characters.

  5. #5
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    file.write(reinterpret_cast<char*>(&sentence), sizeof(sentence));
    How could you think you can just cast a string object to a char array??
    Code:
    file.write(sentence.c_str(), sentence.length());
    Don't call me stupid.

  6. #6
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by iWill View Post
    Code:
    void get_number (char filename, string sentence, vector <string> data){
    	ifstream in(reinterpret_cast<const char*>(&filename));
    	
    	while(in>>sentence)
    		data.push_back(sentence);
    	
    	cout << "Number of Spaces: " << (data.size()-1);
    }
    This code is just crap. What the hell is this cast for?? If you thought about it for one moment you should have realized that filename should already be const char*, and thus see easily where the problem is. Instead blindly using a cast operator to make it compile, looks real stupid to me. You should avoid casts at all costs!
    That ampersand makes the program pass a pointer to a pointer to a char array to the constructor of ifstream. You know it needs a pointer to a char array.
    What is the sentence parameter for? You don't need it, you only need a local string variable.
    Don't call me stupid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM