Help!

can you guys tell me whats wrong with my program?
example outpust should be
"Type a number followed by its multiple: 25 5
Multiples of 5 from 1 to 25: 5 10 15 20 25"

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
  #include <iostream>
using namespace std;

void askValues(int num,int mul);
void dispMulitples(int num,int mul);

int main(){
	
	int num,mul;

	askValues(num,mul); //ask for the number and multiple value
	dispMulitples(num,mul); //displays the multiples(mul) of num

return 0;
}

void askValues(int num,int mul){
	cout << "Type a number and multiple value: ";
	cin >> num;
	cin >> mul;
}

void dispMulitples(int num,int mul){
	cout << "Multiples of " << mul << " from 1 to " << num << " ";
		for(int i = 1; i <= mul; i++){
		cout << i << endl;
	}
}

Line 17: pass your parameters by reference so that when you read into them, the values are still available to you when you return to main. Make sure you change the forward declaration on line 4 also.
void askValues(int& num, int& mul)

Lines 25-26: Use the stepping interval of the 'for' loop to print your multiples. Start at your multiple number, then increment by that number each time through the loop:
1
2
3
4
for(int i = mul; i <= num; i += mul)
{
    std::cout << i << std::endl;
}
well the code is alright.
you declared a function , defined it and then called it to the main.

but you passed a variable to the function which will only make a copy of it and be destroyed when line 12 executes.

so the variable you passed in line 12 was still a garbage value, because askValues function didnt modify the num and mul variable.

you may create a function pass by reference which will modify the num and mul
variables.

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
  #include <iostream>
using namespace std;

void askValues(int& num,int& mul);
void dispMulitples(int num,int mul);

int main(){
	
	int num,mul;

	askValues(num,mul); //ask for the number and multiple value
	dispMulitples(num,mul); //displays the multiples(mul) of num

return 0;
}

void askValues(int& num,int& mul){
	cout << "Type a number and multiple value: ";
	cin >> num;
	cin >> mul;
}

void dispMulitples(int num,int mul){
	cout << "Multiples of " << mul << " from 1 to " << num << " ";
		for(int i = 1; i <= mul; i++){
		cout << i << endl;
	}
}

ok thanks for your answers!
Topic archived. No new replies allowed.