hey guys i'm new to c and still learning.need some help here.

i wrote this code and it isn't work properly,unable to figure out whats wrong!!but when the strget() call from the main() its working.please some help...

main()
{
	int x;
	
	printf("enter a value	:");
	scanf("%d",&x);
	
	switch(x)
	{
		case 1:
		pri();
	}
	
	
}
void strget()
{
	int i;
	char z[30];
	for(i=0;i<4;i++)
	{
		
		printf("enter string:");
		gets(z);
	}
}

What are you trying to do (to learn)? The posted snippet is quite broken, therefore, hard to guess your intention with it.
What do you think is supposed to happen and it is not occurring?

i try to learn this..this is not the original program.bt same structure and gives the same error.

when program start to looping it prints "enter string: enter string: "
thats the error..

screen clipping attached..

hey..i've figure out that whats wrong with this.it's caused due to buffer.once clean the buffer and no more errors

#include<stdio.h>
void pri(void);

main()
{
	int x;
	
	printf("enter a value	:");
	scanf("%d",&x);
	
	switch(x)
	{
		case 1:
		pri();
	}
	
	
}
void pri()
{
	
	int i;
	char z[30];
	for(i=0;i<4;i++)
	{
		fflush(stdin);
		printf("enter string:");
		gets(z);
	}
}

thanx Aia for trying to help.but i still unable to understand whats happned .can you explain ??

what do you want the program to do ???

this is not the program which i'm building.i wrote this to present my problem.now it's solved.but unable to understand the theory part of it.(i mean how the program works correctly after write fflush(stdin); )

The problem is when you code of the sort

int i;
printf("Enter val ");
scanf("%d",&i);

and you input the value 5, you are actually pressing 2 keys 5 and the enter key. The value 5 gets assigned to i but enter key is still present in the stream. Then in the while loop

while(i<4)
    {
              printf("Enter String: ");
              gets(str);
              printf("String is %s",str);
              i++;
    }

The code will print Enter String. The enter('\0') which was already present it the stream will be assigned to str. The code will print String is and then it will again go to the top of the while loop.
When you use fflush(stdin), it removes any char in the stream, so the extra '\0' char which was present in the stream disappears and the program works as expected

I have explained to you why the program works when you use fflush(stdin), but it is a bad idea to use fflush(stdin).Use the link above to see what the problems are with fflush

commented: nice work +1
commented: For endorsing the use of gets() -2

thanx for the lesson bro.

thanx Aia for trying to help.but i still unable to understand whats happned .can you explain ??

The logic of the program is nonsensical.

void pri()
{
	
	int i;
	char z[30];
	for(i=0;i<4;i++)
	{
		fflush(stdin);
		printf("enter string:");
		gets(z);
	}
}

It overwrites z variable four times with whatever the user inputs and then exits. That's it. The visual result to the human eye, might or might not be the display of the phrase: "enter string:" four times.

fflush() function is not intended for stdin. The proper use would have been:

printf("enter string: ");
fflush(stdout); /* stdout, no stdin */

gets() is a dangerous function that will read input without respect for the capacity of the variable passed as parameter. It can produce buffer over runs easily.
fgets() is recommended instead.

main()
{
	int x;
	
	printf("enter a value	:");
	scanf("%d",&x);
	
	switch(x)
	{
		case 1:
		pri();
	}
	
	
}

main should explicitly return an int and the passed argument can be void or a combination of two variables to record how many arguments were passed at the command line.
Meaning int main (void) or int main (int argc, char *arg[]) In your case the first one is correct.

int x;
	
	printf("enter a value	:");
	scanf("%d",&x);

printf("enter a value :"); might or might not appear on the screen at the proper time. printf("enter a value :\n"); the \n will guarantee the proper display of the screen or

printf("enter a value :");
fflush(stdout);

scanf("%d", &x); by itself is a gambling proposition. It is a must to check the return of the function before continuing and using the result stored in x. Like so:

