whats wrong here?

im practicing a code i made up ( id o realize it does not make sense but the purpose of it is to see if the void type and functions work. now everything is working but the void function does not execute whats wrong here. please help

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 #include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <string>

 using namespace std;

 int buyshoes(int price)
 {
	 string name, email;
	 long phone;
	cout <<  "\n\nPlease enter your name : ";
	cin >> name;
	cout << "\nEnter your phone number : ";
	cin >> phone;
	cout << "\nEnter email : ";
	cin >> email;
	cout << "\nyour shoes cost : " << price;
		return price;
 }
 int discount1(int discount)
 {
	 int total;
	 total = discount/2;
	 cout << "\nyou get a discount\n";
	 
		 return total;
 }
  void totalprice (int total);
int main()
{
	int shoesize, menu, price,x,y, discount2;
	string purchase, stop;
		cout << endl;
		cout << "Welcome to shoe store.\n\n\n";
	do
		{

	
		cout << "press [1] to buy shoes\n";
		cout << "press [2] to buy a watch\n";
		cout << "press [3] to exit";
		cout << "\noption : ";
		cin >> menu;
		cout << endl;
		cout << endl;
		
		switch (menu)
		{
			case 1:
				price = 14;
				discount2 = 14;
				cout << "\nWhat is your shoe size? : ";
				cin >> shoesize;
				if  (shoesize > 7)
				{
					
					buyshoes(price);
				}
				else 
				{
					buyshoes(price);
				discount1(discount2);
				totalprice (discount2);
				}
				break;

			case 2:

				cout << "\nWould you like to purchase a watch? : ";
					cin >> purchase;
				if (purchase == "y" || purchase == "Y")

					cout << "\nLet's get you a new watch";

				else
					cout << " \npurchase a bundle pack (shoes and watch)";
				break;

			case 3 :
				
				cout << "\nexit? : ";
					cin >> stop;
					break;
		
		}
		
}while(stop != "y" && stop != "Y");

return 0;
}

		void totalprice (int total)

		{
			int complete;
			complete = total*10;
		
		}
The first thing that I'll say is that you REALLY need to learn to indent properly. It is an imperative part of a program and a failure to do so will result in a misunderstanding or more difficult understanding of your code when you or someone else tries to re-read it. But a flow-understanding doesn't seem to be your main problem (though I see some strange things there that I would question).


Take a look at void totalprice(int total). I see that you declare a local variable and set it to total*10. What happens after that? We discard the local variable and return to the main function. The function didn't output anything, it just executed a line of code, then discarded any calculation. What did you want to happen here? What were you looking for? If you were expecting something displayed on the console, you need to put a cout, if you were expecting to modify something in main, you need to return a value.
@stewbond
ok your right i forgot to cout but what exactly do you mean i need to learn how to indent properly?
closed account (3qX21hU5)
THe usual indentation is 4 spaces for each scope for example

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
int main()
{
    // Mains scope
    
    if (something)
    {
        // If's scope
    }

    // Back to mains scope

    for (something)
    {
        // First For's scope

        for (something)
        {
            // Second For's Scope
            if (something)
            {
                // If's scope
            }
        }
    }
}


Notice how each of the braces line up to eachother also. Now when we look at your indentation it is going way off to the right and isn't consistent (Meaning some are indented farther or less in the same scope).
ok i see so you say 4 spaces which is pretty much Hitting tab once for each scope indentation. i really appreciate the insight im going to be more organized with my codes from now on. thanks. i actually have another question how would i be able to input a longer number for the phone number because when the program asks for a phone number; if you input more than 7 numbers the program goes haywire i used long thinking it would allow me to do this but it didnt any recommendations?
Last edited on
closed account (3qX21hU5)
Depends on what type you are trying to use to hold the phone number. Each integer(whole number) variable like int can only hold a certain amount.

The amounts vary a little by implementation but the max they can hold is somewhere around these.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    cout << "Max Sizes" << endl;

    cout << "int = " << numeric_limits<int>::max() << endl;
    cout << "unsigned int = " << numeric_limits<unsigned>::max() << endl;
    cout << "double = " << numeric_limits<double>::max() << endl;
}


To hold a phone number I would recommend using the std::string type to hold it. That why you can garuntee that it can hold the phone number and it can also support phone numbers in this format 1-800-654-342.

Here is how you can do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>   // You need this to use the string type

using namespace std;

int main()
{
    string phoneNumber;

    cout << "Enter your phone number: ";
    cin >> phoneNumber;

    cout << "Your phone number is: " << phoneNumber;
}
@Fyah

I would recommend setting up your editor to replace tabs with 4 spaces. The code tags on this site seem to make tabs 8 spaces - which makes it look worse than what's shown in your IDE. Or are you using a basic editor?

Can you do auto formatting / indenting on your IDE / editor - that is no need to press tab at all?
Topic archived. No new replies allowed.