Reference to another article or thread Fixed Errrors

I found that this method I used I found that this is how I did this If this is any help to you great. I fixed some errors that it had in c++

Original Thread found here to reference it. http://www.cplusplus.com/forum/articles/13355/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// this is the main entry point for our program...
int main(int argc, char* argv[]) {

	std::cout<< "make sure to type -ex"<<endl;
	 if (argc < 5) { // Check the value of argc. If not enough parameters have been passed, inform user and exit.
        std::cout << "Usage is -in <infile> -out <outdir>\n"; // Inform the user of how to use the program
        std::cin.get();
        exit(0);
    } else { // if we got enough parameters...
        char* myFile;
	char* myPath; // here is where I changed.
	char* myOutPath;
	char* extractt;
	char* reimportt;
        std::cout << argv[0];
        for (int i = 1; i < argc; i++) { /* We will iterate over argv[] to get the parameters stored inside.
                                          * Note that we're starting on 1 because we don't need to know the 
                                          * path of the program, which is stored in argv[0] */
            if (i + 1 != argc) // Check that we haven't finished parsing already
                if (argv[i] == "-f") {
                    // We know the next argument *should* be the filename:
                    myFile = argv[i + 1];
                } else if (argv[i] == "-p") {
                    myPath = argv[i + 1];
                } else if (argv[i] == "-o") {
                    extractt = argv[i + 1];
				}else if (argv[i] == "-ex") {
                    myOutPath = argv[i + 1]; // I added this new
				}else if (argv[i] == "-re") {
                    reimportt = argv[i + 1]; // I added this new
					
					
                } else {
                    std::cout << "Not enough or invalid arguments, please try again.\n";
                    Sleep(2000); 
                    exit(0);
            }
            std::cout << argv[i] << " ";
        }
        //... some more code
        std::cin.get();
        return 0;
    }



So what have I changed?
I changed the char* myFile, myPath, myOutPath;

To char* myFile;
char* myPath;
char* myOutPath;

This way you can have no errors which are a few from this method or approach.

I also added 2 new commmands -ex -re for reimport and extract...
Last edited on
Hi, welcome to the forum. Please use code formatting... edit your post and put [code] and [/code] around your code.

Yes, whoever wrote that wrote it wrong. Although the program is still wrong. And honestly, it looks messy and modern C++ doesn't need to use raw pointers that often.

Biggest problem is you can't compare char pointers/arrays using ==, you have to use something like strcmp.
http://www.cplusplus.com/reference/cstring/strcmp/
Or better yet, use C++ std::string.
1
2
std::string a = argv[1];
if (a == "apple") { ... }
Last edited on
A modern way would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> get_args(int argc, char *argv[])
{
  vector<string> result;

  for (int i = 1; i < argc; ++i)
  {
    if (argv[i] != nullptr)
      result.push_back(argv[i]);
  }
}
int main(int argc, char *argv[])
{
  string myFile, myPath, myOutPath, extractt, reimportt;
  vector<string> args = get_args(argc, argv);
  for (size_t i = 0; i < args.size(); ++i)
  {
    // put you logic here
    // be careful last arg is args[args.size()-1]
  }
}
Topic archived. No new replies allowed.