• C

How would I set up my function to accept a string and store it into the array?

I'm trying to prompt the user for their name and then store it into the next record in the structure but my get_str function doesn't work like the get_int and get_float functions do. How would I set up my function to accept a string and store it into the array.
/**************************
*Function to add a record *
**************************/
void addRecord (struct myRecords *table)
{
	char string[50];
	int x;
	int y;
	for (x=0; x < 10; x++)
	{	
		if (table->rec[x].cust_id == 999)
		{
            y = x;
			x = 10;		
		}
	}
	table->rec[y].cust_id = get_int("Please enter customer id: ");
	table->rec[y].cust_name[strlen(table->rec[y].cust_name)-1] = get_str("Please enter customer name: ");
	table->rec[y].state[strlen(table->rec[y].state)-1] = get_str("Please enter a state: ", string);
	table->rec[y].discount = get_char("Please enter discount type: ");
	table->rec[y].balance_due = get_float("Please enter a balance due: ");
	table->rec[y].order_out = get_int("Please enter how many orders are out: ");
    table->rec[y+1].cust_id = 999;
}

/*************************
*Function to get strings *
*************************/
void get_str(char prompt[], char string[])
{
	printf("\n %s",prompt);
	gets(string);
}

Open in new window

reesee324Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Infinity08Connect With a Mentor Commented:
Since the state member is defined as char state[3], you can indeed pass it to the function as argument :

        get_str("Please enter a state: ", table->rec[y].state);

However, you have to be very careful with the gets call inside get_str. It does not know how many characters it's allowed to write into the string. For example, state can only hold a string of 2 characters (plus the terminating '\0' character), so if the user enters anything longer than 2 characters, you have a buffer overflow, and you'll overwrite possibly important memory locations, causing all kinds of strange issues.

You have to avoid this at all costs by making sure that you never write a string longer than 2 characters into state. You could for example use fgets instead of gets :

        http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

which allows you to pass the maximum amount of characters as parameter. But then you have to be aware that the newline character will be included in the string (so you'll have to remove it manually if you don't want that).
0
 
Infinity08Commented:
Your get_str function doesn't return anything (void), yet you're trying to assign the return value :

>> table->rec[y].state[strlen(table->rec[y].state)-1] = get_str("Please enter a state: ", string);

The left-hand side of the assignment also looks a bit odd :

>> table->rec[y].state[strlen(table->rec[y].state)-1]

How is table->rec[y].state defined ?
0
 
reesee324Author Commented:
This is the whole code but when I change my function to character and returning string I still get a bunch of random characters for the 2 strings. I was told that I could pass in an empty string but I didn't quite understand how that would still help the whole thing and get it to store into my table.
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <stdio.h>

struct customers
{
	int cust_id;
	char cust_name[19];
	char state[3];
	char discount;
	double balance_due;
	int order_out;
};

struct myRecords
{
	customers rec[10];
};

using namespace std;

//functions go here
void loadFile(FILE *, struct myRecords*, char[]);
void listFile(struct myRecords*);
int binarySearch(struct myRecords*, int);
void addRecord(struct myRecords*);
int get_int(char[50]);
char get_char(char[50]);
void get_str(char [50], char[50]);
float get_float(char[50]);

int main()
{	
	char buffer[50];
	int counter = 0;
	int cust_id;
	int menu;
	
	myRecords table, *point_table;
	point_table = &table;
	
	menu = get_int("Please enter a selection: \n 1. Load the file\n 2. List the file\n 3. Search the file\n 4. Add a record\n 5. Exit\n");
	
	while (menu != 5)
	{
		if(menu == 0) 
		{ 
			menu = get_int("Please enter a selection: \n 1. Load the file\n 2. List the file\n 3. Search the file\n 4. Add a record\n 5. Exit\n"); 
		} 
		else if(menu == 1)
		{ 
			FILE *fp;
			fp = fopen("ASSIGNV1.DAT", "r");
			if (fp == NULL)
			{
				printf ("Error opening file! ");
			}
			loadFile(fp, point_table, buffer); 
			menu == 0;
		}
		else if(menu == 2)
		{
			listFile(point_table);
			menu = get_int("Please enter a selection: \n 1. Load the file\n 2. List the file\n 3. Search the file\n 4. Add a record\n 5. Exit\n"); 
		}
		else if(menu == 3)
		{
			cust_id = get_int("Please enter a customer id: ");
			binarySearch(point_table, cust_id);
			menu = get_int("Please enter a selection: \n 1. Load the file\n 2. List the file\n 3. Search the file\n 4. Add a record\n 5. Exit\n");
		}
		else if (menu == 4)
		{
			addRecord(point_table);
			menu = get_int("Please enter a selection: \n 1. Load the file\n 2. List the file\n 3. Search the file\n 4. Add a record\n 5. Exit\n");
		}
		else if (menu == 5)
		{
			exit(1);
		}
		else 
		{
			printf("Please enter a valid menu selection!\n");
			menu = get_int("Please enter a selection: \n 1. Load the file\n 2. List the file\n 3. Search the file\n 4. Add a record\n 5. Exit\n");
		}
	}
}//end of main

