Function is not printing

Task:
- Write a function `index` that converts an int from 0 to 5 into its word. (It should take an int and return a string.)
0 -> "zero"
1 -> "one"
2 -> "two"
3 -> "three"
4 -> "four"
5 -> "five"
anything else -> "other"


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;
string index(int x)
{
	if(x=0)
		return "zero ";
	else if(x==1)
		return "one ";
	else if(x==2)
		return "two ";
	else if(x==3)
		return "three ";
	else if(x==4)
		return "four ";
	else if(x==5)
		return "five ";
        else 
               return "other ";
}
int main()
{
	cout<<"Enter a number from 0-5 "<<endl;
	int x;
	cin>>x;
	cout<<index(x);
	
	return 0;
}


It compiles but doesn't print anything. I need help.
Last edited on
One thing to note is that you have a dangling else-if - that is, you don't have a final "else" condition to catch a situation where none of the previous conditions is met. You might want to add something like:

1
2
	else
		return "invalid number ";


Or maybe construct a string which states what the number actually is, for debugging purposes, and return that.
In the first statement of the function variable x is set to zero

if(x=0)


You have to write

if(x == 0)
i forgot to add it. thanks do you know why it doesnt print anything when it runs?
@vlad from moscow
Thank you! Careless mistake on my part.
Topic archived. No new replies allowed.