what is wrong with this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int convert(int number);

int main(int number)
{
    convert(number);
    cout << "the number is "<< number;
}

int convert(int number = 5)
{


    number += 5;
    return number;
}
What are you looking to do here? and why are you not declaring "int number = 5" in the body of the function?
If you're trying to modify the number, you either need a pointer or a reference.
im just trying to figure out how to pass a variable to and from a function (where the value is changed from the function) and passed to the calling function
shouldn't this take number from main and pass it to convert function (which adds 5 to it) then goes back to main and displays 10?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int convert(int number);

int main()
{
    int number = 5;
    convert(number);
    cout << "the number is "<< number;
}

int convert(int number)
{
    number += 5;
    return number;
}


Last edited on
#1 main is not returning a value
#2 Convert returns a value assign it to number use it Number = Convert (Number);
Hope this helps :)
Last edited on
Is this the correct way???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int convert(int number);

int main()
{
    int number = 100;
    cout << "the number is " << convert(number) <<endl;
    cout << "the number is "<< number <<endl;
}

int convert(int number)
{
    number += 15;
    return number;
}
Yes and No if you want to display the number then yes but if you want to assign the value then:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int Converter (int Number);
int main ()
{
	int Number = 0;
	Number = Converter (Number);
	cout << Number << endl;
	cout << "Press ENTER to end the program." << endl;
	cin.get ();
	return 0;
}
int Converter (int Number)
{
	return Number += 5;
}
Pass a reference instead of by value.
I believe you'll want to display your original number before you convert it, as well as have the program sleep for a few seconds so that it doesn't close as soon as it's finished.
Last edited on
I know it was said before but you either seemed to not notice it or else you had ignored it.

int main() will be expecting an integer to be returned so you have to make sure to put in return 0; at the end of the main function (or something similar).
This is the simplest as it can be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int convert(int number);

int main(int number){
    
    

    
    cout << "the number is  "<< convert(number);
    system("PAUSE");
    return 0;
}

int convert(int number = 5)
{


    number += 5;
    return number;
}
Topic archived. No new replies allowed.