basic If control

ok so have just started to learn C++ and im wanting to check all arguments to call out errors when it finds a character. Ive been able to get it to work but having to manually tell it which argv to check, is there a loop or check all argv??

thanks in advance.
Brad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <iostream>
using namespace std;


int main(int argc, char *argv[])
{

	if (isalpha(*argv[1]))

		cerr << "error" << endl;

		
	if (isalpha(*argv[2]))

		cerr << "error" << endl;

		
		return 0;
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cctype>


int main(int argc, char *argv[])
{
    for (int i=1; i<argc; ++i)
    {
       if (std::isalpha(*argv[i]))
       {
           std::cerr << "ERROR: argument is \"" << argv[i] << "\"\n";
           return 0 ;
       }
    }
}
Last edited on
1
2
3
4
for(int i = 1; i < argc; ++i) {
    if (isalpha(*argv[i]))
        std::cerr << "error" << std::endl;
}
@ cire: Did you notice that you have put using namespace std;? Yet you are doing std::cerr.
Last edited on
Did you notice that you have put using namespace std;? Yet you are doing std::cerr.

Sure. Let me fix that for you.
I know it is a bad practice. I meant to either remove the using statement or the std:: from cerr.
Last edited on
I know it is a bad practice. I meant to either remove the using statement or the std:: from cerr.

Why? It is permissible to bring something into the global namespace and still use it's fully qualified name, and in this case it was simply an artifact of pasting the original code and then modifying it. Hardly worthwhile to point out.
thanks guys
@ cire: Because it kinda looked weird lol.
Topic archived. No new replies allowed.