0

please help with this program i dont know whats wrong.

#include <stdio.h>
#include "strlib.h"
#include "simpio.h"
#include "genlib.h"
#include "string.h"

void reverse(void);

int main()
{
	printf("Please enter a sentence and end the sentence with a -\n");
	reverse();
	system("Pause");
}

void reverse(void)
{
	int ch;
	
	if(getchar(ch) != EOF)
	{
		reverse();
		putchar(ch);
	}
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Aia
0

>please help with this program i dont know whats wrong.
This is what's wrong with it.

#include "strlib.h"
#include "simpio.h"
#include "genlib.h"
#include "string.h"

None of them are necessary. if(getchar(ch) != EOF) getchar() doesn't accept arguments, and returns an int. i.e. if ((ch = getchar()) != EOF) reverse() doesn't live up to expectation as a recursive function.

if(getchar(ch) != EOF)
	{
		reverse();
		putchar(ch);
	}

It keeps calling itself and then the only character that gets "displayed" by putchar() is EOF, which you can not see. system("Pause"); Bad call there, a simple getchar(); would had sufficed. int main() needs a returned int at exit.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.