Hi, I have been working on this program for quite some time but I can't figure out what's wrong with it. The program compiles and this is the the output I get:

?Invalid number of arguments
Press any key to continue . . .

Can somebody please take a look at this program and give me some ideas?

Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TERMINAL -9

void swapArgs(int *a, int *b);
void divideArgs(int *a, int *b);
double *powerArgs(int *a, int *b);

int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("?Invalid number of arguments\n");
        system("pause");
        exit(1);
    }

    int parmA = atoi(argv[1]);
    int parmB = atoi(argv[2]);

    printf("A=%d, B=%d. ",parmA,parmB);
    swapArgs(&parmA,&parmB);

    printf("Swapped values A=%d, B=%d.\n",parmA,parmB);
    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    printf("Quotient of %d / %d is ",parmA,parmB);
    divideArgs(&parmA, &parmB);
    printf("%d, remainder is %d\n",parmA,parmB);
    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));
    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    system("pause");
    exit(0);
}

void swapArgs(int *a, int *b)
{
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;   
}


void divideArgs(int *a, int *b)
{    
      int temp;
      temp = *b;
      *b = *a;
      *a = temp;
      
    int remainder;
    
    *a = *b / *a;
    
    remainder = *a;
}

double *powerArgs(int *a, int *b)
{
   static double result;
   int i;
   for ( i = 0, result = 1; i < *b; ++i )
   {
      result *= *a;
   }
   return &result;
}

What does your command-line invocation look like?

?Invalid number of arguments
Press any key to continue . . .

int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("?Invalid number of arguments\n");
        system("pause");
        exit(1);
    }
}

Well, it is expecting to be run with 2 arguments, if those arguments are not present on the commandline, the program just exits with the message.

So try using it like:

> program.exe 123 456

It seems to be expecting strings that atoi() converts to integers.

The program compiles and this is the the output I get:

?Invalid number of arguments
Press any key to continue . . .

Can somebody please take a look at this program and give me some ideas?

You must not be entering the right amount of parameters when running the program at the command line.
It requires three: the name of the program and the two strings representing numbers.

Well, it is expecting to be run with 2 arguments, if those arguments are not present on the commandline, the program just exits with the message.

So try using it like:

> program.exe 123 456

It seems to be expecting strings that atoi() converts to integers.

Can you please explain what you meant by "> program.exe 123 456"?

Actually, there are two arguments

if (argc != 3)
    {
        printf("?Invalid number of arguments\n");
        system("pause");
        exit(1);
    }

    int parmA = atoi(argv[1]);
    int parmB = atoi(argv[2]);

Can you please explain what you meant by "> program.exe 123 456"?

Actually, there are two arguments

Actually there are three, from the program's point of view, that is. The name of the program itself counts as one argument. Aia pointed that out ^^ but I think you missed it. So, given that you run the program as follows:

program.exe 123 456

then your program sees ..

argv[0] -> "program.exe"
argv[1] -> "123"
argv[2] -> "456"

and argc would be equal to three.

Can you please explain what you meant by "> program.exe 123 456"?

Are you familiar with a command shell ("DOS window")?
Are you running from an IDE? If so, do you know how to set up command-line arguments?
(Hint: these are not mere simple questions, but also solutions.)

Are you familiar with a command shell ("DOS window")?
Are you running from an IDE? If so, do you know how to set up command-line arguments?
(Hint: these are not mere simple questions, but also solutions.)

I'm so new to programming. I'm taking this class online and makes even more difficult. To be honest, I don't know what IDE and command-line arguments are. I was given the program below to fix it and make it to work and all my work is shown in my first post. I don't care much for the extra credit question.

#include <stdio.h>
#include <stdlib.h>

/* MAIN */

int main(int argc, char **argv) {
    if (argc != 3) {
       printf("?Invalid number of arguments\n");
       system("pause");
       exit(1);
       }

    int parmA = atoi(argv[1]);
    int parmB = atoi(argv[2]);

/* Part A: Swap the values.  Value in parmA is moved to parmB and vice versa */
/* Reset the original values after we print the result */

   printf("A=%d, B=%d. ",parmA,parmB);
   swapArgs(&parmA,&parmB);
   printf("Swapped values A=%d, B=%d.\n",parmA,parmB);
   parmA = atoi(argv[1]);
   parmB = atoi(argv[2]);

/* Part B: Divide parmA by parmB.  Put the quotient in parmA, remainder in parmB */
/* Reset the original values after we print the result */

   printf("Quotient of %d / %d is ",parmA,parmB);
   divideArgs(&parmA, &parmB);
   printf("%d, remainder is %d\n",parmA,parmB);
   parmA = atoi(argv[1]);
   parmB = atoi(argv[2]);

/* Part C: Raise parmA to the power of parmB.  Return pointer to the result */    
/* Reset the original values after we print the result */

   printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));
   parmA = atoi(argv[1]);
   parmB = atoi(argv[2]);

 

/* Extra credit: Fibonacci generator function.  The first call to fibArgs returns the first Fibonacci number (0) and each subsequent call to fibArgs returns the next Fibonacci number.  In this case parmA will specify the number of Fibonacci numbers to display.  Fibonacci numbers:
             F(0) = 0
             F(1) = 1
             F(n) = F(n-1)+F(n-2)
   The first few numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*/

  int idx;
  printf("The first %d Fibonacci Numbers are: ",parmA);
  for(idx=0; idx < parmA; idx++) {
     printf(" %d",*fibArgs());
     }
  printf("\n"); 
  system("pause");
  exit(0);
}

I'm so new to programming. I'm taking this class online and makes even more difficult. To be honest, I don't know what IDE and command-line arguments are. I was given the program below to fix it and make it to work and all my work is shown in my first post. I don't care much for the extra credit question.

I asked how you make the program run.

(But I get the feeling that producing an answer might be like pulling teeth.)

It is very likely that your issue has absolutely nothing to do with the code, which worked just fine when I ran the program with the expected number of arguments. The problem is not IN THE CODE. The problem is HOW DO YOU RUN THE PROGRAM.

Please try to describe what you use to build the code: Dev-Cpp, Microsoft XXX, etc. (generally speaking, IDEs -- which, by the way, is a very easy thing to look up using an online search).

Oh, I use Microsoft Visual C++ 2008 Express edition.

To run it from the MS IDE with command-line arguments:

Project -> ProjectName Properties...
Configuration Properties -> Debugger -> Command Arguments

To run it from the MS IDE with command-line arguments:

Project -> ProjectName Properties...
Configuration Properties -> Debugger -> Command Arguments

Yea but this is for Visual Studio 2003 and I'm using 2008. Do you know how to do it in 2008 version?

Thank you so much for all your help.

Yea but this is for Visual Studio 2003 and I'm using 2008.

It's identical, I just checked it to make sure. Mine just says "Properties" on the project menu but I've seen it say "MyProjectName" Properties at points. Click on configuration properties on the sidebar of that window to get it to expand.

It's identical, I just checked it to make sure. Mine just says "Properties" on the project menu but I've seen it say "MyProjectName" Properties at points. Click on configuration properties on the sidebar of that window to get it to expand.

Can you please compile my program and let me know if it is working? I'm still having trouble with it.

Thanks!

Can you please compile my program and let me know if it is working? I'm still having trouble with it.

Thanks!

That has been done for you already. Dave Sinkula told you, here. Perhaps, it will be better if you practice some more basic steps?

I got it now! Thank you all so much for your help.

Happy coding to you all! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.