Thread: Help With Parameters

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by peckitt99
    i need to output it using i code line similar to

    Code:
    Sum = Add(a,b,ArrLength);
    No, You need to output it using code line similar to what was written in your assignment description:
    Code:
    Add(A,B,Result,ArrLength);
    You should fill A and B arrays before you call the function,
    you should pass in the last argument the lenght of the arrays,

    and you should put as a 3rd parameter the empty array that will be filled by your function.

    So after the function finishes - you just take the contents of the Result array and output it as you wish.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i got that program working and now trying to modify the previous function such that it only adds ‘n’ characters of the second parameter to the first. first of all i am having trouble passing the first output of string characters into main (i am not using any string functions as it is not allowed which is kind of a pain).

    This is what i have

    Code:
    #include <iostream>
    
    using namespace std;
    
    int Number(int a, int b);
    char Add(char*a, char*b, char*StrOut);
    
    int main()
    {
    	char a[3] = {'T','i','m'};
    	char b[7] = {'P','e','c','k','i','t','t'};
    	char StrOut[20];
    	
    
    	cout << "Sum = " << Number(8,2) << endl;
    
    	int sum = Add(a,b,StrOut);
    
    	system ("pause");
    }
    
    int Number(int a, int b)
    {
    	return a+b;
    }
    
    char Add(char* a, char* b, char* StrOut)
    {
    	  int i = 0;
          while (a[i] != '\0') 
          {
              StrOut[i] = a[i];
              i ++;
          }
      
      StrOut[i] = ' ';
      i++;
      
      int j = 0;
      while (b[j] != '\0') 
          {
              StrOut[i] = b[j];
              j ++;
              i++;
          }
      StrOut[i] = '\0';
      return StrOut[i];
    }

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
          while (a[i] != '\0')
    You havn't put '\0' in your array, you even havn't made a room for it

    use
    Code:
    char a[]="Tim";
    compiler then will do both things for you
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah i have corrected that and changed everything but still not sure how to return StrOut to int sum = Add(a,b,StrOut); because at the moment nothing outputs.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you shouldn't

    Just try to output the StrOut after you fill it inside the function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    would i need another function so that it only adds the ‘n’ characters of the second parameter to the first?

  7. #22
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or you can modify the current function after you make it working. you'll need slightly modify the second loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #23
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    when it asks for the "n characters" does it mean show characters up to n? for example

    "hello world"

    n=7

    show "hello wo"?

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I bet on "hello w" - space is also a character
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #25
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    As far as I've understood your objective is too take two strings and a number 'n', and add up the first 'n' characters of str2 to str1. So just add another parameter to Add, so that 'j' simply adds up to 'n'. Also, I think you've made a grave mistake in : int sum = Add(blah...). Variables can only be initialised at the beginning of main(). So int sum up there, and do sum = Add(blah...) here. Then print it. But as vart says, you could save your self a lot of space, code and time and just output code as it comes.

    You don't need a StrOut then, and you can ditch 'j'. Just use 'i' again and again, and use for loops (to make things neater) for (i=0; a[i] != '/0'; i++). Also for the last condition, take the parameter 'n' and put for (i=0; i <j && a[i] != '/0'; i++) (the && bit is to take care if j > than the length of the string). You also don't have to return anything, so just make it a void function (and you don't have to have sum either).

  11. #26
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i am unsure how to implement what you are saying as i am slightly confused about what u are saying

  12. #27
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Ok, I'm sorry to have confused you.
    I said that you should restructure your program a bit. Instead of adding these numbers to another variable StrOut (as you are doing now), and then printing the string out, you can just print the characters of the string as they come. Where ever you have a StrOut assignment (like StrOut[i] = b[i] ), you simply replace it with cout << b[i]. This saves you a lot of variables and naming and space and time and bugs.
    If you can do away with strOut, then you can also not define the variables sum, and j, because they aren't required any more. Make your function Add a void function (void Add(char* a,...) ) this means that it doesn't need to return a value.
    Also, you can reuse the variable 'i' in the function Add. I personally like for loops better, as they include the increment operation into it, but it's upto your taste really.
    Now, if what vart says is what you want, i.e. by printing 'n' characters, you just want print 'n' character of str1 + str2 (in this case "Tim Peckitt"), then you start your 'i' = 0 at the beginning of the function Add. Then if you use for loops, ignore the first part (for (;a[i] != '/0';i++). Also, you'll have to check for the condition that i < n. So what you do is add this condition into you looping condition. So for a while loop, you'll have while (a[i] != '/0' && i < n). This includes both conditions in one. You could also put the if condition:
    [code]
    if (i >= n)
    break;
    and retain the same code as you have now.

    You have made a mistake by putting the code: int sum = Add(a,b,StrOut); This is a definition, and all definitions must be done at the beginning of the code block. So restructure your code as:
    Code:
    int main()
    {
        int sum;
        char a = "Tim"
        blah blah....
    
        sum = Add(a,b,StrOut);
        system ("pause");
    }
    That's all I had said. And as a general thing, it would be nice if you named your functions better, like Number should be something like add or addNum. And 'Add' should be something like 'cat' or 'catStr' (short for catenate). It makes it easier for people to read and understand what your code is doing.

  13. #28
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    First step - finish your original task in any possible way exactly as it is required by the assigment.
    The modifications and search of other better ways to do the same thing can be done later.

    My advice - read once more your assigment and try to realise what is missing in YOUR code for finishing it.

    PS.
    Don't use
    Code:
    char a = "Tim"
    it has 2 errors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #29
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well from my view everything works apart from the nth character. and im just getting a bit confused with everything that is getting said and how to implement everything. i know you are thinking im thick n are annoying n arquard but as u can see im a beginer at programming
    Last edited by peckitt99; 10-29-2006 at 02:30 PM.

  15. #30
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i know you are thinking
    No, you don't
    Just go on with what you are doing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Additional parameters for operator delete
    By darksaidin in forum C++ Programming
    Replies: 0
    Last Post: 09-21-2003, 11:46 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM