Function to get and return a C string

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 1658 Views - Last Post: 02 April 2013 - 02:56 PM Rate Topic: -----

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Function to get and return a C string

Posted 01 April 2013 - 09:08 AM

Stop calling strlen all the time. Call it once.
int size = strlen(buffer);
buffer[size-1] = '\0';

for(int x = 0; x < size; x++) {
	buffer[x] = toupper(buffer[x]);
}



Or:
int pos = strlen(buffer) - 1;
buffer[pos] = '\0';
while(pos>0) {
	pos--;
	buffer[pos] = toupper(buffer[pos]);
}



Or, just for fun, you needn't call it at all:
char *p = buffer;
while(*p!='\0' && *p!='\n')
	*p++ = toupper(*p);
}
*p='\0'; // just in case it was a \n


Was This Post Helpful? 0
  • +
  • -

#17 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 09:12 AM

Thanks Baavgai, I fixed it!

Haha I can just see you cringing at me calling it over and over.


This is my last program for this class. I'll be so glad when I can go back to using normal C++ strings. These C strings have taunted me all semester.

This post has been edited by synlight: 01 April 2013 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#18 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 11:24 AM

Okay, I have made a few changes to my string function...

//Prototype:

void getString(char*, char*);

//Function

void getString(char *prompt, char *buffer)
{
	char str[40];
	int size = strlen(buffer);

	printf("\n%s",prompt);
	fgets(buffer,size,stdin);
	//buffer[size-1] = '\0';

	return;
}//END GETSTRING

Function Call:

table[pos].name = getString("\nEnter the customer name:", *buffer);


I am getting an error that the table.name must be a modifiable lvalue..

If I can get this working, I can finish the program on my own. Any advice would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: Function to get and return a C string

Posted 01 April 2013 - 11:27 AM

Well, you should know by now that you can't return anything from a void function.
Also this is probably not doing what you expect.

int size = strlen(buffer);


Jim

This post has been edited by jimblumberg: 01 April 2013 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#20 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 11:39 AM

View Postjimblumberg, on 01 April 2013 - 01:27 PM, said:

Well, you should know by now that you can't return anything from a void function.
Also this is probably not doing what you expect.

int size = strlen(buffer);


Jim


Oops fixed the return, sorry!


Why am I not able to call the function? I can call the other parts of my struct, the ones that are numeric.
Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: Function to get and return a C string

Posted 01 April 2013 - 11:44 AM

Quote

Why am I not able to call the function?

I don't know you haven't provided enough substance for me to tell. Show how you defined your structure. And also re-read my last post. Why do you think I'm mentioning the strlen() call?

Jim
Was This Post Helpful? 0
  • +
  • -

#22 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 11:54 AM

View Postjimblumberg, on 01 April 2013 - 01:44 PM, said:

Quote

Why am I not able to call the function?

I don't know you haven't provided enough substance for me to tell. Show how you defined your structure. And also re-read my last post. Why do you think I'm mentioning the strlen() call?

Jim


I'm sorry. I have 2 kids on Spring Break, and a toddler that just woke up. I'm distracted, and extremely frustrated and just UGH. . I appreciate your help. I'm trying to get as much done on this as I can before I leave for class in 2 hours.

As for the strlen.. is it because buffer is not initialized yet? So the length is 0?

Here is my struct, declared above main:

struct *record
	{
		int custID;
		char name[28];
		char state[3];
		char discCode;
		double balanceDue;
		dueDate date;
	};


And here is my call to the getString function:

record *table[50];




table[pos]->name = getString("\nEnter the customer name:", *buffer);

Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: Function to get and return a C string

Posted 01 April 2013 - 12:03 PM

And how do you copy strings in C?

Quote

As for the strlen.. is it because buffer is not initialized yet?

Yes.

Quote

So the length is 0?

Very rarely will it be zero. But is could be larger than the size of your array.

You should alter your function, if possible to pass the size of the array as a third argument.

