Well.. here goes. I originally wrote this using visual c++ but now the requirement has changed that it must run on a *nix system. Whenever i attempt to compile using g++ main.cpp (which is what the file is named) i recieve the following errors.
I have already corrected a few by moving some declarations outside of main(), but i am at a loss on how to deal with the others.... the errors sound greek to me. ANY help would be grand.
Errors:
1 2 3 4 5 6 7 8 9 10 11
main.cpp: In function ‘int main()’:
main.cpp:53: error: missing template arguments before ‘fin’
main.cpp:53: error: expected ‘;’ before ‘fin’
main.cpp:55: error: ‘fin’ was not declared in this scope
main.cpp:62: error: invalid use of qualified-name ‘std::pos’
main.cpp:63: error: ‘pos’ is not a member of ‘std’
main.cpp:66: error: ‘pos’ was not declared in this scope
main.cpp:81: error: ‘struct std::string’ has no member named ‘front’
main.cpp:92: error: expected initializer before ‘:’ token
main.cpp:97: error: expected ‘)’ before ‘;’ token
main.cpp:143: error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘constchar*’ for argument ‘1’ to ‘int atoi(constchar*)’
/*----------------------------------------------------------------------------------------------*/
//Name:
//Date:
//Email:
//Email:
//
//
//
//
//
/*----------------------------------------------------------------------------------------------*/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <cctype>
#include<cstdlib>
int row;
int listCount = 0;
std::string line ;
std::string file_path;
constchar nullchar = 0 ;
const std::string space( 1, ' ' ) ;
const std::string three_spaces = space + space + space ;
std::vector< std::vector<std::string> > tokens_in_all_lines ;
// An array to hold the non numeric strings from the file - arbitrary size
std::string listArray[256];
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
int main()
{
std::cout << "Name: " << std::endl;
std::cout << "Date: " << std::endl;
std::cout << "Email: " << std::endl;
std::cout << "Email: " << std::endl<< std::endl;
std::cout << "Please enter the full path to the file to be decrypted."<< std::endl;
std::cout << "Path = ";
std::cin >> file_path;
std::cout << std::endl<<std::endl<<std::endl;
std::basic_ifstream fin(file_path) ;
while( std::getline( fin, line) ) // while not EOF
{
// split into tokens
std::vector<std::string> tokens_in_this_line ;
// check for three consecutive spaces in this line indicating a space string
// stringstream does not pick up consecutive spaces
auto pos = line.find(three_spaces) ;
if( pos != std::string::npos ) // if 3 spaces found
{
// before pos of three consecutive spaces
std::string part_one = line.substr( 0, pos ) ;
// after pos of three consecutive spaces
std::string part_two = line.substr( pos + three_spaces.size() ) ;
// replace the middle space of the three consecutive spaces
// replaced with a nullchar
//concatenate back to form a line
line = part_one + space + nullchar + space + part_two ;
}
// parse this line
std::istringstream stm(line) ;
std::string temp ;
while( stm >> temp )
{
// if nullchar, replace it with a space
if( temp.front() == nullchar ) tokens_in_this_line.push_back(space) ;
else tokens_in_this_line.push_back(temp) ;
}
// add extracted tokens to tokens_in_all_lines
tokens_in_all_lines.push_back(tokens_in_this_line) ;
}//get next line
// determine the max number of columns
std::size_t max_width = 0 ;
for( constauto& tokens : tokens_in_all_lines )
if( max_width < tokens.size() ) max_width = tokens.size() ;
row = tokens_in_all_lines.size();
//Display Encrypted File
std::cout << "---------------Begin Encrypted Message-------------"<<std::endl;
for(int r=0;r<row;++r)
{
for(int i=0;i<max_width;++i)
{
tokens_in_all_lines[r].resize(max_width);
std::cout << tokens_in_all_lines[r][i];
}
std::cout << std::endl;
}
std::cout << "---------------End Encrypted Message---------------"<<std::endl;
std::cout << std::endl<< std::endl<< std::endl;
// Begin to Decrypt Message one string/token at a time
std::cout << "---------------Begin Decrypted Message-------------"<<std::endl;
for(int r=0;r<row;++r)
{
for(int i=0;i<max_width;++i)
{
tokens_in_all_lines[r].resize(max_width);
//Make sure vector isnt empty, if it is.. skip that iteration. Empty vector errors out if attempted to be accessed
if(tokens_in_all_lines[r][i]!="")
{
//Check If value is not a number
if(!is_number(tokens_in_all_lines[r][i]))
{
if(listCount==0)
{
listArray[listCount]=tokens_in_all_lines[r][i];
}
else
{
for(int l=listCount+1;l>0;--l)
{
listArray[l]=listArray[l-1];
}
listArray[0]= tokens_in_all_lines[r][i];
}
listCount+=1;
std::cout << listArray[0];
}
//If it was not a number then it must be a number
else
{
int value = atoi(tokens_in_all_lines[r][i]);
std::cout << listArray[value];
std::string swap = listArray[value];
for(int n=value;n>0;--n)
{
listArray[n]=listArray[n-1];
}
listArray[0]=swap;
}
}
}
std::cout << std::endl;
}
std::cout << "---------------End Decrypted Message---------------"<<std::endl;
//std::cout << std::endl<< std::endl<< std::endl<< std::endl;
}
- Line 53: std::basic_ifstream fin(file_path) ; Why don't you use std::ifstream fin(file_path); ?
- Line 62: auto pos = line.find(three_spaces) ; You may want to try to declare pos as: size_t pos = line.find(three_spaces);
- Line 81: temp.front() front() is a c++11 function, which may not be supported by your compiler. Try: temp[0]
- Line 92: for( constauto& tokens : tokens_in_all_lines ) range based for-loops are also a c++11 feature, which may or may not be supported by your compiler. You can try a for loop using iterators.
- Line 143: tokens_in_all_lines is a std::vector<std::string>, atoi() expects a character array (c-string) as an argument. You can try atoi(tokens_in_all_lines[r][i].c_str());
Ah yes, i confused myself by your statement of using visual c++. Apologies, like JLBorges said, using the -std=c++11 (or -std=c++0x) flag with g++ allows you to use c++11 features.