keeps sending out a random number

please help
also i cant use global varables

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Nico Kalin OConnell
#include <iostream>
#include <iomanip>
using namespace std;
int validate(int);
int main()
{
	double bags = 15;
	int amount = 0;
	
	cout << "Bags discount Program" << endl;
	
	// call function
	validate(amount);
	
	//if else for calculations.
	if 
	(amount >= 10 && amount <=20)
	    cout << bags * amount * 0.95;
	else if 
	(amount >=21 && amount <=30)
	    cout << bags * amount * 0.90;
	else if 
	(amount >=31)
	    cout << bags * amount * 0.85;
	else
    cout << bags * amount;
	    
	//calculate and display total 
//	cout << setw() << setprecision() << fixed << showpoint;     


	

}


int validate(int amount)
{
    cout << "Please enter the amount of bags you would like to purchase" << endl;
    cin >> amount;
    while (amount <= 2)
    {
        cout << "invalid entry, please enter a number greater than 2" << endl;
        cin >> amount;
    }    
    return amount;
}
Last edited on
Line 9: amount is an uninitialized variable. It contains random garbage.
Line 13: You have a comment about calling your function, but you never actually call it.
Line 18-27: You proceed to use your uninitialized variable in your calculations.

follow main.
amount .. not initialized, has random value.
if(things about amount, which is random)
cout things about amount * something, which is still random, just shifted.

validate might help but you don't actually call it.
but it makes no sense, the way its written, amount should not be passed in.

consider:
main:
int amount = validate();
1
2
3
4
5
6
7
8
9
10
11
int validate()
{
    cout << "Please enter the amount of bags you would like to purchase" << endl;
    cin >> amount;
    while (amount <= 2)
    {
        cout << "invalid entry, please enter a number greater than 2" << endl;
        cin >> amount;
    }    
    return amount;
}
if I do that then it just sends out 0
if I do that then it just sends out 0

If you are going to return a value from a function you should actually use that returned value:
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
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

int validate();

int main()
{
   double bag_cost { 15.0 };

   std::cout << "Bags discount Program\n";

   int amount { validate() };

   if (amount >= 10 && amount <= 20)
   {
      std::cout << bag_cost * amount * 0.95;
   }
   else if (amount >= 21 && amount <= 30)
   {
      std::cout << bag_cost * amount * 0.90;
   }
   else if (amount >= 31)
   {
      std::cout << bag_cost * amount * 0.85;
   }
   else
   {
      std::cout << bag_cost * amount;
   }
   std::cout << '\n';
}

int validate()
{
   std::cout << "Please enter the amount of bags you would like to purchase: ";
   int amount;
   std::cin >> amount;

   while (amount <= 2)
   {
      std::cout << "invalid entry, please enter a number greater than 2\n";
      std::cin >> amount;
   }

   return amount;
}

You can use a reference as a parameter to the function so the value can be passed into and out of the function. Any changes made to the variable in the function show up in main:
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
35
36
37
38
39
40
41
42
43
#include <iostream>

void validate(int&);

int main()
{
   double bag_cost { 15.0 };
   int amount      { };

   std::cout << "Bags discount Program\n";

   validate(amount);

   if (amount >= 10 && amount <= 20)
   {
      std::cout << bag_cost * amount * 0.95;
   }
   else if (amount >= 21 && amount <= 30)
   {
      std::cout << bag_cost * amount * 0.90;
   }
   else if (amount >= 31)
   {
      std::cout << bag_cost * amount * 0.85;
   }
   else
   {
      std::cout << bag_cost * amount;
   }
   std::cout << '\n';
}

void validate(int& amount)
{
   std::cout << "Please enter the amount of bags you would like to purchase: ";
   std::cin >> amount;

   while (amount <= 2)
   {
      std::cout << "invalid entry, please enter a number greater than 2\n";
      std::cin >> amount;
   }
}

I havent learned about the second part (using references) but I didnt really think of setting a variable to a function itself, super helpful and informative thanks so much
There are three ways to pass a variable into a function, two inherited from C.

1. Pass by value: void swap(int x, int y);

The parameter(s)' values are copied, any changes made in the function are not reflected in the caller.

2. Pass by pointer: void swap(int* x, int* y);

Any changes make in the function to the passed parameters are reflected in the caller.

3. Pass by reference: void swap(int& x, int& y);

Same as 2.

1 & 2 is how C passes variables into functions, C++ added reference passing.

Passing by pointer looks a bit messy IMO, and can be a source of bugs because of the syntax:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

void swap_val(int, int);
void swap_ptr(int*, int*);
void swap_ref(int&, int&);

int main()
{
   int x { 5 };
   int y { 205 };

   std::cout << "Main: before swap x: " << x << ", y: " << y << "\n\n";

   swap_val(x, y);
   std::cout << "Main: after swap_val x: " << x << ", y: " << y << "\n\n";

   swap_ptr(&x, &y);
   std::cout << "After swap_ptr x: " << x << ", y: " << y << "\n\n";

   swap_ref(x, y);
   std::cout << "After swap_ref x: " << x << ", y: " << y << '\n';
}

void swap_val(int x, int y)
{
   std::cout << "\tSwap_val. Before swap, x: " << x << "  y: " << y << '\n';

   int temp { x };

   x = y;
   y = temp;

   std::cout << "\tSwap_val. After swap,  x: " << x << " y: " << y << "\n\n";
}

void swap_ptr(int* px, int* py)
{
   std::cout << "\tSwap_ptr. Before swap, *px: " << *px << ",  *py: " << *py << '\n';

   int temp { *px };

   *px = *py;
   *py = temp;

   std::cout << "\tSwap_ptr. After swap, *px: " << *px << ",  *py: " << *py << "\n\n";
}

void swap_ref(int& rx, int& ry)
{
   int temp { rx };

   std::cout << "\tSwap_ref. Before swap, rx: " << rx << ",  ry: " << ry << '\n';

   rx = ry;
   ry = temp;

   std::cout << "\tSwap_ref. After swap,  rx: " << rx << ", ry: " << ry << "\n\n";
}
Main: before swap x: 5, y: 205

        Swap_val. Before swap, x: 5  y: 205
        Swap_val. After swap,  x: 205 y: 5

Main: after swap_val x: 5, y: 205

        Swap_ptr. Before swap, *px: 5,  *py: 205
        Swap_ptr. After swap, *px: 205,  *py: 5

After swap_ptr x: 205, y: 5

        Swap_ref. Before swap, rx: 205,  ry: 5
        Swap_ref. After swap,  rx: 5, ry: 205

After swap_ref x: 5, y: 205
Topic archived. No new replies allowed.