#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


digits()

{
If(num==1)
cout<< "one";
}



int main(int nNumberofArgs, char* pszArgs[])
{
    int num;


    cout << "Enter a number 1 to 9:";
    cin >> num;
    digits();

    system("pause");
    return 0;
}

I just want to make a simple function that when asked for a number 1-9
it returns and outputs the spelling of that number.

ie. you input the number 1 and cout returns "one"

very very basic no error checking or anything indepth. I just cant seem to figure how to name the function and how to hand off an integer and return the spelling.

I've seen examples of math and handing off equations, but i'm slow in the department of comprehension when it comes to coding and making minor tweaks that change the program just errors out. thanks in advance

Since your numbers are integers, you need an integer parameter for your function. Have it return a std::string.

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


string digits()

{
string num="one";
return num;
}



int main(int nNumberofArgs, char* pszArgs[])
{
    string num;


    cout << "Enter a number 1 to 9:";
    cin >>num;
    cout << digits();


    system("pause");
    return 0;
    }

is this what you mean?

Getting closer. Line 8 is better, but you still need an integer parameter. I would revert line 19 back to being an int so you can pass it into your function.

A function definition looks like:

[I]Returnvalue[/I] functioname([I]datatyp[/I]e parameter1,[I]datatype[/I] parameter2, etc) { }
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


string digits()

{
string num="one";
return num;
}




int main(int nNumberofArgs, char* pszArgs[])
{
    int n;
    int i;

    cout << "Enter a number 1 to 9: ";


for(i=0;i<10;i++)

        {

    cin >> n;
    cout << digits();

        }

    system("pause");
    return 0;
    }

alright so for the string digits() what goes between the characters is (int) since its receiving an integer? when i place int there i run into the problem where it either error you get set a int to a character or too few arguments and any of my ideas to make an if else statment in my funtion to compare the int to

if(num==1)
return num;
else
cout << "invalid number";

sorry this may help this is my original question but i need to look at it one scope at a time because i get to ahead of myself and then make a mess of the program.

6. Write a function named "digits" that takes an integer argument in the range from 1 to 9 , inclusive, and prints the English name for that integer on the computer screen. No newline character should be sent to the screen following the digit name. The function should not return a value. The cursor should remain on the same line as the name that has been printed. If the argument is not in the required range, then the function should print "digit error" without the quotation marks but followed by the newline character. Thus, for example, the statement digit_name(7); should print seven on the screen; the statement digit_name(0); should print digit error on the screen and place the cursor at the beginning of the next line.

So per the assignment, your function should return nothing and therefore be void. You are still not getting that you need a parameter for your function. Look for an example in your text that takes an int. Where is the int in your function definition? Forget about the contents of the function for now, just set up the skeleton of it.

You've been told twice that your digits() needs a parameter. jonsca even provided you with the format of a function. Now, go back to your book, look at the section on functions and read it. Look at the examples. Then try to make digits() look like those examples.

If it still doesn't work, post a very detailed explanation of what you tried and what happened. Your explanation:

when i place int there i run into the problem where it either error you get set a int to a character or too few arguments and any of my ideas to make an if else statment in my funtion to compare the int to

is not helpful to us.

Also, note that your instructions say:

The function should not return a value

my text is c++ for dummies 6th edition its independant study so no teacher for help do work submit to get grade as per the function i noticed after posting whole question that it should not return anything so what im looking to do now is

input a int in int main, send it to the function the function will now have to take that number compare it using a switch case ie case '1': {} case '2' {} then function will output string num1=one; string num2=two;

as for the function i was putting string digits(int) but took it out. Since is not suppose to return anything would it be void digits(void) seeing as it will send the cout itself?

// FunctionDemo - demonstrate the use of functions
//                by breaking the inner loop of the
//                NestedDemo program off into its own
//                function
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

// displayExplanation - prompt the user as to the rules
//                      of the game
void displayExplanation(void)
{
    cout << "This program sums multiple series\n"
         << "of numbers. Terminate each sequence\n"
         << "by entering a negative number.\n"
         << "Terminate the series by entering an\n"
         << "empty sequence.\n"
         << endl;
    return;
}

// sumSequence - add a sequence of numbers entered from
//               the keyboard until the user enters a
//               negative number.
//               return - the summation of numbers entered
int sumSequence(void)
{
    // loop forever
    int accumulator = 0;
    for(;;)
    {
        // fetch another number
        int nValue = 0;
        cout << "Enter next number: ";
        cin  >> nValue;

        // if it's negative...
        if (nValue < 0)
        {
            // ...then exit from the loop
            break;
        }

        // ...otherwise add the number to the
        // accumulator
        accumulator += nValue;
    }

    // return the accumulated value
    return accumulator;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    // display prompt to the user
    displayExplanation();

    // accumulate sequences of numbers...
    for(;;)
    {
        // sum a sequence of numbers entered from
        // the keyboard
        cout << "Enter next sequence" << endl;
        int accumulatedValue = sumSequence();

        // terminate the loop if sumSequence() returns
        // a zero
        if (accumulatedValue == 0)
        {
            break;
        }

        // now output the accumulated result
        cout << "The total is "
             << accumulatedValue
             << "\n"
             << endl;
    }


    cout << "Thank you" << endl;
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    system("PAUSE");
    return 0;
}

