Parsing CL arguments and Function call queue
I am trying to create program which will process command line arguments and define which functions should be run, with specific order and specific arguments.
This is my first problem:
Code:
// proccessing_args.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> cities;
cities.push_back("Seattle");
cities.push_back("Los Angeles");
// Use C++ "for each" construct syntax
// to iterate over "cities" vector
for(string& city : cities)
{
cout << city << endl;
}
_getch();
return 0;
}
proccessing_args\proccessing_args.cpp(20): error C2143: syntax error : missing ',' before ':'
refers to the line with for. I copied the code form here http://technologyriver.blogspot.com/...loop-in-c.html
Re: Parsing CL arguments and Function call queue
Possibly I try to change to this:
Code:
for each(string& city in cities)
{
cout << city << endl;
}
But here I got error
proccessing_args\proccessing_args.cpp(20): error C2440: 'initializing' : cannot convert from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::string &'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
Conversion loses qualifiers
How to fix it?
Re: Parsing CL arguments and Function call queue
My first question is: what does your "string convert ..." problem have to do with the "Parsing CL arguments and Function call queue"?
The code you posted does nothing with argv/argc!
Second: your code/problem has nothing to do with the Visual C++ Programming, so I will move this thread to a more appropriate forum...
Re: Parsing CL arguments and Function call queue
Why? I create program in C++ Visual studio.
Re: Parsing CL arguments and Function call queue
Quote:
Originally Posted by
crazy boy
Why? I create program in C++ Visual studio.
Because
Quote:
Forum: Visual C++ Programming
Ask questions about Windows programming with Visual C++ and help others by answering their questions.
What is in your code specific for Windows programming?
Re: Parsing CL arguments and Function call queue
I am trying to find out how to parse command line arguments using foreach equivalent in C++
Re: Parsing CL arguments and Function call queue
Quote:
Originally Posted by
crazy boy
I am trying to find out how to parse command line arguments using foreach equivalent in C++
No.
You are trying to test "foreach equivalent in C++" using vector of strings containing some random texts that do NOT come from command line arguments.
Re: Parsing CL arguments and Function call queue
Quote:
proccessing_args\proccessing_args.cpp(20): error C2143: syntax error : missing ',' before ':'
What version of Visual Studio are you using? Range-based for loops are only implemented from VS2012. See http://msdn.microsoft.com/en-us/libr...vs.120%29.aspx for a list of which parts of the c++11 standard are in which versions of Visual Studio.
Re: Parsing CL arguments and Function call queue
I have VS C++ 2010 and cannot upgrade. May I ask you about this?
Why this works:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
a = argv[i];
}
}
But this does not work:
Code:
class MyClass{
MyClass(int argc, _TCHAR* argv);
};
MyClass::MyClass(int argc, _TCHAR* argv){
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
cout << "hello" ;
a = argv[i]; // HERE IS PROBLEM
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyClass *Obj(int argc, _TCHAR* argv);
}
Visual Studio C++ error is:
'_TCHAR' to '_TCHAR *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
I cannot see the difference between the two algorithms...
I also tried to declare the constructor as:
MyClass::MyClass(int argc, _TCHAR* argv[]){
Re: Parsing CL arguments and Function call queue
Quote:
Originally Posted by
crazy boy
I have VS 2004 and cannot upgrade
And what is the VS 2004? :confused:
Quote:
Originally Posted by
crazy boy
Why this works:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
a = argv[i];
}
}
But this does not work:
Code:
class MyClass{
MyClass(int argc, _TCHAR* argv);
};
MyClass::MyClass(int argc, _TCHAR* argv){
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
cout << "hello" ;
a = argv[i]; // HERE IS PROBLEM
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyClass *Obj(int argc, _TCHAR* argv);
}
Because the type of argv is _TCHAR**, not a _TCHAR*
Re: Parsing CL arguments and Function call queue
Thank you. I would like to parse the arguments but I have problem that they are different type. I have found example code using strings but the arguments are _TCHAR so if I would replace input for a it would produce error:
Code:
MyClass::MyClass(int argc, _TCHAR **argv){
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
cout << "hello" ;
a = argv[i];
std::string input = "abc,def,ghi";
std::istringstream ss(input);
std::string token;
while(std::getline(ss, token, ','))
{
std::cout << token << '\n';
}
}
};
Re: Parsing CL arguments and Function call queue
Your main problem is a messed description of your problem! :eek:
This code does work:
Code:
class MyClass
{
public:
MyClass(int argc, _TCHAR* argv[]);
};
MyClass::MyClass(int argc, _TCHAR* argv[])
{
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
a = argv[i]; // there IS NO PROBLEM at all!
cout << a << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyClass Obj(argc, argv);
return 0;
}
Re: Parsing CL arguments and Function call queue
What? I said I want to parse the argument:
"abc,def,ghi"
I explain father more:
I run myapp.exe abc,def,ghi 124,25,13
so I need to parse the argument 1 to array of values ["abc","def","ghi"]
and argument 2 to array of values ["124","25","13"]
as example.
In this case I want to use "," as delimiter. In one circle of the loop to get array from the argument
The point is that I want to parse every one argument to proccess the data from the argument itself. Understand?
Re: Parsing CL arguments and Function call queue
Comma is not a standard delimiter.
In your case with "abc,def,ghi 124,25,13" you pass only two command line parameters, so you will have to implement your own method(s) to parse them.
Or you could use the standard delimiters like white spaces and special options like '/' or '-'.
See also about Parsing C Command-Line Arguments
Re: Parsing CL arguments and Function call queue
On the page where I found it on, they use the
Code:
std::getline(ss, token, ','))
but working with string type. The dash is only example. It could also be "-" used as delimiter. So can I use sstream to parse it? I want to parse the argument as one , not as separated arguments.
I tried:
Code:
MyClass::MyClass(int argc, _TCHAR **argv){
_TCHAR *a;
for(int i = 0; i < argc; i++)
{
cout << "hello" ;
a = argv[i];
//std::string input = "abc,def,ghi";
std::istringstream ss((_TCHAR) a);
std::string token;
while(std::getline(ss, token, ','))
{
std::cout << token << '\n';
}
}
};
But this also does not. When I debug it seems like it parses the argument but I don't see the parsed values and I don't see them displayed