My Own Input Function Problem

Started by
1 comment, last by dbzprogrammer 18 years, 8 months ago
Ok, so I am creating a program that needs to be saving keyboard input to a character pointer, however the CIN function wont support that, so I coded something myself. But when you type in a character the program crashes.

#include <conio.h>
#include <iostream>
using namespace std;

typedef struct charlink
{
	char data;
	charlink* next;
}charlinkthing;

char* translate(charlinkthing* const);

int main()
{
	char ch;
	char *string=NULL;
	bool done;
	charlinkthing* start=NULL;
	charlinkthing* current=NULL;
	charlinkthing* nextcur=NULL;

	cout << "Welcome to my cin program" << endl;
	do
	{
		ch=getch();
		if(ch==13)
		{
			cout << endl;
			string=translate(start);
		}
		else
		{
			cout << ch;
			if(!start->data)
			{
				current = new charlink;
				current->data = ch;
				start = current;
				start->next = new charlink;
				current = start->next;
			}
			else
			{
				current->data = ch;
				nextcur = new charlink;
				current->next = nextcur;
				current = nextcur;
				nextcur = NULL;
			}
		}
	}while(!done);
	return 1;
}

char* translate(charlinkthing* const startchar)
{
	char *ch;
	return ch;
}
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Advertisement
This is the wierdest thing ever... I simply changed (!start->data) to (!start)...

#include <conio.h>#include <iostream>using namespace std;typedef struct charlink{	char data;	charlink* next;}charlinkthing;char* translate(charlinkthing* const);int main(){	char ch;	char *string=NULL;	bool done;	charlinkthing* start=NULL;	charlinkthing* current=NULL;	charlinkthing* nextcur=NULL;	cout << "Welcome to my cin program" << endl;	do	{		ch=getch();		if(ch==13)		{			cout << endl;			string=translate(start);		}		else		{			cout << ch;			if(!start)			{				current = new charlink;				current->data = ch;				start = current;				start->next = new charlink;				current = start->next;			}			else			{				current->data = ch;				nextcur = new charlink;				current->next = nextcur;				current = nextcur;				nextcur = NULL;			}		}	}while(!done);	return 1;}char* translate(charlinkthing* const startchar){	char *ch;	return ch;}
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
New Problem, everytime I hit enter the program crashes.

#include <conio.h>#include <iostream>using namespace std;typedef struct charlink{	char data;	charlink* next;}charlinkthing;char* translate(charlinkthing* const);char* input();int main(){	char* output;	cout << "Welcome to my cin program" << endl;	output = input();	cout << endl;	cout << output << endl;	return 1;}char* input(){	char ch;	char *string=NULL;	bool done=false;	bool notrue=false;	charlinkthing* start=NULL;	charlinkthing* current=NULL;	charlinkthing* nextcur=NULL;		do	{		ch=getch();		if(ch==13)		{			cout << endl;			current->next = NULL;			string=translate(start);			current=start->next;			delete start;			start=NULL;			do			{				if(current->next=NULL)					notrue=true;				else				{					nextcur=current->next;					delete current;					current = NULL;				}			}while(notrue);			done=true;		}		else		{			cout << ch;			if(!start)			{				current = new charlink;				current->data = ch;				start = current;				start->next = new charlink;				current = start->next;			}			else			{				current->data = ch;				nextcur = new charlink;				current->next = nextcur;				current = nextcur;				nextcur = NULL;			}		}	}while(!done);	return string;}char* translate(charlinkthing* const startchar){	char *ch;	charlinkthing *current;	bool finished=false;	current=startchar;	do	{		if(current->next=NULL)		{			*ch='\0';			finished=true;		}		else		{			*ch++=current->data;			current=current->next;		}	}while(finished);	return ch;}
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"

This topic is closed to new replies.

Advertisement