need help with testing argv sum program

This is a discussion on need help with testing argv sum program within the C Programming forums, part of the General Programming Boards category; Hi, I wrote program that can add two integers from command prompt using argv. I tried to write a test ...

  1. #1
    m&m
    m&m is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    need help with testing argv sum program

    Hi,

    I wrote program that can add two integers from command prompt using argv. I tried to write a test program like below that would automatically test all the possible pairs of integers (from 0 to 99) that can be added, and if valid sum comes out, it wold print valid, else invalid. But of course, to have the loop work, the program itself wouldn't be able to use the keyboard to enter the parameters needed.

    Can anyone help? Seems like so far, I'm not doing something right. Or is there a better way for this? Thanks.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int funcAdd(int, char**);
    int StringToInt(char*);
    
    int main(int argc, char** argv)
    {
    	int i;
    	int j;
    	int test_sum;
    	int sum=0;
    	int count;
    	char** param;
    
    	if(argc!=3)
    	{
           puts("ERROR: Usage: add1test <number1> <number2>");
           return -1;
    	}
    
    	count = argc;
    	param = argv;
    
    	for(i= 0; i<100; i++)
    	{
    		for(j=0; j<100; j++)
    		{
    			test_sum = i + j;
    			printf("test_sum = %d, ", test_sum);
    
    			sum = funcAdd(argc, argv);
    			printf("sum = %d ", test_sum);
    
    			if(test_sum == sum)
    				printf("valid\n");
    			else
    				printf("in-valid\n");
    		}
    	}
    
    	return 0;
    }
    	
    int funcAdd(int count, char** param)
    {
    	int number1 = StringToInt(param[1]);
    	int number2 = StringToInt(param[2]);
    
    	int sum = number1 + number2;
    	return sum;
    }
    
    /*function StringToInt
    gets a numeric string from the user and returns the integer
    representation of the string
    */
    int StringToInt(char* input)
    {
       int  number = 0;
       int  inputLen;
       int  i;
    
       inputLen = strlen(input);
       for (i = 0; i < inputLen; i++)
       {
           int digit = input[i] - '0';
           number *= 10;
           number += digit;
       }
       return number;
    }
    Last edited by m&m; 07-12-2010 at 05:41 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If I'm adding up integers into sums, I don't see why ANY of the sums would be "invalid".

    The answer may not be right, because I used bad logic, but numbers are numbers (and have set values), and their sums would always be valid.

  3. #3
    m&m
    m&m is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    i'm just trying to get it to work. Of course, all the sums should be valid if you entered in valid integers, so that means somewhere my test program is not written right somewhere... but where?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    		test_sum = i + j;
    			printf("test_sum = %d, ", test_sum);
    
    			sum = funcAdd(argc, argv);
    			printf("sum = %d ", test_sum);
    Did you mean to print test_sum both times? seems you should print sum the second time.

  5. #5
    m&m
    m&m is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    I rewrote the program, sort of got it to do what I want. Any tips on how to make it better??

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int funcAdd(char*, char*);
    int StringToInt(char*);
    
    int main(int argc, char** argv)
    {
       int i;
       int j;
       int valid_count=0;
       int invalid_count=0;
       int test_sum;
       int sum=0;
       char param1[16];
       char param2[16];
    
       for(i= 0; i<=100; i++)
       {
           for(j=0; j<=100; j++)
           {
               test_sum = i + j;
    		   //printf("add %d %d:  ", i, j, test_sum);
    
               /*
               The funcAdd takes strings, so in testing the program 
               we must first generate the strings
               */
               sprintf(param1, "%d", i);
               sprintf(param2, "%d", j);
    
               sum = funcAdd(param1, param2);
    		   printf(" add %3d %3d: sum = %3d  ", i, j, sum);
    
               if(test_sum == sum)
    		   {
    			   printf("valid\n");
    			   valid_count++;
    		   }
    				
               else
    		   {
    			   printf("invalid\n");
    			   invalid_count++;
    		   }
    	   }
       }
       printf(" -----------------------------");
       printf("\n # valid: %d, # invalid: %d\n", valid_count, invalid_count);
       return 0;
    }
    
    int funcAdd(char* param1, char* param2) 
    {
       int number1 = StringToInt(param1);
       int number2 = StringToInt(param2);
    
       int sum = number1 + number2;
       return sum;
    }
    
    /*function StringToInt
    gets a numeric string from the user and returns the integer
    representation of the string
    */
    int StringToInt(char* input)
    {
       int  number = 0;
       int  inputLen;
       int  i;
    
       inputLen = strlen(input);
       for (i = 0; i < inputLen; i++)
       {
           int digit = input[i] - '0';
           number *= 10;
           number += digit;
       }
       return number;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    The way I see it is that funcAdd is quite useless. You can easily do what it does by directly using operator+. What you are really testing is the StringToInt function, so you might as well do it directly instead of bothering with funcAdd.

    Oh, and you might want to make your indentation more consistent, though at least it is better than in the original post.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. Need Help Writing a Sum of Digits program
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 03-13-2008, 03:06 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  5. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21