Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a program where I could dynamically increase the size of a character pointer as the user enters each character into the console. Here is the program where I am attempting this concept: (A dynamically allocated string)

C++
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct Structure
{
	char *buf;
	int intvar1, intvar2;
};

char *getdt()
{
	char *chr = (char*)malloc(1);
	int i = 0;
	char ch = getchar();
	while (ch != '\n')
	{
		chr[i] = ch;
		ch = getchar();
		chr = (char *)realloc(chr, (++i) + 1);
	}
	chr[i] = '\0';

	return chr;
}

void main()
{

	int elmts = 0, i = 0;
	Structure * strt;
	scanf("%d", &elmts);

	strt = (Structure*)malloc(sizeof(Structure)*elmts);

	for (i = 0; i < elmts; i++)
	{
		strt[i].buf = getdt();
		scanf("%d", &strt[i].intvar1);
		scanf("%d", &strt[i].intvar2);

	}

	for (i = 0; i < elmts; i++)
	{
		printf("%s %d %f", strt[i].buf, strt[i].intvar1, strt[i].intvar2);
	}


	free(strt);
	getch();

}


[The getdt() function is supposed to get a string from the user.]

The only trouble is with the following fragment:
strt[i].buf = getdt();
		scanf("%d", &strt[i].intvar1);
		scanf("%d", &strt[i].intvar2);

where I am able to retrieve a string properly at strt[i].buf = getdt(), but the successive scanf() 's skips and goes directly to the for-loop beneath.

After some researching I suspect the combination getchar() and scanf() would be the culprit. But I really dont understand what causes the malfunction.

Could any corrections be suggested ?

What I have tried:

This is what I have tried:
C++
for (i = 0; i < n; i++)
{
strt[i].buf = getdt();
char tmp[10],tmp1[10], *tp,*tq;
getss(tmp);
strt[i].intvar1 = strtol(tmp,&tp,10);
gets(tmp1)
strt[i].intvar2 = strtol(tmp1,&tq,10)
}

These efforts were simply in vain.
Posted
Updated 26-Jun-17 21:29pm
v2
Comments
Rick York 27-Jun-17 13:13pm    
I think Richard's answer is on target. If you don't want to use gets then you can write your own that accepts as many characters as are entered. I think it would be easier to just pass a really big string, say 128 or 256 characters, because it seems unlikely a user will enter a string that big for this purpose.

1 solution

Why complicate things for yourself? Use gets, _getws[^] to read a string. This is a common problem with scanf, it does not fir well with other console character input calls; gets should avoid this problem as it clears the input buffer.
 
Share this answer
 
Comments
compuknow 27-Jun-17 12:25pm    
Can gets be used if the size of the char array (or the pointer in this case) is known only at runtime., ie., only when the user inputs the string ?
Richard MacCutchan 27-Jun-17 12:28pm    
The documentation explains it all.
compuknow 27-Jun-17 12:31pm    
The document gives an example of fixed size arrays. for example 'char line[21]', but I have a char pointer whose value is known only as the user enters the string.
Richard MacCutchan 27-Jun-17 12:39pm    
Then use getchar as you have done, but make sure you flush the input buffer so scanf will work. See http://c-faq.com/stdio/stdinflush2.html. Unfortunately scanf is not a particularly friendly function so you have to code round it to get the result you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900