Printing name in reverse using pointers.

There is alien code. What's wroooooong? Thanks in advance.

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
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std; 

char printReverse(char*);

main(){
       
       char name[5] = {'C', 'E', 'S', 'A', 'R'};
       cout<<"\n\n\t\tYour name in reverse: "; for(int i=0; i<1; i++){printReverse(name);}cout << endl;
       
       getch();
       }
       
       
char printReverse(char* name){

     for(int i=5; i>=0; i--){
             cout<<name[i];

              }
     
     }
1. dont use iostream.h use iostream
2. main() should be int main
3. you've told the compiler printReverse returns a char, but in it's current state it returns bugger all. (just make it void)
4. for (int i = 5; i >= 0; i--){

do NOT start at 5. Start at 4.

5. what's the point of the for loop in your main()?? just do this:
1
2
3
4
char name[5] = { 'C', 'E', 'S', 'A', 'R' };
	cout << "\n\n\t\tYour name in reverse: "; 
	printReverse(name);
	cout << endl;
Last edited on
I'm just trying to figure things out earlier lol. Btw, thanks.
Well it depends by how you look at it. There might be a lot of things wrong, there might be a few. In my opinion there is a lot.

-<iostream> instead of <iostream.h>
-3 of the library headers are not used or outdated
-namespace std should not be practiced anymore (namespaces in general are bad practice due to conflicts)
-main function missing return definition
-printReverse() has a useless return definition
-getch() belongs to conio.h which is outdated, use std::cin.get() instead
-the for loop inside the main function is producing an incorrect solution
-since your printReverse() function accepts a pointer, it is not wise to predefine its size in the for loop. use strlen from the <string.h> library or (sizeof(array)/sizeof(array[0]) method.
-remember that if a function can be defined before the main function, you will not need a "prototype" for the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void printReverse(char* name)
{

    for(int i = (sizeof(name) / sizeof(name[0])); i >= 0; i--)
    {
        std::cout << name[i];
    }
}

int main()
{

    char name[5] = {'C', 'E', 'S', 'A', 'R'};
    std::cout << "\n\n\t\tYour name in reverse: ";

    printReverse(name);

    std::cout << std::endl;

    std::cin.get();
    return 0;
}
Idontownaplanet wrote:
-namespace std should not be practiced anymore (namespaces in general are bad practice due to conflicts)

Presumably you are referring to the using directive.


Idontownaplanet wrote:
-main function missing return definition

Presumably you are referring to the missing return type.


Idontownaplanet wrote:
-getch() belongs to conio.h which is outdated, use std::cin.get() instead

One is not really a replacement for the other. Prefer, instead, to set up your environment so you don't need to use some hack to keep the console window open.


Idontownaplanet wrote:
-since your printReverse() function accepts a pointer, it is not wise to predefine its size in the for loop. use strlen from the <string.h> library or (sizeof(array)/sizeof(array[0]) method.

That second suggestion is wrong. A pointer is not an array. sizeof applied to a pointer will give you the size of the pointer, not the size of any array it may point to. The code you supplied is definitely not correct.

Given the definition of the array, which is not a null-terminated string, the suggestion to use strlen would also be wrong.

Last edited on
Topic archived. No new replies allowed.