0

I need help understanding why code isn't working. I don't have a full understanding how to use argc and argv. I need to do addition, subtraction, multiplication, and division operations that can have multiple inputs. The addition is adding them all together, i was able to get that one. For the others, its taking the first input and either subtracting/dividing/multiplying by the rest. I'm using linux. To input the numbers, i would do (./.a.out 1 2 3 4) to input integers. Thank you


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

int main( int argc, const char * argv[])
{
    float sum=0.0;
    float sub=0.0;
    float div=0.0;
    float mult=0.0;

    char ch;

    printf("Pick a function \n");
    scanf("%c", &ch);  

    int x,y;

    switch(ch)                                                        
    {
    case ('A'):                                                           
        {
        x = argc -1;
        for(y=0;y<x;y++)
        {
        sum = sum + atoi(argv[y+1]);
        }
        printf("The result of addition is %f\n",sum);             
        break;
        }
    case ('S'):
        {
        x = argc -1;
        for(y=0;y<x;y++)
        {
        sub = sub - atoi(argv[y+1]);
        }
        printf("The result of subtraction is %f\n",sub);             
        break;
        }
    case ('M'):
        {
        x = argc -1;
        for(y=0;y<x;y++)
        {
        mult = mult * atoi(argv[y+1]);
        }
        printf("The result of multiplication is %f\n",mult);              
        break;
        }
    case ('D'):
        {
        x = argc -1;
        for(y=0;y<x;y++)
        {
        div = div / atoi(argv[y+1]);
        }
        printf("The result of division is %f\n",div);              
        break;
        }
    }
}
  • 4
    "_I need help understanding why code isn't working._" 1) How isn't it working? 2) Did you try stepping through your code with a debugger, while investigating the values of variables, at each execution step? – Algirdas Preidžius Sep 23 '19 at 23:06
  • 2
    what your program should do, and what is not doing exactly? – phoenixstudio Sep 23 '19 at 23:08
  • 1
    argv is an array of strings where the first element is the program name and the remaining are the arguments, argc is the length of argv – geckos Sep 23 '19 at 23:09
  • _where the first element is the program name_ – geckos Sep 23 '19 at 23:14
  • @geckos Sorry, I deleted my comment. Somehow I just didn't read that part! :( – csabinho Sep 23 '19 at 23:16
  • @AlgirdasPreidžius, I ran it for each operation, and for multiplication/division i would get zero, no matter how many different inputs i would do. and no i haven't tried a debugger before. – Brandon Gutierrez Sep 23 '19 at 23:27
  • 1
    *argv is an array of strings where the first element is the program name* Not quite. What goes into `argv[0]` is implementation dependent. It is usually the command used to execute the program, and this is not always the same as the program name. There could be some path information, full or relative, in there and possibly abstractions like symlinks obscuring the true naming. And some systems, embedded systems for example where the OS (if any) and the application logic are one, don't have names. – user4581301 Sep 23 '19 at 23:27
  • 1
    @BrandonGutierrez Debuggers are the bomb. When it comes to programmer productivity tools, they probably rank second behind the optimizing compiler. The sooner you familiarize yourself with using debuggers to assist you, the sooner you'll take your place among those annoying dudes in the class who finished their programming homework in, like, half an hour and look like geniuses. – user4581301 Sep 23 '19 at 23:32
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Max Vollmer Sep 23 '19 at 23:40
  • @user4581301 Would a debugger be able to take in the inputs such as( /.a.out # # # #) and do the operations needed? Im trying to understand why it wont take the first integer and divide by the rest or multiply by the rest. – Brandon Gutierrez Sep 23 '19 at 23:40
  • Debuggers always have a way to allow you ro specify the command line arguments. The exact method depends on the debugger. GUIs usually have an *arguments* option in some property page. GDB you just type the suckers in. – user4581301 Sep 24 '19 at 00:30

1 Answers1

2

Here's the blueprint of what you need to fix:

    #include <iostream>
    // etc..
    using namespace std;

    float mult = 1.0;
    float div = 1.0;

    case ('M'):
    {
            x = argc - 1;
            for (int y = 0; y < x; ++y)
            {
                mult = mult * atoi(argv[y+1]);
            }
            cout << "The result of multiplication is" << mult << endl;             
            break;
    }

For subtraction and division, just have sub = atoi(argv[1]);/div = atoi(argv[1]); outside of the loop, and set loop control variable to int y = 1 for both.

xv8
  • 183
  • 2
  • 11
  • 1
    this worked for the multiplication, now im having trouble with the division, keeps giving zero. Any ideas to why its zero even if the first input is bigger than 0 ~~~~ #include float mult = 1.0; float div = 1.0; case ('M'): { x = argc - 1; for (int y = 0; y < x; ++y) { mult *= atoi(argv[y+1]); } printf("The result of multiplication is %f\n",mult); break; } – Brandon Gutierrez Sep 24 '19 at 02:21
  • Ive updated my comment above to be more explicit, you do the same thing for division. Another final thing to keep in mind is try to use c++ expressions, functions and libraries when writing c++ code, and be cognizant of data types (eg atof() instead of atoi() function). Be sure to mark as the answer if this works for your setup:) – xv8 Sep 24 '19 at 02:36