thats the example my book has only one I understand what hes saying i just forgot to put int back into the() because it was throwing extra errors

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


void digits(int)

{
int n

cout << "You entered the number" << n;
return;
}



int main(int nNumberofArgs, char* pszArgs[])
{
    int n;

    cout << "Enter a number 1 to 9: ";
    cin >> n;
    digits(int n);



    system("pause");
    return 0;
    }

I cant even get this to work right and thats what I want to use as my "shell"

Edited 5 Years Ago by alonewolf23: n/a

void digits(int)
{
int n

cout << "You entered the number" << n;
return;
}

So what is your parameter variable? All I see is a parameter type, not a variable.

And since your examples don't use a parameter, keep looking at more examples -- preferably one that has a parameter.

Edited 5 Years Ago by WaltP: n/a

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


void digits(int n)
{
    cout << "You entered the number" << n;
    return;
}



int main(int nNumberofArgs, char* pszArgs[])

{
    int n;

    cout << "Enter a number 1 to 9: ";
    cin >> n;
    void digits(int n);



    system("pause");
    return 0;
}

How do i send the "cin n;" to my function

void digits(int n); is not a function call. It's a definition. Get rid of ALL the types an just use digits(n); , as all your examples should have shown...

I went a diffrent route

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


void digits(void)
{
    int i;
	int nvalue;
	cout << "Enter a Number 1 thru 9: ";


    for(i=0; i<10; i++)
    {

    cin >> nvalue;

	switch(nvalue)
        {
            case 1:
            cout << "one";
            break;

            case 2:
            cout << "two";
            break;

            case 3:
            cout << "three";
            break;

            case 4:
            cout << "four";
            break;

            case 5:
            cout << "five";
            break;

            case 6:
            cout << "six";
            break;

            case 7:
            cout << "seven";
            break;

            case 8:
            cout << "eight";
            break;

            case 9:
            cout << "nine";
            break;

            default:

            cout << "digit error\n";
        }
    }
    return;
}



int main(int nNumberofArgs, char* pszArgs[])

{
    digits();

    system("pause");
    return 0;
}

but thanks you and I do see the below works. thought it says nvalue uninialized in main(), but i'm not using it so I understand.

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;


void digits(int nvalue)

{
    int i;

    for(i=0; i<10; i++)
    {

    cin >> nvalue;

	switch(nvalue)
        {
            case 1:
            cout << "one";
            break;

            case 2:
            cout << "two";
            break;

            case 3:
            cout << "three";
            break;

            case 4:
            cout << "four";
            break;

            case 5:
            cout << "five";
            break;

            case 6:
            cout << "six";
            break;

            case 7:
            cout << "seven";
            break;

            case 8:
            cout << "eight";
            break;

            case 9:
            cout << "nine";
            break;

            default:

            cout << "digit error\n";
        }
    }
    return;
}


int main(int nNumberofArgs, char* pszArgs[])

{
    int nvalue;

    cout << "Enter a number 1 to 9: ";
    digits(nvalue);



    system("pause");
    return 0;
}

Did your books teach you how to declare functions?

Look for something on "Prototypes". It SHOULD be in the functions section.

Is it necessary in this particular case? No. But is it a good habit that you should become accustomed to? Absolutely.

Much better. Note my comments below in the code you just posted.

#include <cstdio>  //don't really need this, no functions were used from it
#include <cstdlib> //same here
#include <iostream>

using namespace std;


void digits(int nvalue) //this is correct

{
    int i;

    for(i=0; i<10; i++) //you don't really need the loop unless you want to do it 10 times
    {

    cin >> nvalue; // delete this if you're passing the value in, don't take in another one

	switch(nvalue)
        {
            case 1:
            cout << "one";  //good, the instructions say no endl after
            break;

            case 2:
            cout << "two";
            break;

            case 3:
            cout << "three";
            break;

            case 4:
            cout << "four";
            break;

            case 5:
            cout << "five";
            break;

            case 6:
            cout << "six";
            break;

            case 7:
            cout << "seven";
            break;

            case 8:
            cout << "eight";
            break;

            case 9:
            cout << "nine";
            break;

            default:

            cout << "digit error\n";
        }
    }
    return;  //when void functions reach the end, they return automatically
}


int main(int nNumberofArgs, char* pszArgs[])

{
    int nvalue;

    cout << "Enter a number 1 to 9: "; //put the cin >> nvalue; after this statement and before the next
    digits(nvalue); //otherwise you're passing in garbage here since nvalue is not
                      //initialized



    system("pause");
    return 0;
}

Edited 5 Years Ago by jonsca: n/a

This question has already been answered. Start a new discussion instead.