Jump to content
  • Advertisement
Sign in to follow this  
dbzprogrammer

My Own Input Function Problem

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!