A question about Vectors

This is a discussion on A question about Vectors within the C++ Programming forums, part of the General Programming Boards category; Hello i have a string vector which contains something like this 1 2 3 a b 4 -1 seperatly. String ...

  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
    Posts
    23,033
    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
    22,312
    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 06:30 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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, 07: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, 10: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

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