Please correct Program

When is Wrong in my program please cerrect it
.

#include <iostream>
#include <string.h>
using namespace std;

void ten(int n);


main()
{
cout<<"Only 0 to 99999";

char num;


cout<<"Enter Number :";
cin>>num;
ten(num);


}
void ten(int n)
{
string c[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};

cout<<c[n];
}
What is your program supposed to do?

It appears it will work for numbers from 0 to 9. Any number > 9 will cause an out of bounds subscript referencing the array c.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string.h>

void ten(int n)

int main()
{
char num;

cout <<  "Please enter a number, Only 0 to 9." << endl; 

cin >> num;
ten(num);
return 0;
}
void ten(int n)
{
string c[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
cout << c[n];
}
This code works, donĀ“t forget to comment your code in the future.

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

using namespace std;

// Function: dataType nameOfFunction() {}

//Function declarations
void ten(int num);


int main()
{

	//Initiating variable num
	int num;

	cout <<  "Please enter a number, Only 0 to 9." << endl; 

	//Enter a value into variable num
	cin >> num;

	//Calling function ten() with parameter num
	ten(num);

	return 0;
}

// Function Definitions
void ten(int num)
{
	//Declaring array number with type string
	const string number[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
	cout << number[num] << endl;
}
Topic archived. No new replies allowed.