4648 Shares

C : If "argv" in "main(int argc,char *argv[]" point to strings,how do I input 2 integers from CMD to add them?

C : If "argv" in "main(int argc,char *argv[]" point to strings,how do I input 2 integers from CMD to add them? Topic: How to write a string in c programming
June 19, 2019 / By Debby
Question: I read that argv[] is an array of pointers to strings.If inputs from command prompt are considered as strings, then how do I input 2 integers from the command prompt, so that I can add them through my program and display them?I know it will be basic stuff for you so can you kindly give me a little code for that or tell me how to change my following code: //Integer input through command prompt of windows #include int main(int argc,char *argv[]) { int sum; //What to write here? printf("The sum of the two integers is %d",sum); return 0; }
Best Answer

Best Answers: C : If "argv" in "main(int argc,char *argv[]" point to strings,how do I input 2 integers from CMD to add them?

Britney Britney | 10 days ago
well, there is a function in called atoi(). this function takes in a string and converts it to an int. it returns 0 if there is an error. also, recall that argv[0] is the program name. so here's the program #include #include int main(int argc, char* argv[]) { //do some error checking if (argc < 3) { printf("Too few arguments\n"); exit(1); } //this converts the first meaningful command-line argument into an int int num1 = atoi(argv[1]); //error checking if (num1 ==0) { printf("first number is in an illegal format\n"); exit(1); } //convert second command-line argument to an int and do error checking int num2 = atoi(argv[2]); if (num2 ==0) { printf("second number is in an illegal format\n"); exit(1); } int sum = num1 + num2; printf("The sum of %d and %d is %d\n", num1, num2, sum); return EXIT_SUCCESS; } hope this helps, and contact me with questions. (note: I'm not amazing with programming in C, but I might be able to help)
👍 228 | 👎 10
Did you like the answer? C : If "argv" in "main(int argc,char *argv[]" point to strings,how do I input 2 integers from CMD to add them? Share with your friends

We found more questions related to the topic: How to write a string in c programming


Britney Originally Answered: C programming help. Trying to make a program that converts user input integers into words -99 to 99?
Try this as a starting point printf("Please enter an integer from -99 to 99:\n\n"); scanf("%d", x);//take a number from the user -99 to 99 while (x > 99 || x < -99) {//determine if the number is within the specified range printf("The number entered is outside the range, please enter a number within the range"); printf("\nPlease enter an integer from -99 to 99:\n\n"); scanf("%d", x);//take a number from the user -99 to 99 } Now check this link for the code in VB Have fun.

Alicia Alicia
    if (argc != 3) {         printf("Please give 2 integer arguments\n");         return 0;     }     int a = atoi(argv[1]);     int b = atoi(argv[2]);     if (a == 0) {         if (strcmp(argv[1], "0")) {             printf("Please give 2 integer arguments\n");             return 0;         }     }     if (b == 0) {         if (strcmp(argv[2], "0")) {             printf("Please give 2 integer arguments\n");             return 0;         }     }     int sum = a + b; Note that the comparison with 0 is necessary because atoi returns 0 on failure. So there is no way to tell from the return value whether it was an error, or the user actually entered the number 0. So if the value is 0, we manually compare it with strcmp.
👍 90 | 👎 4

Uel Uel
The quantity of individuals who cannot spell "Murphy" is appalling. Also, for those who quite wish to appease the side of Chaos, you could go into a fire condominium and yell "film." maintain them on their toes.
👍 81 | 👎 -2

Rimmon Rimmon
You can supply command line arguments in IDE's thorugh arguments option in their menus. When we opt this option in their manu, we get pop up window in which we can enter all the arguments before executing the program
👍 72 | 👎 -8

Rimmon Originally Answered: Please help writing java program adding two integers and converting it into its own array of integers?
You are implementing addition! The only really tricky think is the possible carry. Your code look entirely too complicated. I've been thinking about how I would write such a routine and so here are my thoughts: Note that the indexing of strings is from the left or high order end. To do addition you need to do it from the other end. I'll assume that the strings have the same length. The result can possibly be longer by 1: ex 9+9 = 18. int carry = 0; int temp = 0; String result = ''"; for( int k=str1.length()-1; k>=0; k--) { ...temp = Integer.parseInt(str1[k])+ ......Integer.parseInt(str2[k]) + carry; ...carry = 0; ...if ( temp > 9 ) { ......carry = 1; ......result += temp%10; ...} } if( carry == 1) result += carry; // but result needs to be reversed, you can figure out how to do it. You can also use string builder or string buffer to do it. See how that works? Add the 2 digits and the carry, then figure if that sum is > 9, if so then set the carry for the next higher column and set the result = the amount over 9, just like in real arithmetic. ++add Here's my code that works. The inputs have to be the same length. To generalize, left pad the small one with zeros. Also, note how I aligned the columns, just like you learned to do in the 4th grade: java.util.Scanner kb = new java.util.Scanner(System.in); System.out.print("String 1: "); String str1 = kb.nextLine(); System.out.print("String 2: (same length!): "); String str2 = kb.nextLine(); int carry = 0; int temp = 0; String result = ""; for (int k = str1.length() - 1; k >= 0; k--) { temp = Integer.parseInt(str1.substring(k, k + 1)) + Integer.parseInt(str2.substring(k, k + 1)) + carry; carry = 0; if (temp > 9) { carry = 1; temp %= 10; } result += temp; } if (carry == 1) { result += carry; } String width = String.format("%s", result.length()); // reverse it StringBuilder sb = new StringBuilder(result); result = sb.reverse().toString(); System.out.printf( "\n%" + width + "s\n%" + width + "s\n" + "---------\n%" + width + "s\n\n", str1, str2, result); } output: String 1: 999 String 2: (same length!): 128 999 128 --------- 1127

If you have your own answer to the question how to write a string in c programming, then you can write your own version, using the form below for an extended answer.