Looping through strings in a vector and removing spaces

void getWords()
{
    string line;
    for (int i=0; i<assemb.size(); i++)
    {
        for (int word=0; word<assemb.at(i).size(); word++)
        {
            if (assemb.at(i).at(word) != ' ')
            {
                  getline(cin,line);
            }       

        } 
        fixedWords.push_back(line);
    }
}

I am trying to loop through this vector of strings and remove characters that are not spaces and add them to another vector, in my head this function should work but it doesn't it appears to get trapped in some sort of loop.

The words that at the moment print out as:

          VAR 0       
START:    LDN NUM01   
          SUB NUM02   
          STO MYSUM   
          LDN MYSUM   
          STO MYSUM   
END:      STP         
NUM01:    VAR 1025    
NUM02:    VAR 621     
MYSUM:    VAR 0 

However I want them to be:

-Var0
-Start:
-LDN
-NUM01
-SUB 
-NUM02
-STO
-MYSUM
-LDN
-MYSUM
-STO
-MYSUM
-END:
-STP
-NUM01
-VAR
-1025
-NUM02
-VAR
-621
-MYSUM
-0

This is part of an assembler program which takes a piece of assembler code turns it into binary commands and then inputs this into another program I have written which is able to read and execute binary commands.

Answers


How about this :

#include <iostream>
#include<vector>
#include<algorithm>
#include<iterator>
//#include<fstream>

using namespace std;

int main() {

std::vector<std::string> vec;
// std::ifstream fin("input.txt");
std::copy(std::istream_iterator<std::string>(std::cin), //fin
             std::istream_iterator<std::string>(),
             std::back_inserter(vec));

for(auto v:vec)
   std::cout<<"-"<<v<<std::endl;
return 0;
}

Replace std::cin with std::ifstream for the file

See here


It appears that what you want is pretty close to this:

std::copy(std::istream_iterator<std::string>(std::cin), 
          std::istream_iterator<std::string>(),
          std::ostream_iterator<std::string>(std::cout, "\n"));

The only difference is that this doesn't add the - at the beginning of each line. To do that, we can use transform instead of copy:

#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>

int main() {
    std::transform(std::istream_iterator<std::string>(std::cin),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(std::cout, "\n"),
        [](std::string const &in) { return "-" + in; });
    return 0;
}

Need Your Help

Strange error with zend frame work

zend-framework pdo zend-db zend-db-table

Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[28000] [1045] Access denied for user 'liveaide_dbuser1'@'lynx-u.znetindia.net' (using password: YES)' in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.