/***********************
*Function to load file *
***********************/
void loadFile(FILE *fp, struct myRecords* table, char buffer[])
{
	int counter = 0;
	
	while(!(feof(fp)))
	{
		fgets(buffer, 20, fp);
		table->rec[counter].cust_id = atoi(buffer);
		fgets(table->rec[counter].cust_name, 20, fp);
		table->rec[counter].cust_name[strlen(table->rec[counter].cust_name)-1] = '\0'; 
		fgets(table->rec[counter].state, 20, fp);
		table->rec[counter].state[strlen(table->rec[counter].state)-1] = '\0';
		fgets(buffer, 20, fp);
		table->rec[counter].discount = buffer[0];
		fgets(buffer, 20, fp);
		table->rec[counter].balance_due = atof(buffer);
		fgets(buffer, 20, fp);
		table->rec[counter].order_out = atoi(buffer);
		++counter;
	}
	fclose(fp);
}
/***********************
*Function to list file *
***********************/
void listFile(struct myRecords *table)
{
	int x;
	printf ("Cust ID\t Cust Name\t       State   Disc.     Bal Due      Order Out\n");
	for (x=0; x < 10; x++)
	{	
		if(table->rec[x].cust_id == 999)
		{
			x = 10;		
		}
		else
		{
			printf ("\n%i\t%-20s\t%-7s\t%c\t%0.2f\t\t%i", table->rec[x].cust_id, table->rec[x].cust_name, table->rec[x].state, table->rec[x].discount, table->rec[x].balance_due, table->rec[x].order_out);
		}
	}
}
/****************************
*Function for Binary Search *
****************************/
int binarySearch (struct myRecords *table, int cust_id)
{
	int x;
	int last;
	for (x=0; x < 10; x++)
	{	
		if(table->rec[x].cust_id == 999)
		{
			last = x;
            x = 10;	
        }
	}
	int first=0, middle, position=-1;
	bool found=false;
	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (table->rec[middle].cust_id == cust_id)
		{
			found = true;
			position = middle;
		}
		else if (table->rec[middle].cust_id > cust_id)
		{
			last = middle - 1;
		}
		else
		{	
			first = middle + 1;
		}
	}
	
	if (position == -1)
	{
		printf ("RECORD NOT IN DATABASE!");
	}
	else
	{
		printf ("\n%i\t%-20s\t%-7s\t%c\t%0.2f\t\t%i", table->rec[middle].cust_id, table->rec[middle].cust_name, table->rec[middle].state, table->rec[middle].discount, table->rec[middle].balance_due, table->rec[middle].order_out);
	}
	return position;
}
/**************************
*Function to add a record *
**************************/
void addRecord (struct myRecords *table)
{
	char string[50];
	int x;
	int y;
	for (x=0; x < 10; x++)
	{	
		if (table->rec[x].cust_id == 999)
		{
            y = x;
			x = 10;		
		}
	}
	table->rec[y].cust_id = get_int("Please enter customer id: ");
	table->rec[y].cust_name[strlen(table->rec[y].cust_name)-1] = get_str("Please enter customer name: ");
	table->rec[y].state[strlen(table->rec[y].state)-1] = get_str("Please enter a state: ", string);
	table->rec[y].discount = get_char("Please enter discount type: ");
	table->rec[y].balance_due = get_float("Please enter a balance due: ");
	table->rec[y].order_out = get_int("Please enter how many orders are out: ");
    table->rec[y+1].cust_id = 999;
}
/**************************
*Function to get integers *
**************************/
int get_int(char prompt[50])
{
	char buf[50];
	int i;
	printf("\n %s", prompt);
	gets(buf);
	i = atoi(buf);
	return i;
}

/****************************
*Function to get characters *
****************************/
char get_char(char prompt[50])
{
	char c[50];
	printf("\n %s", prompt);
	gets(c);
	return c[0];
}
/*******************************
*Function to get float numbers *
*******************************/
float get_float(char prompt[50])
{
	char buffer[50];
	float r;
	printf("\n %s", prompt);
	gets(buffer);
	r = atof(buffer);
	return r;
}
/*************************
*Function to get strings *
*************************/
void get_str(char prompt[], char string[])
{
	printf("\n %s",prompt);
	gets(string);
}

Open in new window

0
 
reesee324Author Commented:
Great! Got it working, thanks so much!
0
 
Infinity08Commented:
Glad to have been of assistance :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.