6 Replies - 295 Views - Last Post: 03 March 2012 - 06:14 AM Rate Topic: -----

#1 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Order of commands changing behaviour?

Posted 02 March 2012 - 05:08 PM

OK, so this is probably a silly newb-question but I can't seem to find an answer on yon Interweb so I will ask you fine people.

Trying to take two inputs, an integer value and then a line of characters, using scanf/getline respectively. Problem is that this doesn't seem to work. In this order, the program takes the int input then seems to skip right over the getline.

int n;
char* temp;
int size = 100;

temp = (char*) malloc(size);

scanf("%d", &n);
getline(&temp, &size, stdin);


But if I swap their positions, thusly:

int n;
char* temp;
int size = 100;

temp = (char*) malloc(size);

getline(&temp, &size, stdin);
scanf("%d", &n);


they both work! Is this something dumb I'm doing, some bizarre compiler issue, or what? Ideally I'd like to get the first order working properly, and I can't see why it isn't. Any enlightenment offered would be gratefully received.

This post has been edited by Maynia: 02 March 2012 - 05:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Order of commands changing behaviour?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4134
  • View blog
  • Posts: 12,874
  • Joined: 25-December 09

Re: Order of commands changing behaviour?

Posted 02 March 2012 - 05:16 PM

The scanf() leaves the end of line character in the input buffer, The getline() takes this end of line and thinks it has the correct input. So you need to clean out the input buffer after the scanf() before you use getline().

Are you programming a C or C++ program?

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Order of commands changing behaviour?

Posted 02 March 2012 - 05:18 PM

That... makes sense, actually. Hooray for underthinking!

Also, C. Should have mentioned that in the post, but forgot. Apologies there.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • Joined: 16-October 07

Re: Order of commands changing behaviour?

Posted 03 March 2012 - 04:28 AM

It's almost always easier to grab a line and look at that.

Two things. Never cast malloc. And why do you need dynamic storage?

I had to lookup getline; it's non standard. That's why you needed the storage. Have to say, I really don't like it. But let's play with it.
#include <stdio.h>
#include <stdlib.h>

int main() {
	int n, tempSize;
	char *temp;
	
	tempSize = 5;
	temp = malloc(tempSize); /* never cast malloc */
	
	printf("Please enter a number\n");
	/* get a line */
	getline(&temp, &tempSize, stdin);
	
	/* now scanf that */
	sscanf(temp, "%d", &n);

	printf("Please enter a line of text\n");
	/* get another line */
	getline(&temp, &tempSize, stdin);
	
	printf("n=%d\ntemp=\"%s\"\ntempSize=%d\n", n, temp, tempSize);
	
	/* don't forget to give back */
	free(temp);

	return 0;
}



Results:
Please enter a number
34
Please enter a line of text
this is only a test
n=34
temp="this is only a test
"
tempSize=21



Looks like getline keeps the newline. Something to be aware of.

Same program, no getline, standard C, fixed buffer size:
#include <stdio.h>
#include <stdlib.h>

#define BUFF_SIZE 100

int main(void) {
	int n;
	char temp[BUFF_SIZE];
	
	printf("Please enter a number\n");
	fgets (temp, BUFF_SIZE, stdin);
	sscanf(temp, "%d", &n);

	printf("Please enter a line of text\n");
	fgets (temp, BUFF_SIZE, stdin);
	
	printf("n=%d\ntemp=\"%s\"\n", n, temp);
	
	/* no need to free, no chance of leaks */

	return 0;
}





Results:
Please enter a number
42
Please enter a line of text
This is another test
n=42
temp="This is another test
"



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Order of commands changing behaviour?

Posted 03 March 2012 - 05:21 AM

Most informative! Now, to reply to points that I actually have a reply to.

re: casting malloc: if I don't, the compiler whines at me about assigning an int to a char pointer. Pretty sure it's a warning rather than an error, though. Ignorable?

I need dynamic storage because I'm taking an unknown number of lines of unknown length, and it seems the best way to solve that.

Didn't know 'bout sscanf though. Will play with that a bit, it looks useful.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • Joined: 16-October 07

Re: Order of commands changing behaviour?

Posted 03 March 2012 - 06:10 AM

View PostMaynia, on 03 March 2012 - 08:21 AM, said:

re: casting malloc: if I don't, the compiler whines at me about assigning an int to a char pointer.


Probably a sign you're not using a true C compiler. It's either also C++, a more pedantic superset of C, or too damn old. A C compiler written in the last decade doesn't want you to cast your malloc. It deals with void pointers for such operations.

View PostMaynia, on 03 March 2012 - 08:21 AM, said:

I need dynamic storage because I'm taking an unknown number of lines of unknown length


Yes, but not in your example.

At some point, you need to deal with a max length somewhere. Setting a max buffer size protects you from the program trying to allocate a stupid amount of space and crashing. This is called a buffer overflow.

If you want to store an unknown number of lines then it can get tricky. A linked list would be preferred. If you simply need to process a line at a time and not store it forever, then it doesn't really matter how may lines there are.
Was This Post Helpful? 1
  • +
  • -

#7 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Order of commands changing behaviour?

Posted 03 March 2012 - 06:14 AM

re: casting malloc again/errors therein: forgot to include stdlib. SILLY ME.

And yes, that was really just the first problem I encountered. Perhaps I should have given more context as to the overall goal. Lesson learned, will do in future.

Thanks for the help.

This post has been edited by Maynia: 03 March 2012 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1