Thread: Newbie needs help with vectors!

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    Newbie needs help with vectors!

    this is a function i am trying to write so that i can set up a vector for a flag operation to make sure i don't process the same letter of a string twice. i do understand variables(well i thought i did) from what i know if a variable is declared at the begginning of the function, then it should be available throughout the entire function right? could someone please look at my code and tell me where i am going wrong please? I could use another global vector but as i read everywhere, its best to keep globals to a minimum!

    Thanks for your time!

    Werdy666

    Code:
    #include <vector>
    
    using namespace std;
    
    extern vector <string> words;
    
    int init()
    {
    string temp;
    
    vector <int> flag; // *****i want this local to this function
    
    // if i put flag.pushback(0) here this works fine!
    
    ifstream load;
    load.open(NAMES);
    if (load.fail())
    	{
    	cout << endl << "Error! Cannot load word file! Please reinstall!" << endl;
    	exit(0);
    	}
    while (!load.eof())
    	{
    	std::getline(load,temp);
    	words.push_back(temp);
    	cout << endl << temp;
    	}
    
    int test;
    string jumbled;
    	
    cout << endl << int(words.size()) << endl;
    cout << int(words[5].length()) << endl;
    
    cout << "lets try to jumble a word at random" << endl;
    cout << "You see the list.... Pick one! ";
    
    cin >> test ;
    int lengthofstring = int(words[test].length());
    
    for (int fil = 0; fil < lengthofstring ; fil++)
    	{
                    flag.pushback(0) ; //*** this is the line where it doesn't recognise 'flag' as a vector
    	}
    	
    	for (int loop = 0 ; loop < lengthofstring ; loop++)
    		{
    		
    		}
    	
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    33
    ok i appear to have solved my problem but i still don't know why it wasn't working.

    Code:
    cout << int(words[5].length()) << endl;
    
    cout << "lets try to jumble a word at random" << endl;
    cout << "You see the list.... Pick one! ";
    
    cin >> test ;
    int lengthofstring = int(words[test].length());
    i'm a beginner and i'm still learning but as far as i know the code above is casting the length of the string into an int right? well when i don't cast it the flag vector is recognised as a vector. Why is this??? what did i do wrong??? Could someone please give me some insight????

    Thanks for your time

    Werdy666

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM