Problem passing argument into function, basic problem

This is a discussion on Problem passing argument into function, basic problem within the C++ Programming forums, part of the General Programming Boards category; Group, Again, I am new at this and need help figuring out how to pass an int that I got ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    15

    Problem passing argument into function, basic problem

    Group,
    Again, I am new at this and need help figuring out how to pass an int that I got from user input into a function to use it. I am trying to print out the words to a string of numbers.
    I got the input from user.
    I got an absolute value of the input.
    I then separate the string into individual digits and name them.
    I can print these out.
    Then I started my if statement by checking if the original input was zero, and if it is, printing zero and exiting.
    Then I an trying to pass the digits into a switch function and this is where I go off the rails.
    Please give me some comments and suggestions that do not include using arrays and pointers. I am sure there are much easier ways.
    I believe that I should be able to do it this way. What do you think.
    Code:
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    
    
    int numpn;
    int numpn2;
    int onesPrint;
    int num;
    int ones;
    int tens;
    int hund;
    int thou;
    int ttho;
    int htho;
    int mill;
    
    const char ifNegative ()// gives back an absolute value of numpn
    {
        if ( numpn < 0)
        {
            return numpn2 = (numpn * -1);
        }
        else if (numpn > 0)
        {
            return numpn2 = (numpn * 1);
        }
        else
        {
            return 0;
        }
    }
    
    
    void printOnes ( int ones)// trying to print the word of the number, here trying to pass the int mill to the function
        {
            switch (ones)
        {
        case '9':
            cout << " nine";
            break;
        case '8':
            cout << " eight";
            break;
        case '7':
            cout << " seven";
            break;
        case '6':
            cout << " six";
            break;
        case '5':
            cout << " five";
            break;
        case '4':
            cout << " four";
            break;
        case '3':
            cout << " three";
            break;
        case '2':
            cout << " two";
            break;
        case '1':
            cout << " one";
            break;
        default :
            break;
    
        }
    }
    
    int main()
    {
    cout << "Please enter a number less than two billion and more then negative two billion" << "\n";
    cin >> numpn; // input
    
    ifNegative (); // changing to absolute value of input
    
    
    int ones = numpn2 % 10 / 1;
    int tens = numpn2 % 100 / 10;
    int hund = numpn2 % 1000 / 100;
    int thou = numpn2 % 10000 / 1000;
    int ttho = numpn2 % 100000 / 10000;
    int htho = numpn2 % 1000000 / 100000;
    int mill = numpn2 % 10000000 / 1000000;// separating the input into addressable digits with names
    
    cout << ones << "\n";
    cout << tens << "\n";
    cout << hund << "\n";
    cout << thou << "\n";
    cout << ttho << "\n";
    cout << htho << "\n";
    cout << mill << "\n";// showing that I have done that
    
    if (numpn == 0)
        {
            cout << "Zero";// starting by changing a 0 input to Zero and stopping (numpn is still around)This works 
    
        }
    else if ( numpn != 0 )
        {
        printOnes ( ones);// trying to call the switch function to print the word. this part does not work.
                            // i don't know how to pass the int ones into the function. Can I do this at all? 
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > case '9':
    Take off all the single quotes, so it's like

    case 9:
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    15
    Thank you very much. it is always the little things.
    Dan

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Clearly you know how to return values from functions and how to send parameters to them, so why not get rid of the global variables?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by tsdad View Post
    Code:
    const char ifNegative()
    Note that the const here is totally useless. Writing const char ifNegative() offers absolutely no advantages over char ifNegative(), it's just more typing for you.

    And as Elysia said, absolutely get rid of those horrible global variables!

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    15
    Elysia and antred,
    Thank you for your comments.
    First, Elysia, thank you for thinking I know what I am doing. I am throwing things in the dark and seeing if they stick as anything else.
    Second, antred, I have removed the const attribute. Question, when would it be advantageous to use the const?
    Third, I have put the final project below. Would I still not need the global variables?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    
    int numpn;
    int numpn2;
    int onesPrint;
    int num;
    int ones;
    int tens;
    int hund;
    int thou;
    int ttho;
    int htho;
    int mill;
    void printTwenty (int tens);
    char ifNegative ()// gives back an absolute value of numpn
    {
        if ( numpn < 0)
        {
            cout << "Negative";
            return numpn2 = (numpn * -1);
        }
        else if (numpn > 0)
        {
            return numpn2 = (numpn * 1);
        }
        else
        {
            return 0;
        }
    }
    
    
    void printOnes ( int ones)// trying to print the word of the number, here trying to pass the int mill to the function
        {
            switch (ones)
        {
        case 9:
            cout << " nine";
            break;
        case 8:
            cout << " eight";
            break;
        case 7:
            cout << " seven";
            break;
        case 6:
            cout << " six";
            break;
        case 5:
            cout << " five";
            break;
        case 4:
            cout << " four";
            break;
        case 3:
            cout << " three";
            break;
        case 2:
            cout << " two";
            break;
        case 1:
            cout << " one";
            break;
        default :
            break;
    
        }
    }
    
    void printMill (int mill)
    {
    
        int ones = mill;
        printOnes(ones);
    
    }
    
    void printHtho (int htho)
    {
    
        int ones = htho;
        printOnes(ones);
    
    }
    void printTtho (int ttho)
    {
    
        int tens = ttho;
        printTwenty(tens);
    }
    
    void printHund (int hund)
    {
    
        int ones = hund;
        printOnes (ones);
        cout << " hundred";
    }
    
    void printTwenty (int tens)
    {
            switch (tens)
        {
        case 9:
            cout << " ninety";
            break;
        case 8:
            cout << " eighty";
            break;
        case 7:
            cout << " seventy";
            break;
        case 6:
            cout << " sixty";
            break;
        case 5:
            cout << " fifty";
            break;
        case 4:
            cout << " forty";
            break;
        case 3:
            cout << " thirty";
            break;
        case 2:
            cout << " twenty";
            break;
        default :
            break;
    
        }
    
    }
    void printTeens ( int ones)
    {
            switch (ones)
        {
        case 9:
            cout << " nineteen";
            break;
        case 8:
            cout << " eighteen";
            break;
        case 7:
            cout << " seventeen";
            break;
        case 6:
            cout << " sixteen";
            break;
        case 5:
            cout << " fifteen";
            break;
        case 4:
            cout << " fourteen";
            break;
        case 3:
            cout << " thirteen";
            break;
        case 2:
            cout << " twelve";
            break;
        case 1:
            cout << " eleven";
            break;
        default :
            break;
    
        }
    }
    void printThouTeens ( int ones)
    {
        printTeens(ones);
    }
    int main()
    {
    cout << "Please enter a number less than two billion and more then negative two billion" << "\n";
    cin >> numpn; // input
    
    ifNegative (); // changing to absolute value of input
    
    
    int ones = numpn2 % 10 / 1;
    int tens = numpn2 % 100 / 10;
    int hund = numpn2 % 1000 / 100;
    int thou = numpn2 % 10000 / 1000;
    int ttho = numpn2 % 100000 / 10000;
    int htho = numpn2 % 1000000 / 100000;
    int mill = numpn2 % 10000000 / 1000000;// separating the input into addressable digits with names
    
    //cout << ones << "\n";
    //cout << tens << "\n";
    //cout << hund << "\n";
    //cout << thou << "\n";
    //cout << ttho << "\n";
    //cout << htho << "\n";
    //cout << mill << "\n";// showing that I have done that
    
    if (numpn == 0)
        {
            cout << "Zero";// starting by changing a 0 input to Zero and stopping (numpn is still around)This works
    
        }
    if ( mill != 0 )
        {
        printMill ( mill);
        cout << " million,";
    
        }
    if ( htho != 0)
        {
    
        printHtho ( htho);
        cout << " hundred";
        }
    if (ttho != 0 && ttho != 1)
    {
        int tens = ttho;
        printTwenty(tens);
    }
    
    if (thou != 0 && ttho != 1)
    {
        int ones = thou;
        printOnes(ones);
        cout << " thousand,";
    }
    if (ttho == 1)
    {
        int ones = thou;
        printTeens ( ones );
        cout << " thousand,";
    }
    
    if (hund != 0)
    {
        printHund (hund);
    }
    if (tens != 0 && tens != 1)
    {
    
        printTwenty( tens);
    }
    
    if (tens == 1 )
    {
    
        printTeens ( ones);
    }
    
    if (ones != 0 && tens != 1)
    {
        printOnes(ones);
    }
    }

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by tsdad View Post
    Second, antred, I have removed the const attribute. Question, when would it be advantageous to use the const?
    When you pass a reference, const is useful to prevent the function to modify the original variable (1). For member functions, it is useful in enforcing that the function does not change the state of the object it is part of (2).
    Code:
    // (2)
    struct foo2
    {
    	// Prevent unintended modification of bar and perhaps enable some compiler optimizations
    	void foo() const
    	{
    		bar = 10; // Compile error: bar is const.
    	}
    
    	void foo3()
    	{
    		bar = 10; // OK
    	}
    
    	int bar;
    };
    
    // (1)
    void foo(const int& bar) // Prevent unintended modification of bar and perhaps enable some compiler optimizations
    {
    	bar = 5; // Compile error: bar is const.
    }
    
    int main()
    {
    	int x;
    	foo(x);
    	foo2 myfoo;
    	const foo2& rfoo = myfoo;
    	rfoo.foo3(); // Compile error: foo2.foo3 is not const. Compiler cannot guarantee object's state does not change.
    	rfoo.foo(); // Ok. foo2.foo is const.
    }
    Third, I have put the final project below. Would I still not need the global variables?
    You still don't need them. I suggest you read a tutorial on functions and how to pass and return values from them. There is one on the site.
    Last edited by Elysia; 05-22-2013 at 11:29 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    15
    Thanks for the direction. Will get back to you,
    Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem: passing function as argument !!!
    By tharnier in forum C++ Programming
    Replies: 29
    Last Post: 07-16-2009, 03:25 PM
  2. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  3. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  4. Passing function as an argument.
    By hyaku_ in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 03:43 AM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 05:12 PM

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