tags:

views:

600

answers:

6

Okay here's the program I have typed up(stdio.h is included also):

/* function main begins program execution */
int main (int argc, char *argv[])
{
    int x; /*first number input*/
    int y; /*second number input*/
    int sum; /* variable in which sum will be stored */
    int product; /* variable in which product will be stored */
    int quotient; /* variable in which x divided by y will be stored */
    int md; /* variable in which the modulo division of x divided by y */

    x = argv[2]; /*assign total to x*/
    y = argv[3]; /*assign total to y*/

    if (argc ==3) {
       sum = x + y; /* assign total to sum */
       printf("%d\n",sum); /*print sum*/

       product = x * y; /*assign total to product*/
       printf("%d\n", product); /*print product*/

       quotient = x / y; /*assign total to quotient*/
       printf("%d\n", quotient); /*print quotient*/

       md = x % y; /*assign total to md*/
       printf("%d\n", md); /*print md*/
    } /*end if*/

    if (argc !=3) {
       printf("need two integers\n"); /*need two integers*/
    }

    return 0; /*indicate program ran successfully*/
} /*end of main*/

When I run it through the compiler it says that in lines 15 and 16 (x= and y= lines) "assignment makes integer from pointer without a cast." How do I fix this?

**I changed it to x=atoi(argv[2]) and y=atoi(argv[3]) and it fixed that problem. But as always something else has now screwed up. Now when I run the program I get:

163 [main] a_4312 _cygtls::handle_exceptions: Error while dumping state

Segmentation fault

I read that this means I didn't allocate memory for the output or something like that...can anyone give me a hand here?**

+1  A: 

argv[2] gives you a char *. You need conversion to an int:

x = atoi(argv[2]);
Ates Goral
+2  A: 

The compiler error was due to you assigning text to a number. You actually need to call a function to convert between the 2 atoi.

The most important runtime error is the seg fault, this is caused by using the wrong indexes into the array, c has 0 based arrays so the first element is 0, this means the 2 arguments you want are 1 and 2 (since the first argument is the executable's name).

Finally, you may want to check the y value before the divide and mod since if it's 0 you'll get divide by zero problems.

x = atoi(argv[1]); /*assign total to x*/
y = atoi(argv[2]); /*assign total to y*/
Tom
2 and 3 is wrong, it should be 1 and 2
Peter Miehle
Naveen
I actually pointed that out in the comments of the question - have updated here.
Tom
+2  A: 

Aside from the provided solution, you should also check whether the required arguments are there (move the last if block to before the assignment to to x and y) before trying to pass them to atoi or you will get run-time issues.

dreamlax
+4  A: 

The numbers you get passed are argv[1] and argv[2], not argv[2] and argv[3].

Eduard - Gabriel Munteanu
A: 

Thanks everyone the program runs fine now! Yall were a lot of help! Now I can finally go to bed...it's 2:30 am here lol. Thanks again!

Kaity
You're welcome and Congrats.Stick around and participate in the discussions... don't just drop in for homework. :)
abelenky
+1  A: 

If your command line is:

prompt>> myprogram 5 6

argv[0] should be the program name (myprogram) argv[1] should be the first parameter ("5") argv[2] should be the second parameter ("6")

I think you're referring to argv[3], which doesn't exist.

abelenky