Need help with separating numbers using reference

So heres what I have to do
Write a function called breakThree that will accept a 3 digit integer and returns each of the numbers individually. This function will take four paramaters. The first parameter is the three digit number to break apart. Parameters 2 through 4 are passed by reference and will be used to return each of the numbers back to main.
You should make sure that the input into the function is a 3-digit number. If it is not a three digit number the breakThree function should simply return false. If it is a three digit number the breakThree function should break the number apart, and store each of the numbers in the parameters passed by reference.
In main you should get the number from input and then output each of the numbers on a separate line.

What not to use
global variables
cin in breakThree function
cout in breakThree function
goto statements



yes this is homework no i don't want you to do it for me. heres what I have so far please help the tutors at my school are unavailable on fridays and the assignment is due sunday
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// main.cpp
// assignment 13
//

#include <iostream>
using namespace std;

void separate(int a, int b, int c, int d);
int main(int argc, const char * argv[])
{
int num;
int a;
int b;
int c;
cout << "enter a three digit number" << endl;

cin >> num;
separate(a,b,c);





return 0;
}

void separate(int &a, int &b, int &c, int &d)
{
d = a % 10;
c = (a / 10) % 10;
b = a / 100;

return;
}

Last edited on
Problem #1:
1
2
void separate(int a, int b, int c, int d); // <- your prototype
void separate(int &a, int &b, int &c, int &d) // <- your definition 


Notice that the signatures do not match. Your prototype is taking parameters by value, but the definition is taking them by reference. These must match exactly.


Problem #2:
1
2
3
// your separate function takes 4 parameters
cin >> num;
separate(a,b,c);   // <- you are only giving it 3 



Problem #3:
Your function is named incorrectly
Your Assignment wrote:

Write a function called breakThree that will accept a 3 digit integer and returns each of the numbers individually...



Problem #4:
Your function is returning void when it shouldn't be. The assignment says:
Your Assignment wrote:
If it is not a three digit number the breakThree function should simply return false


"return false" suggests the function should be returning a bool. Returning true if the number was successfully separated, and returning false if the input was invalid.


Problem #5:
You're not doing this part of the assignment:
Your Assignment wrote:

You should make sure that the input into the function is a 3-digit number. If it is not a three digit number the breakThree function should simply return false.


...Though this can be added later. Fix the other problems first.
Last edited on
Your function seperate takes four arguments (int &a, int &b, int &c, int &d) but you are only passing three separate(a,b,c);.
You need to add another variable (d)
Also, there's no need to pass "a" by reference.
And finally when asking for help you should mention what exactly isn't working ie I'm guessing you got an error when trying to compile.
it is saying that there is no function for breakThree
this is what I have right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
//  main.cpp
//  assignment 13
//

#include <iostream>

using namespace std;

void breakThree(int &num, int &a, int &b, int &c, bool &morethanthree = false);

int main()
{
    int x, y, z;
    breakThree(x, y, z)
    cout << x << endl;
    cout << y << endl;
    cout << z << end;
    
    
    return 0;
    
}

void breakThree(int &num, int &a, int &b, int &c, bool &morethanthree)
{
    cout << "enter a three digit integer" << endl;
    cin >> num;
    

    return;
}
Last edited on
void breakThree(int &num, int &a, int &b, int &c, bool &morethanthree = false);

Since 'morethanthree' is being passed by reference (that's what the & symbol means)... you can't give it a default value of 'false', since you cannot have a non-const reference to a literal value.

The assignment says to RETURN true or false depending on whether or not it was successful. Returning does not mean "pass as parameter". Returning means returning:

1
2
3
4
int example()
{
    return 5;
}


In this example function... it returns an int (that's what the type before the 'example' is -- it's the return type). And when it has a return statement... it has a value to return (here, it's returning 5).


Your current 'breakThree' function returns void (nothing). This is not correct. You want it to return a bool. return true if the function was successful, and return false if it wasn't.




1
2
//line 15
breakThree(x, y, z)


1) You're missing a semicolon

2) breakThree (assuming you get rid of the 'morethanthree' parameter) takes FOUR parameters. You are only giving it 3. You need to give it 4.
1 for the number to separate
and 3 more for the separated digits.

Note that since the 3 separated digits are the only ones that are output... the 1st parameter (num) does not need to be passed by reference (you don't need the & symbol).


1
2
3
4
void breakThree(int &num, int &a, int &b, int &c, bool &morethanthree)
{
    cout << "enter a three digit integer" << endl;
    cin >> num;


breakThree should not ask the user for anything. The number to separate is passed in as a parameter. Just work with that number.

main should be the one asking the user for the input. Once it gets that input, it should pass it to breakThree as a parameter.
Last edited on
Topic archived. No new replies allowed.