if (scanf("%d", &x) == 1) {
    /* then I can use x */
}

or

if (scanf("%d", &x) != 1) {
   /* I cannot use x, I must do something else */
}

This is ugly

switch(x)
	{
		case 1:
		pri();
	}

The use of a switch was intended to eliminate the repetitive code when the need for many if/if else was required.
However, here using just one set of condition if/else is the proper way.
i.e.

if (x == 1)
{
    pri();
}

Now, even with all this correction, the code is still nonsensical and no amount of proper syntax will help that.
If you state your reasoning behind, maybe more can be done to remedy that.

while(i<4)
    {
              printf("Enter String: ");
              gets(str);
              printf("String is %s",str);
              i++;
    }

Over three-hundred and a half posts in this forum and still endorsing the use of gets()?
Makes me wonder at one point you are going to recognize that there's no instance for its use. Specially at the learning stage.

yeah,it;s nonsensical code.with you guys help,i'm going to write better codes.thanks for your post Aia..it's very useful for me.

Over three-hundred and a half posts in this forum and still endorsing the use of gets()?
Makes me wonder at one point you are going to recognize that there's no instance for its use. Specially at the learning stage.

I am not endorsing gets(). I was trying to clear the confusion of baby_c so as to why his code behaves the way it does. But I should have included a line at the end telling him to use fgets instead of gets

I am not endorsing gets(). I was trying to clear the confusion of baby_c so as to why his code behaves the way it does. But I should have included a line at the end telling him to use fgets instead of gets

Yes, you should had.
As stands, for all purposes and intend, you were endorsing it. Especially, since you modified her code, and included it without warnings. And even with a warning, it is still dubious.

hey,actually that code didn't give me the error.it's a model one.this is the original one..

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

void create(void);
void edit(void);
void view(void);
void menu(void);

main()
{	
	menu();
}

void menu()

{
	int i,t;
	
	printf("-----------------------------------\n            Results Database               \n-----------------------------------\n");
		printf("\t\n\nWhat do you want to do?\t\n\n");
	printf("\t\tCreate new database---------[1]\n");
	printf("\t\tEdit exiting database-------[2]\n");
	printf("\t\tView database---------------[3]\n");
	printf("\t\tExit------------------------[4]\n\n");
	
	printf("Choose a option\t:\t");
	scanf("%d",&i);
	

		switch(i)
	{	
		case 1:
		create();
		
      /*	case 2:
		edit();
		
		case 3:
		view();        */
		
		 case 4:
		 exit(1);
		 
	default:
			{
		                printf("\n\n\n     ***    Incorrect input   ***              ");
				system("cls");
				menu();
			}
			
	}

}


void create()
	{

	int i,j,k;
	char name[30],fname[30],z[5];

	FILE *fp;
	fp=fopen("C:\\database.txt","w+");
	
	if(fp==NULL)
	{
		printf("Error\n");
		exit(1);
	}
	system("cls");
		for(i=0;i<4;i++)
	{	
		fflush(stdin);
		printf("Student no.%d name  :",i+1);
		gets(name);
		strcat(name,"\n");	
		fputs(name,fp);
			
			strcpy(fname,name);				
			for(j=0;j<31;j++)				
			{								
				if(name[j]==' ')			//
				fname[j]='\0';				//
			}								
				

	printf("%s's z-score  :",fname);

		gets(z);
		
		fputs(z,fp);
	
		fputs("\n",fp);

	system("cls");		//clear the screen
	

	}
	
	
	
	fclose(fp);
	
	menu();
}

is that ugly too ?????

Still making use of fflush(stdin)
Still making use of gets()
Still not checking the return of scanf()
Still not using the proper main construct.

Adding extra none portable components:
conio.h
system("cls");

How come you thought there would be a positive answer if you haven't tried any of the previous suggestions?

A good thing going on for you is declaring prototypes at the beginning of the code, and opening a file for writing and checking that it was successful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.