Your function call doesn't look correct either. How do you pass an array into a function? Hint: you don't use the *. Also how is that variable buffer defined?

I think you really should reread the function tutorials contained in my signature.

Jim

This post has been edited by jimblumberg: 01 April 2013 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#24 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 12:19 PM

You use strcpy to copy C strings.

I did read the tutorial.. one of the requirements of my program is to pass the string function 2 pointers, one for a prompt, and one to get keyboard input.

buffer is declared as such:

char buffer[40];

Was This Post Helpful? 0
  • +
  • -

#25 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Function to get and return a C string

Posted 01 April 2013 - 12:24 PM

Instead, think of some way to do this:
void loadString(const char *prompt, char *result, int bufferSize);



Your call should simply be:
loadString("\nEnter the customer name:", table[pos].name, sizeof(table[pos].name));



Careful with sizeof, it won't give you what you expect if something degrades to a pointer. Passing table[pos].name makes it a pointer, but within the context of table[pos] you can do an accurate sizeof.
Was This Post Helpful? 2
  • +
  • -

#26 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 12:30 PM

View Postbaavgai, on 01 April 2013 - 02:24 PM, said:

Instead, think of some way to do this:
void loadString(const char *prompt, char *result, int bufferSize);



Your call should simply be:
loadString("\nEnter the customer name:", table[pos].name, sizeof(table[pos].name));



Careful with sizeof, it won't give you what you expect if something degrades to a pointer. Passing table[pos].name makes it a pointer, but within the context of table[pos] you can do an accurate sizeof.


Thank you, baavgai. I'm going to try to puzzle that out for the next half hour before I have to leave for class.

This is my last program for this class. It's due at midnight tomorrow. I have a B right now, if I don't get a C in this class, I won't graduate. And it's only offered in the Spring, so it would take me another year. My GPA is a 4.0.. I'm freaking out right now. Maybe that's clouding my thinking, I'm in panic mode.
Was This Post Helpful? 0
  • +
  • -

#27 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: Function to get and return a C string

Posted 01 April 2013 - 12:31 PM

Also your structure definition is incorrect. It should be:
struct record
	{
		int custID;
		char name[28];
		char state[3];
		char discCode;
		double balanceDue;
		dueDate date;
	};


Notice the lack of the pointer!

Jim
Was This Post Helpful? 1
  • +
  • -

#28 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 01 April 2013 - 12:32 PM

I had JUST changed it to a pointer.. if you notice in the very looong code I posted earlier, it wasn't declared as one. I'm changing code all over the place just trying to get things to work. Not a good strategy. I need to calm down.
Was This Post Helpful? 0
  • +
  • -

#29 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Function to get and return a C string

Posted 02 April 2013 - 10:13 AM

Just wanted to say thanks to Jim and Baavgai for helping me yesterday. My function is working now, and I wanted to post the code, in case it could help anyone else in the future:

PROTOTYPE:
void getString(char *prompt, char *buffer);


FUNCTION:
void getString(char prompt[], char buffer[])
{
	char str[40];
	printf("\n%s",prompt);
	fgets(buffer,40,stdin);

}//END GETSTRING


FUNCTION CALL (n my case.. your mileage will vary):
getName();
		strcpy(table[pos].name, buffer);


ETA: I plan to work on passing the string size once I get this program turned in

This post has been edited by synlight: 02 April 2013 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#30 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,349
  • Joined: 25-December 09

Re: Function to get and return a C string

Posted 02 April 2013 - 10:22 AM

What is the purpose of the str[] variable, you never use it. Since you don't pass the size of the array to the function you should be using a const global variable for the maximum size for all of your arrays. Otherwise you will be in trouble. For instance if you pass the state[] array from your structure to this function you will allow out of bounds access to this array, which is a very bad thing.

getName();

Where are the required parameters?


Jim

This post has been edited by jimblumberg: 02 April 2013 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3