Link to home
Start Free TrialLog in
Avatar of reesee324
reesee324

asked on

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

Avatar of Infinity08
Infinity08
Flag of Belgium image

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 ?
Avatar of reesee324
reesee324

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great! Got it working, thanks so much!
Glad to have been of assistance :)