Default values

Hi, I was wondering how do I go about displaying default values, in the case of the code shown below.

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
  #include <iostream>

struct Candybar
{
	char name [50];
	float weight;
	int calories;
};

void candyshow (...);
void candyfill (Candybar & candy, const char * name1 = "Millenium Munch", float weight1 = 2.85, int calories1 = 350);

int main()
{
	Candybar x;
	candyfill(x);
	candyshow(x);
	return 0;
}

void candyshow (Candybar & candy)
{
	some codes here
}
void candyfill (Candybar & candy, const char * name1,  float weight1, int calories1)
{
	std::cout << "Enter the name of the candy: ";
	std::cin.getline(candy.name, 50);
	std::cout << "Enter the weight of the candy: ";
	std::cin >> candy.weight;
	//std::cin.get();
	std::cout << "Enter the calories of the candy: ";
	std::cin >> candy.calories;
	//std::cin.get(); 


I know the code works fine, but when I want it to show the default values (by pressing enter only), the display just shows a blank. Is my understanding of default values wrong?

When I include "candyfill()", the complier says this:

18:12: error: too few arguments to function 'void candyfill(Candybar&, const char*, float, int)'
11:6: note: declared here

Am I doing it wrong?
Last edited on
You don't provide a default value for the first parameter so you must always call the function with at least that one argument.

Also I really don't understand why you even have the rest of the parameters since you never use them.



Thanks! I think I misunderstood the question.

Yea, you're right! :)
Topic archived. No new replies allowed.