Switches

Nov 7, 2015 at 11:17pm
I don't know what happened to my last post, but no matter. I figured out how to add command line switches.

So below is the program I have been working on today Just practicing.

My question is how do I run the write function by default with no command line parameters, but NOT run if I do supply parameters.

I tried simply removing

if ((argc >= 2 ) && ( strcmp(argv[1], "-w") == 0))

But if I do that, the write function runs even if I supply command line parameters. And if I simply remove the '-w' to leave a blank "", then it wont run at all.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
int main(int argc, char* argv[])
{

if ((argc >= 2 ) && ( strcmp(argv[1], "-w") == 0))
	{

	char choice;
do {

	std::string test;
	std::cout << "Enter a string: ";
	getline (std::cin, test);
	
	std::ofstream outfile;
	outfile.open("test.txt", std::ios_base::app);
	if (outfile.is_open()) {
	outfile << test << '\n'; 
}
	else std::cout << "Unable to open file";


do {
	std::cout << "Would you like to enter another string? ";
	std::cin >> choice;
	std::cin.clear();
	std::cin.ignore(1000, '\n');

	if (choice != 'y' && choice !='n')
	std::cout << "You did not make a valid selection" << '\n';
}
	while (choice != 'y' && choice !='n');
	
}
	while (choice == 'y');
	
	if (choice !='y' && choice !='n')
	return 0;
}
	if ((argc >= 2 ) && ( strcmp(argv[1], "-v") == 0))
	{
	std::string line;
	std::ifstream myfile ("test.txt");
  
	if (myfile.is_open())
	{
	while ( getline (myfile,line) )
	{
	std::cout << line << '\n';
	}
	myfile.close();
	}

	else std::cout << "Unable to open file";}

	

	else if ((argc >= 2 ) && ( strcmp(argv[1], "-e") == 0))


	{

char erase;

	std::cout << "This will erase contents of file. Proceed? ";
	std::cin >> erase;

	if (erase !='y')
	return 0;

        std::ofstream myfile;
	myfile.open ("test.txt");
	myfile << "";
	myfile.close();
	

	std::cout << "Contents of file were erased" << '\n';
}


}
Last edited on Nov 7, 2015 at 11:19pm
Nov 7, 2015 at 11:58pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(int argc, char* argv[]) {
	
	if ( argc == 2) {
		char *choice = argv[1];
		
		switch ( choice[1] ) {
			case 'w':
				break;
			case 'v':
				break;
			case 'e':
				break;
			default:
				break;
		}
	}
	else {
		// Erase or write function goes here.
	}
	
	return 0;
}
Nov 8, 2015 at 12:52am
default , in this example, is only being executed if one supplies improper command line switch, not by default (by simply running the program with no parameters), Which was my whole problem.
Nov 8, 2015 at 12:55am
Incorrect, if you don't supply any parameters argc won't equal 2. Default is only there is you enter something other than w-v-e.
Nov 8, 2015 at 1:01am
1
2
3
4
5
if (argc == 0) {
  // run function
}

// do something else 


ShiftLeft's implementation is a good idea for processing arguments, do loops are a bit messy IMO.

Good Luck !!
Nov 8, 2015 at 1:02am
Thank you both for your feedback. I will work on it!

*edit* TheIdeasMan, it's still not working.

Since you both think switch would be better, I am certainly willing to rework my program, but I still have the problem of not being able to run the program without parameters.
Last edited on Nov 8, 2015 at 1:07am
Nov 8, 2015 at 1:13am
Don't worry...I don't expect you guys to figure it all out for me. I will continue to reasearch and see what I can come up with :-)
Nov 8, 2015 at 1:21am
Keep in mind, my example has one drawback that if you enter more than 1 argument in command line, it will also execute write.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(int argc, char* argv[]) {
	
	if ( argc == 2) {
		char *choice = argv[1];
		
		switch ( choice[1] ) {
			case 'w':
				break;
			case 'v':
				break;
			case 'e':
				break;
			default:
				break;
		}
	}
	else if ( argc == 1 ) {
		// Erase or write function goes here.
	    }
	else
		cout << "Too many arguments" << endl;
		
	return 0;
}


or

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
#include	<iostream>

using namespace std;

int main(int argc, char* argv[]) {
	char *choice = argv[1];
	
	switch ( argc ) {
		case 2:
			cout << "Processing " << choice << endl;
			
			switch ( choice[1] ) {
				case 'w':
					break;
				case 'v':
					break;
				case 'e':
					break;
				default:
					break;
			}
			break;
		
		case 1:
			// Implement erase/write code here
			break;
			
		default:
			cout << "Too many arguments" << endl;
	}
	
	return 0;
}


Might be a better alternative.
Nov 8, 2015 at 1:25am
Thank you ShiftLeft. I love learning, so the new ideas are great! I appreciate it :-)
Nov 8, 2015 at 1:31am
My question is how do I run the write function by default with no command line parameters, but NOT run if I do supply parameters.


But I still have the problem of not being able to run the program without parameters.


You'll have to be more specific what you mean as these statements are ambiguous. If you mean you want to execute write in any case then you just need to put a call to that function in line 22 of last example.


Nov 8, 2015 at 1:34am
Sorry ShiftLeft. I am a brand-newbie. I am taking a course in C++ and haven't even gotten to arrays yet!
I just wanted to venture off and learn some things for practicing reading from and writing to text files.

Anyways, just found a code snippet that works for what I wanted

if ( argc != 2 ) // argc should be 2 for correct execution
std::// We print argv[0] assuming it is the program name
cout<<"usage: "<< argv[0] <<" <filename>\n";
Nov 8, 2015 at 1:49am
if (argc !=2){
std::cout << "No arguments" << '\n';
}

Finally got it working the way I want! Thank you guys for your help. :-)
Last edited on Nov 8, 2015 at 2:14am
Nov 8, 2015 at 2:51am
if (argc !=2){
std::cout << "Usage:" << '\n';
std::cout << "-w to write to file" << '\n';
std::cout << "-v to verify contents of file" << '\n';
std::cout << "-e to erase contents of file" << '\n';
}

This is what I was trying to do. I wanted to display usage if no command line arguments were used and didn't want it to display if command line arguments *were* used.

I guess I didn't ask my question very well.

God, it seems so obvious now. LOL!
Last edited on Nov 8, 2015 at 2:59am
Topic archived. No new replies allowed.