8.7k views
5 votes
Write a function that takes number between 1 and 7 as a parameter and prints out the corresponding number as a string. For example, if the parameter is 1, your function should print out one. If the parameter is 2, your function should print out two, etc. If the parameter is not between 1 and 7, the function should print an appropriate error message. In your file, you should include a main() that allows the user to enter a number and calls your function to demonstrate that it works.

User Kloffy
by
6.5k points

1 Answer

5 votes

Answer:

The program in C++ is as follows:

#include<iostream>

using namespace std;

void changenum(int num){

string nums[7] = {"One","Two","Three","Four","Five","Six","Seven"};

if(num >7 || num < 1){

cout<<"Out of range";

}

else{

cout<<nums[num-1]; }

}

int main(){

int num;

cout<<"Number: ";

cin>>num;

changenum(num);

return 0;

}

Step-by-step explanation:

The function begins here

void changenum(int num){

This initializesa string of numbers; one to seven

string nums[7] = {"One","Two","Three","Four","Five","Six","Seven"};

If the number is less than 1 or greater than 7, it prints an out of range error

if(num >7 || num < 1){

cout<<"Out of range";

}

If otherwise, the corresponding number is printed

else{

cout<<nums[num-1]; }

}

The main begins here

int main(){

This declares num as integer

int num;

Prompt the user for input

cout<<"Number: ";

Get input from the user

cin>>num;

Pass the input to the function

changenum(num);

return 0;

}

User Fernandosavio
by
6.8k points