Need help. Function using a switch operator

I have been trying to solve this homework my professor gave me for more than one hour and i still can't find a way for it to work. Been searching a lot but no result sadly. Could you help me on this? It says:
"Write the program which uses the function Day() which uses the Switch() operator for the given numbers from 1 to 7 (no other values allowed) which show the day of the week."
So it is requiring to give a value from the keyboard from 1 to 7 which shows the days Monday to Sunday. You can just write case 1 and go to default to make it faster. Thanks in the first place.
Could you show us what you've actually done? Use code tags for the code, it <> under format.
Last edited on
I have tried like different things i thought they would work but none did, dont have anything and i didnt want to make myself look dumber than i already do lol
Sorry mate, this is your homework not ours. Show us what you've done. We can help you get on the right foot, we aint no homework service. If you want someone to write your homework, post under the job section.
This is in my language. I am so confused right now and not sure what to use in some specific times .
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
#include<iostream>
using namespace std;
int Dita(int d)
{
	cout << "Jepni vleren:"; cin >> d;
	switch (d){
	case 1:
		cout << "E Hene";
		break;
	case 2:
		cout << "E Marte";
		break;
	case 3:
		cout << "E Merkure";
		break;
	case 4:
		cout << "E Enjte";
		break;
	case 5:
		cout << "E Premte";
		break;
	case 6:
		cout << "E Shtune";
		break;
	case 7:
		cout << "E Diel";
		break;
	default:
		cout << "Gabim!"; break;
		return d;

	}
};

int main()
{
	cout << "Dita e javes eshte:"; cin >> Dita;
	
	return 0;
}
Hi,

Line 33, remove the semicolon.

On line 37, there is no need for the cin, input happens inside your function already.

Your function Dita takes one argument (an int) and returns an int

Because everything happens inside your function, there is no need for it to take an argument or return anything, so consider changing the function to:

1
2
3
4
5
6
7
void Dita() 
{
.
.
.

}


Edit: Remove line 30, we are not returning anything any more. Btw having just 1 return in the default: case is bad news, it may never be reached. A function declared to return a value of a certain type, must actually return something in all cases.

and main() becomes:

1
2
3
4
5
6
7
int main()
{
	std::cout << "Dita e javes eshte:\n";  // newline added
        Dita();  // function call with parentheses
	
	return 0;
}


Also, avoid having more than 1 statement per line. I personally find that harder to read, but it is not a strict rule.

I also like main() at the top of the file, so this means putting a forward declaration of your function, and placing the function itself after main(), that way main() is easy to find:

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

// eventually you should avoid doing this - Google it :+)
// using namespace std;  
// Replace cout with std::cout, and cin with std::cin, any other std thing has std:: before it
//sorry if this is too advanced right now :+)

void Dita(); // forward declaration of function

int main()
{
	std::cout << "Dita e javes eshte:\n";  // newline added
        Dita(); // function call with parentheses
	
	return 0;
}

// function definition
void Dita()
{
.
.
.
}


Good Luck !!

Last edited on
Topic archived. No new replies allowed.