Hi, I have made a program that takes name as input(char)and then reverses the letters and displays it. It works completely fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main(){
char name[10];
char ch;
int i=0;
cout<<"Please enter the name: ";
cin>>ch;
while(ch!='.'){
name[i]=ch;
cin>>ch;
i++;
}
for(;i>=0;i--){
cout<<name[i]<<" ";
}
return 0;
}
Now I am trying to turn this code into a function and use it in my program, I have managed to somehow use it but i always get one garbage value in output, if you can tell me where I am wrong, I would be grateful.
Why are you inputting the name one character at a time?
Second program, line 19: What's the point of passing chir as an argument?
You should be inputting the entire name, then calling the reverse function which reverses the order of the bytes in the array, then display it (in main) as a single entity rather than displaying a character at a time.
You have no protection in your loop that you don't overflow name. What happens if the user enters more than 10 characters?
i am inputting one character at a time, it was in my assignment so I have to do it that way, plus my teacher didn't ask me to use protection, so it is good as it is.
And if I don't enter chir as an argument it gives error, this one: [Error] too few arguments to function 'void reverse_func(char*, char&)'