Thread: A question about Vectors

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    A question about Vectors

    Hello i have a string vector which contains something like this 1 2 3 a b 4 -1 seperatly.

    String vector displays 1 as 1, 2 as 2, a as a(for sure) but it does not display -1 properly. When i cout -1 value, it displays something like û1 this.

    I have to write a if statement when -1 equals something. what should i write

    if(a == here), to work.

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not show the code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    I did not redesign my functions yet. I just want to solve my problem first. What i want to do is in the vector array when it sees -1 understand that a new line is beginning.

    My input file dfa.txt is:

    0 0 1 a -1
    1 0 3 a 2 b -1
    -1


    Code:
    #include <iostream>
    #include "graph.h"
    #include <fstream>              // for ifstream
    #include <string>
    using namespace std;
    #include "tvector.h"
    #include "stdlib.h" 
    
    template <class T>
    void PutInputToArray(ifstream &dfa,tvector<T> &v)
    {
    	T word;
    
    	  while(dfa >> word)
    	{
    		  if(word == "-1")
    		  {
    			  cout<<"hey"<<endl;
    		  }
    		  v.push_back(word);	  
    		
    	}
    	  
    
    
    }
    
    
    void GetIntVal(string &strConvert, int &i) {  
    
     
      i = atoi(strConvert.c_str()); 
     
    }
    
    
    int main() {
      Graph myGraph;
    
      tvector<string> v;
    
      ifstream dfa;
      string file="dfa.txt";
      
      int NumberofStatesInt;
      int j=0;
      
      dfa.open(file.c_str());
      PutInputToArray(dfa,v);
      string NumberofStates = v[0];
      
      GetIntVal(NumberofStates,NumberofStatesInt);
    
      for(j; j< NumberofStatesInt; j++)
      {
    	  myGraph.AddVertex(j);
      }
    
      int k = 2;
      string first = v[2];
      string second = v[4];
      string second2 = v[6];
      int firstint, secondint;
      
      GetIntVal(first,firstint);
      GetIntVal(second,secondint);
      
      myGraph.AddEdge(firstint,secondint);
    
    
    
      return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few comments:
    • Do not use using directives and declarations in headers (except within a local scope) and before including a header.
    • "stdlib.h" should be <cstdlib> (or at least <stdlib.h>. However, at a glance, it looks like you do not need to include that header.
    • Declare variables near first use. For example, you should declare j in the for loop itself. k does not ever appear to be used. (More on this in the last point.)
    • Indent consistently with either spaces or tabs, but not both (unless you are very careful, but it typically is not worth the care).
    • Post the smallest and simplest code that demonstrates the problem. In this case you are using a custom vector interface and implementation, so perhaps the bug lies there.
    Last edited by laserlight; 06-02-2008 at 05:30 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is more than one '-' character. My guess is that your input uses one that is different from the simple dash in ASCII. Make sure that the input file has that simple dash.

    If you're on windows, use Notepad to edit it. Check the dash. Try typing it in with Notepad and saving the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 06:31 PM
  2. Real quick question on vectors, if I may
    By hpy_gilmore8 in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2003, 09:01 AM
  3. question about stacks (and vectors too for that matter)
    By Silvercord in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2003, 12:26 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. vector of vectors question
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 02-10-2003, 03:02 PM