making this a function

any suggestions as to how i move this around and make this into a function???

I need help!!!




#include<cmath>
#include <iostream>
using std::cout;
using std::cin;

char alphabet(int i);

int main()
{
char a=97;
int j=0;
int i=0;
cout <<"Enter an integer and return the corresponding letter of the alphabet: ";
cin >> i;
while (j<i){
cout <<" The corresponding letter is: " << char (j+a)<< "\n";
j++;
}

return 0;
}
opie wrote:
any suggestions as to how i move this around and make this into a function???

Sure. Read this carefully-> http://cplusplus.com/doc/tutorial/functions/
This is what I want to do. something like this. I know the above works but when I try to put in in the form of a function I can't get it to work.

This is what I get, and it needs some help. any suggestions!!. I need so add something to my last cout line but adding in number doesn't help it only brings back what i put in.

I want it to return my char (j+a) but don't know how to make it do that.

thats where I need help.

what do I need to add and to where???




#include<cmath>
#include <iostream>
using std::cout;
using std::cin;

char alphabet(int);

int main()
{


int number;
cout <<"Enter a number and get its corresponding alphabet letter: ";
cin >> number;
cout <<"\nThe corresponding letter is: " << << "\n";


return 0;
}

char alphabet(int)
{
char a=97;
int j=0;
int i=0;
while (j<i){
return char (j+a);
j++;
}
}
I can not try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
char alfabeth(int i){
     int j;
     char *t;
    for(j=1;j=i;j++){
    for(t[j]='a';t[j]='z';t[j]++)
    delete t;}
    return *t;
     }
int main(){int a;
     cout<<"Enter a number and get its corresponding alphabet letter:"<<endl;
     cin>>a;
     cout<<"The corresponding letter is:"<< alfabeth(a)<<endl;
     system("PAUSE");
     return 0;}
1. Where exactly you call your function?
Maybe call can be inserted to cout between empty << <<'s?
2. How would you call your function?
Yes like this:
alphabet(number);
But oh... Wait...
3. What your function takes on a parameter?
yes... Your prototype tells to take integer, but can you set any variable as follows:
int = 80085;?
So... How about is time to read more about functions and after that you should know that you need actually name for that int you are passing :)
4. Oh about your while loop inside your "function"...
First time it get runned j is 0 and i is 0... Now program makes a check if j is lesser than i which it is not (them are equal) -> while loop never gets executed

p.s. Sorry for grammar
I tried this.And this is running:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
char alfabe(int i){
     return (char)(i+96);
     }
int main()
{int a;
    cin>>a;
    cout<<alfabe(a)<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.