You mean like this?:
for(y=0;temp[y]!='\0';y++)
{
names[x][y]=temp[y];
}
names[x][y]='\0';
I updated it in the code.
Printable View
Updated code2:
This code has started behaving very strangely now. Now its taking 10 inputs and closes all of a sudden.
Red codes are recent changes.
Code:#include<stdio.h>
void word_swap(char*,char*);
int main()
{
char temp[20],names[5][20];
int x,y,z;
for(x=0;x<5;x++)
{
printf("\nEnter a name :");
fgets(temp,20,stdin);
for(y=0;temp[y]!='\0';y++)
{
names[x][y]=temp[y];
}
names[x][y]='\0';
}
for(x=0;x<5;x++)
{
for(z=x+1;z<5;z++)
{
for(y=0;names[x][y]!='\0'&& names[z][y] !='\0';y++)
{
if((int)names[x][y]>(int)names[z][y])
word_swap(&names[x][0],&names[z][0]);
}
}
}
for(x=0;x<5;x++)
{
printf("\n");
for(y=0;names[x][y]!='\0';y++)
printf("%c",names[x][y]);
}
getchar();
return 0;
}
void word_swap(char*str1,char*str2)
{
int n=0;
char temp;
while(n!=2)
{
temp=*str1;
*str1=*str2;
*str2=temp;
str1++;
str2++;
if(*str1=='\0')
n++;
if(*str2=='\0')
n++;
}
}
It would be nice if I didn't have to hold your hand. How did you test it yourself?
I'm going to post an example program to test your work this time.
Based on the output:Code:
#include <stdio.h>
#include <assert.h>
int test ( char names[5][20], int x, char * temp )
{
int y;
if ( scanf( "%19s", temp ) == 1 )
{
for(y = 0; temp[y] != '\0'; y++)
{
names[x][y] = temp[y];
}
names[x][y] = '\0';
}
return 0; /** siccessful return **/
}
void dump ( char names[5][20], const unsigned count )
{
unsigned idx;
for ( idx = 0; idx < count; idx++ )
{
printf( "%d : %s\n", idx, names[idx] );
}
}
int main ( void )
{
char names[5][20];
char temp[20];
const unsigned N = 5;
int idx;
for ( idx = 0; idx < N; idx++ )
{
/** learn to assert what the program is doing from time to time. **/
/** if you're wrong, it fails loudly, but not so it breaks everything. **/
assert( test( names, idx, temp ) == 0 );
}
dump( names, N );
return 0;
}
Joe
John
Mary
Dave
Shirley
0 : Joe
1 : John
2 : Mary
3 : Dave
4 : Shirley
It seems to work fine with these relatively simple input expectations. Plus, by reading into a temporary buffer, you don't have to pollute the data we use later on in the program if something goes wrong. But learning how to test your program's units is important. Why? It leads to better software.
I guess there is some problem with my IDE (Dev C++).
Same problem while running your code- the console window closes after input. I tried adding getchar() before the return 0 in main but it didn't work.
I have been using it for last 2 months and this is the first time its giving me this kind of trouble.
>I guess there is some problem with my IDE (Dev C++).
It's probably not that. Dev C++ doesn't keep a shell window open for you to view the output after the program is finished. Terminal programs aren't expected to stay open while their "idle" without instructions to follow, unlike an open GUI program. More information about what you can do about it here: http://cpwiki.sf.net/Pause_console
That problem was probably my fault because I used scanf (because you should know that function and wouldn't have to worry about what's working and whatnot there). I probably shouldn't have used it. But you can open a shell yourself and ran it to see the output.
Uhm, what I linked to in the previous post was going to teach you how to unit test. "Write a little, debug a lot."
Why is the window closing ? I ran this code in MS visual express. It takes five inputs and says "Press any key to continue....", and when i press the key, it closes ! Is there something wrong with the code ? Should i use fflush(stdin) ?
I know fflush is bad but I don't know other alternative to clear the buffer.
Huh? That's exactly how it should work.
What is it that you're expecting?
I'm guesing you didn't understand this since you haven't started to fix it yet. I shall be more specific, but please ask questions if you don't understand what's wrong.
This part does NOT order two names alphabetically, the job which you clearly intend it to do!
Try a few examples on paper and you will see the problem.Code:for(y=0;names[x][y]!='\0'&& names[z][y] !='\0';y++)
{
if((int)names[x][y]>(int)names[z][y])
word_swap(&names[x][0],&names[z][0]);
}
word_swap should NOT be inside a loop here.
You can try adding a line of code like this:
The right way to empty the input buffer depends on your compiler. The above line of code is unreliable on my old Turbo C compiler, sometimes sending it into an endless loop, sometimes working. For Turbo C, I use fflush(stdin) sometimes, simply because Borland made that feature to work properly in Turbo C, and recommended it in their tutorials, help screens, etc.Code:while((c = getchar()) != '\n');
You just have to keep in mind that fflush(stdin) doesn't result in defined behavior with compilers, today.
No, it doesn't. All compilers are expected to follow the standard, so if you follow the standard, it works in all compilers.
You are not supposed to use Turbo C at all. Never. There's no good reason to use it.Quote:
The above line of code is unreliable on my old Turbo C compiler...
Except that you shouldn't use Turbo C ever, this is even more so incredibly bad that they recommend flushing the input buffer, which results in undefined behavior. Don't do it!Quote:
For Turbo C, I use fflush(stdin) sometimes...
That one's hilarious.
Who appointed you supreme ruler of compilers? Turbo C is a great compiler, it just happens to be old. If you were writing code to run on an old 286, it would be an excellent choice, as it is small, fast, and runs in DOS, which is the only OS you'd really be able to use on that system.Quote:
You are not supposed to use Turbo C at all. Never. There's no good reason to use it.
Want to fight again, I see.
Or at least is supposed to. The standard was invented to make sure it works as expected, after all.
Yeah, and who writes for that these days? :rolleyes:Quote:
Who appointed you supreme ruler of compilers? Turbo C is a great compiler, it just happens to be old. If you were writing code to run on an old 286, it would be an excellent choice, as it is small, fast, and runs in DOS, which is the only OS you'd really be able to use on that system.
Even if you were, Turbo C does not adhere to the current standards and thus should be avoided.
Yes i did't understand what's wrong with that loop. I had to Google "define:lexicographic" to get a slight meaning of what you said. I first tried to solve the "window closing unexpectedly" problem and then come on the sorting thing...
I am expecting it to take the 5 inputs, arrange them in alphabetical order and then display it.Quote:
Huh? That's exactly how it should work.
What is it that you're expecting?
What it does--"Takes the five inputs and closes the window."
Yes I will try that but its time to go to college now. :)Quote:
Try a few examples on paper and you will see the problem.
There are NO compilers that are 100% C99 compliant, and most are not 100% C89 compliant, after all this time. So there goes your argument, right out the window. :)
I can get help in Turbo C, before I can even THINK about getting the same help in Visual C 6.0., so if you value your time, you have a very good reason to use Turbo C.
They MADE fflush(stdin) work for Turbo C. How about your compiler? It's a bit odd when you join the cheerleaders for the Standard when 1) you are writing programs for yourself 99.9% of the time, 2) The compilers basically ignore the standards, and 3) The Standard could arguably be described as somewhat "off the rails".
Rather than just being a fanatic about "the Standard, the Standard, the Standard", why not just accept and embrace the reality that every compiler is somewhat different, and has it's own strong and weak points, and use whichever one fits your own purposes?
Specifically, there are many students on our forum, who are REQUIRED to use Turbo C (or some other equally non-standard compliant compiler), by their instructor. Your negativity is far less than helpful. It is itself, the one thing that is really useless.