Jump to content

Hey guys I'm having trouble with my append functions in my current homework assignment.  

The functions are correctly taking in the users input, but they are not allocating the memory

and inserting into my linked lists.  I'm not entirely sure how to have them allocate the memory

and then send it into my data structure.  Also, im pretty lost as to how I should implement the

destructors for this program.  Any help or tips would be appreciated.  Thanks, CJ

Functions.cpp

Header,h

main.cpp

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/675710-append-functions-c-help/
Share on other sites

Link to post
Share on other sites

Do you need to use chars? If not, just use std::string. But if you do... Now you create assistance instance which allocates 3 char arrays, then you cut them to size by allocate as long arrays as those cstrings are. Instead of creating those 3 arrays just create function that will prompt for input and allocate such array that is already cut to size of the input, and pass that pointer to your node, and since node creation is successful the node will take care of freeing that memory, but if any of input fails and node is not created then you need free those pointers yourself.

 

I came up with that contraption:

#include <iostream>
#include <cstring>

const int BUFLEN = 30;

char * input(){
	char buf[BUFLEN];
	size_t len;
	char c;
	
	do {		
		if(!static_cast<bool>(std::cin.get(buf, BUFLEN, '\n'))){
			std::cout << "No data has been entered" << std::endl;
			std::cin.clear();
		}
		
		while((c = std::cin.peek()) != std::istream::traits_type::eof() && (static_cast<bool>(std::cin.ignore()) || true) && c != '\n');
		// The above loop is short of below
		/*do {
			c = std::cin.peek();
			if(c != EOF){
				std::cin.ignore();
			}
		} while(c != '\n' && c != EOF);*/
		
		len = strlen(buf);
	} while(len == 0);
	
	try {
		char *ptr = new char[len + 1];
		memcpy(ptr, buf, sizeof(char) * (len + 1));
		
		return ptr;
	} catch(std::bad_alloc &ba){
		return NULL;
	}
}

I have tested it by just pressing enter (no input), with input that exceeds buffer, it ignores rest of the input so next input will prompt user for data. I have tested it also feeding stdin with file. The function works, but I feel like it is overdone, I didn't figured out anything else though. 

 

Usage:

char *cstr1 = input();
if(cstr1){
  std::cout << "OK: " << cstr1 << std::endl;

  char *cstr2 = input();
  if(cstr2){
    std::cout << "OK: " << cstr2 << std::endl;

    delete[] cstr1;	
    delete[] cstr2;
  } else {
    std::cout << "FAIL." << std::endl;
    delete[] cstr1;
  }
} else {
  std::cout << "FAIL." << std::endl;
}

For more than one inputs, usage could be shortened by pulling try catch block from the function and surrounding input function calls with try catch block:

 

char *cstr1 = NULL;
char *cstr2 = NULL;

try {
  char *cstr1 = input();
  std::cout << "OK: " << cstr1 << std::endl;

  char *cstr2 = input();
  std::cout << "OK: " << cstr2 << std::endl;


} catch(std::bad_alloc &ba){
  std::cout << "FAIL." << std::endl;
  delete[] cstr1;
  delete[] cstr2;
}

you set pointers to null so if any input fails to allocate and set new pointer value it will delete[] on null pointer which is safe.

 

Also your code is more like sketch, right? Cause there are some undefined variables, like help which is passed untouched since declaration to add member function of list.

 

If I could use std::string I would probably end up using std::getline and there would not be so much contraptions.

 

I would recommend to keep all classes in separate files as long as class/struct has function that body is not within header file. Your Functions.cpp file contains definitions of functions of two different classes mixed together. Also that global that resides in header file is hard to find at first, maybe make it a static variable of your class.

Link to comment
https://linustechtips.com/topic/675710-append-functions-c-help/#findComment-8701048
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×