Using command line arguments

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Using command line arguments

Postby Jason » Sun Oct 12, 2003 7:09 pm

I am suppose to create a small program that uses command line arguments. Its suppose to be executable as pgm1 -c, if 'c' is 'o' then its suppose to output the sum of odd integers 1-100, if 'c' is 'e' then its suppose to output the sum of even integers 1-100. If c is any other char, it should print an error message. Here is my code but when i try to use a command line argument when i run it nothing happens and i just get another prompt. Could somebody pls point me in the write direction. Thanks a lot.

#include<iostream>
using namespace std;

int sumOdd();
int sumEven();

int main(int argc, char *argv[])
{
switch(argc)
{
case1: break;
case2: if(strcmp(argv[1], "-o") == 0) cout<<"The sum of odd numbers from 1-100 is "<<sumOdd()<<endl;
else if(strcmp(argv[1], "-e") == 0) cout<<"The sum of even numbers from 1-100 is "<<sumEven()<<endl;
else cout<<"Invalid Argument"<<endl;

default:;
}

return 0;
}

int sumOdd()
{
int total = 0;

for(int i = 1; i < 100; i + 2)
total = total + i;

return total;
}

int sumEven()
{
int total = 0;

for(int i = 0; i < 100; i + 2)
total = total + i;

return total;
}
Jason
 

Postby jgbauman » Mon Oct 13, 2003 4:18 am

  1. This loops wont work:
    for(int i = 0; i < 100; i + 2)

    You need i += 2 to modify i.
  2. You stumpled across some of the more obscure aspects of the C/C++ syntax:
    name: defines a label which can be used as target for goto. Yes there is a goto in C++, really ;-)
    The switch case uses this syntax for the default: label, too.
    The cases use it in a modified form case <integer>:.
    But if you forget the whitespace like you did, they become ordinary goto labels without sepcial meaning to the switch statement.
    So
    Code: Select all
      switch(argc) {
      case1:
        break;
      case2:
        if(strcmp(argv[1], "-o") == 0)
          cout << "The sum of odd numbers from 1-100 is " << sumOdd() << endl;
        else if(strcmp(argv[1], "-e") == 0)
          cout << "The sum of even numbers from 1-100 is " << sumEven() << endl;
        else
          cout << "Invalid Argument" << endl;
      default:;
      }

    is equivalent to (since case1 and case2 are unused goto labels)
    Code: Select all
      switch(argc) {
        break;
        if(strcmp(argv[1], "-o") == 0)
          cout << "The sum of odd numbers from 1-100 is " << sumOdd() << endl;
        else if(strcmp(argv[1], "-e") == 0)
          cout << "The sum of even numbers from 1-100 is " << sumEven() << endl;
        else
          cout << "Invalid Argument" << endl;
      default:;
      }

    is equivalent to (since it breaks out of the switch for every input)
    Code: Select all
      switch(argc) {
        break;
      }

    is equivalent to
    Code: Select all
      /* do nothing but irritate programmers */


User avatar
jgbauman
 
Posts: 358
Joined: Sat Sep 27, 2003 9:00 am

Postby Tracer » Mon Oct 13, 2003 4:54 am

/* do nothing but irritate programmers */


:lol: , good times...
(C++ TotalNewB)

if(!die(self));
self++;
User avatar
Tracer
 
Posts: 124
Joined: Wed Oct 08, 2003 4:07 am

Postby Jason » Tue Oct 14, 2003 12:07 am

Ok I see it now, stupid me, I shold of saw that. Thanks a lot for the help.
Jason
 


Return to For Beginners

Who is online

Users browsing this forum: Google Adsense [Bot] and 2 guests