Thread: Carrying ints a chars over to other functions.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    31

    Post Carrying ints a chars over to other functions.

    I'm making a program where you input a word or phrase, and my program will automatically capatilize some of the letters in it. For example, if your entered
    Code:
    I am testing this program.
    , my program might generate
    Code:
    i Am TEstIng tHIS prOgRAm
    . My problem is that I have one function getPhrase() where the phrase or word entered by the user is stored in an array, and autoCapper() which looks something like:
    Code:
    for (i = 0; i <= numOfLettersInChar; i++) {
       if (randNum == 1 || 2 || 3) {
          if (char[i] == 'a') char[i] = 'A';
          if (char[i] == 'b') char[i] = 'B';
          // and so on
       }
    // ...
    What I need to do is carry over the int for number of chars in the array, and what each of the chars are from the getPhrase() to the autoCapper(). I can do one or the other by having getPhrase() return a char or int value, but I guess what I really need it to do is return a char and an int.

    Sorry if this is a bit confusing, I'm a pretty novice programmer, so I can't understand too many technical answers to my question, and I'm not quite sure how to explain some of the things I'm doing.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    8
    Well, what you could do is create a return structure.

    Code:
     typedef struct _CharAndInt
    {
                 int intValue;
                 char charValue;
    }rStruct;
    
    rStruct getPhrase()
    {
              //Do your own stuff here...
              rStruct retVal;
              retVal.charValue = char_to_return;
              retVal.intValue = int_to_return;
             
              return retVal;
    }
    From the sound of it, that's what you are trying to do.. If not, try to be a little more specific, maybe giving more code of both functions.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you need to return two values from a function, you can combine them in a struct or class and return an instance of the struct/class. Another option is to use pass-by-reference to pass in a parameter that will be updated with the output value.

    My guess is pass-by-reference is what you want. If you are not familiar with that concept, then you might be able to redesign your function so that you don't have to return both values.

    >> if (randNum == 1 || 2 || 3)
    By the way, this is the wrong use of multiple ||'s. You have to put randNum == for each potential value.

    >> if (char[i] == 'a') char[i] = 'A';
    You can use toupper in <cctype> to do this automatically instead of writing out all 26 cases.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    so, Daved, does that meen I'd put
    Code:
    if (randNum == 1 == 2 == 3)
    or something?
    and i hav no idea what u mean by
    use toupper in <cctype>
    also, if i use the method marek said, how will i seperate retVal back into a char and int?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    just an afterthought: or do u mean
    Code:
    if (randNum == 1 || randNum == 2 || randNum == 3)
    ?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your second stab at it is correct. This, that is:
    Code:
    if (randNum == 1 || randNum == 2 || randNum == 3)
    and i hav no idea what u mean by
    use toupper in <cctype>
    Google for "site:cppreference.com toupper". Here it is. http://www.cppreference.com/stdstring/toupper.html

    Basically you pass it a character, and it will return that character converted to uppercase.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    how can toupper() be an int if it returns a char?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's one of the greatest mysteries of the universe. No, but it does require a somewhat lengthy explanation.

    First of all, toupper() is a C function, so it was invented and implemented in C. Believe it or not, in C, character constants like 'A' are actually int values, unlike in C++ where they are chars. Also, if there is no prototype for a function (i.e., you forgot to include <stdio.h>/<cstdio>), all char arguments are automatically cast to int, and int return value is assumed. Lastly, several values such as EOF that one might pass to toupper() are in fact int values, and if you tried to squeeze them into a char you might get unexpected results. (I can elaborate on that if you like but I think this is in-depth enough. )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    ok, thx, the toupper() thing speeds things up quite a bit, but now my problem is that my program doesn't seem to redo the getRandNum() function when I tell it to. This results in all the letter becoming capitals, or all the letters staying the same.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    here's part of my code that capitalizes the letters:
    Code:
         int xy = 1;
         while (xy <= nl)
         {
         randNumGen();
         if (randNumGen() == 1 || randNumGen() == 3 || randNumGen() == 5) {
                              bAt[xy] = toupper(bAt[xy]);
                             }
             xy++;
         }
             cout << "You're autmatically capitilized phrase is: ";
           xx = 1;
           while (xx <= nl) {
                 cout << bAt[xx];
                 ++xx;
                 }
    and I use the code provided on cprogramming.com to generate the random numbers.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Post the randNumGen function.

    Also, you should be saving the return value from that function call and using the saved value in the if. It looks like you had it right in your first bit of code with the randNum variable, but in this code you are calling the function up to 4 times and you will be getting 4 different values.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
         randNumGen();
         if (randNumGen() == 1 || randNumGen() == 3 || randNumGen() == 5) {
    I'm guessing you generate a random value from 0-RAND_MAX which is at least 2^15-1. In that case it would hardly ever be 1, 3 or 5 so you would almost never get capitals. You want to generate a number from 1-6 or so.

    Assuming that you limit the range of the random numbers, why not just use
    Code:
    x = randNumGen();
    if(x < 3) {
        /* capitalize */
    }
    (assuming 0<=x<6) or something rather than testing individually against some odd numbers?

    Whatever you do, be sure to call randNumGen() only once as Daved mentioned and save the result in a variable.

    [edit]
    Code:
           xx = 1;
           while (xx <= nl) {
                 cout << bAt[xx];
                 ++xx;
                 }
    What's wrong with
    Code:
    cout << &bAt[1];
    or perhaps you meant
    Code:
    cout << bAt;
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    8
    Quote Originally Posted by jmajeremy View Post
    also, if i use the method marek said, how will i seperate retVal back into a char and int?
    let's pretend you've got the function above. I'll show you exactly how here:

    Code:
    int main()
    {
            //The structure
             rStruct rVal;
    
             //Use the command
             rVal = getPhrase();
     
              //If you want to set the int...
             
              int iValue = rVal.intValue;
             
              //If you want to set the char
             
             char cValue = rVal.charValue;
     
             return 0;
    }
    You need to use the member-access operator ( . ) to access the value's inside.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems we are talking about a phrase, so a C++ string would be most appropriate (already knows its size).

    If std::string is not allowed, the prototypes could be like that:
    Code:
    //reads user input into buffer, assuming it is big enough
    //returns number of characters
    int getPhrase(char* buffer);
    
    //capitalizes random letters (30&#37; - or some other % of the time)
    void autoCapper(char* buffer, int length);
    As to the randomness:
    Code:
    randNumGen();
         if (randNumGen() == 1 || randNumGen() == 3 || randNumGen() == 5) {
    There are 4 calls to randNumGen, each returning a different value. The first result is thrown away. The condition will be true only if: the second random value equals 1, the third value equals 3 or the fourth value equals 5 (and not if a random value equals 1, 3, or 5).

    In short, you need to store the return value of the first call and use this in the if-condition.

    There may be also simpler ways to determine if a random condition should evaluate to true with a known probability. For example, if you wanted something to happen 25% of the time, it might look like this:
    Code:
    if (rand() < RAND_MAX * 0.25) {}

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    ok, i seem to have solved most of the problems by using some of your suggestions, and completely rearranging my function, but there seems to be a new problem. I decided to instead of returning a char and a int to just return a char* and count the chars in it, since that what the int contained, hence:
    Code:
    char* chFunc1 = func1();
    int len = chFunc1.size();
    This always returns the error
    'size' has not been declared
    .
    Don't know what I should do, I included all of the following:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <cctype>
    #include <windows.h>
    #include <string.h> // I also tried it as <string>
    #include <fstream>
    #include <sstream>
    #include <stringstream>
    Is there anything that I should be including here? Not as far as I could tell from anything I found on the web.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. writing strings chars from ints
    By deleeuw in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2003, 09:09 AM
  3. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  4. Chars - ints
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 08:22 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 11:33 AM