CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    May 2014
    Posts
    205

    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
    Last edited by crazy boy; June 12th, 2014 at 02:33 AM.

  2. #2
    Join Date
    May 2014
    Posts
    205

    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?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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...
    Victor Nijegorodov

  4. #4
    Join Date
    May 2014
    Posts
    205

    Re: Parsing CL arguments and Function call queue

    Why? I create program in C++ Visual studio.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Parsing CL arguments and Function call queue

    Quote Originally Posted by crazy boy View Post
    Why? I create program in C++ Visual studio.
    Because
    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?
    Victor Nijegorodov

  6. #6
    Join Date
    May 2014
    Posts
    205

    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++

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Parsing CL arguments and Function call queue

    Quote Originally Posted by crazy boy View Post
    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.
    Victor Nijegorodov

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Parsing CL arguments and Function call queue

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    May 2014
    Posts
    205

    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[]){
    Last edited by crazy boy; June 13th, 2014 at 08:34 AM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Parsing CL arguments and Function call queue

    Quote Originally Posted by crazy boy View Post
    I have VS 2004 and cannot upgrade
    And what is the VS 2004?
    Quote Originally Posted by crazy boy View Post
    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*
    Victor Nijegorodov

  11. #11
    Join Date
    May 2014
    Posts
    205

    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';
    		}
    
    	}
    };
    Last edited by crazy boy; June 13th, 2014 at 10:17 AM.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Parsing CL arguments and Function call queue

    Your main problem is a messed description of your problem!
    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;
    }
    Victor Nijegorodov

  13. #13
    Join Date
    May 2014
    Posts
    205

    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?
    Last edited by crazy boy; June 13th, 2014 at 11:05 AM.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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
    Victor Nijegorodov

  15. #15
    Join Date
    May 2014
    Posts
    205

    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
    Last edited by crazy boy; June 13th, 2014 at 11:42 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured