Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Enter how many messages to create
2
Enter 1th Message
Enter 2th Message
Following are messages
1. 
2. 


No proper output

What I have tried:

//program to read n messages and display them

#include<stdio.h>
#include<string.h>
int main()
{
  
    char message[100][100];
    int i,n;
  
    printf("Enter how many messages to create\n");
    scanf("%d",&n);
            
    for(i=0;i<n;i++)
    {
		printf("Enter %dth Message\n",i+1);
		fflush(stdin);
		
            //gets(message[i]);
    scanf("%[^\n]",message[i]);
	}
	
	printf("Following are messages\n");
	for(i=0;i<n;i++)
    {
		printf("%d. %s\n",i+1,message[i]);
	}
	
    return 0;
}
Posted
Updated 27-Feb-23 21:48pm
Comments
OriginalGriff 28-Feb-23 0:55am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.

1 solution

This is the actual output of your program:
Enter how many messages to create
2
Enter 1th Message
Enter 2th Message
Following are messages
1. 
2. 

Why did you comment out this line?
//gets(message[i]);

This way the user has never a chance to enter a message!
If you remove the comment the output will look like this:
Enter how many messages to create
2
Enter 1th Message
hello
Enter 2th Message
world
Following are messages
1. hello
2. world

My recommendation: Learn to use a debugger.
Then you will be able to see what your program is actually doing.
 
Share this answer
 
Comments
medha s tattimani 28-Feb-23 8:06am    
Error: Function  gets(message[i]) is not defined in current scope
So I used scanf brother
TheRealSteveJudge 28-Feb-23 8:24am    
OK. You're welcome!

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