hello,
Thanks to previous help which has gotten me this far.
However, my lovely program, although looks great in theory doesnt do what its supposed to do. When I type in 3 words, it doesnt put them in order as it should. Can someone point out to me where I am going wrong?

#include <iostream>
#include <string.h>


using namespace std;


int main ()
{
char word1[50] = "word1 string";
char word2[50] = "word2 string";
char word3[50] = "word3 string";


string wordsml;
string wordmed;
string wordlge;


cout<<"This program will ask you to enter 3 words. \n"
"It will then diplay them in order of word length \n"
"(Shortest word first)"<<endl;



cout<<"\tPlease enter 3 words\n";
cin>>word1>>word2>>word3;


if (strcmp (word1, word2)< 0)
{
wordsml = word1;
wordlge = word2;
}


if (strcmp (word1, word2)> 0)
{
wordsml =word2;
wordlge = word1;
}



if (strcmp(wordlge.c_str(), word3) <0);
{
wordmed = wordlge;
wordlge = word3;
}


if (strcmp(wordlge.c_str(), word3) >0);
wordmed = word3;


cout<<"The order of words from smallest to largest is "<<wordsml<<" and "<<wordmed<<" and "<<wordlge<<endl;


system("pause");
return 0;


}

Edited 3 Years Ago by happygeek: fixed formatting

try and use strlen and then display accordingly ..then might be your lovely program may run!!!!

What error is coming...some logical error might be?? does it happen that it is being printed in the dictionary order??

I am trying with this method. (you are the second person who has suggested- so I'b better try)...

But no, still weird error of putting them in varying orders....

#include <iostream>
#include <string.h>


using namespace std;


int main ()
{
int ref,x,y,z;
char word1[50] = "word1 string";
char word2[50] = "word2 string";
char word3[50] = "word3 string";


string wordsml;
string wordmed;
string wordlge;


cout<<"This program will ask you to enter 3 words. \n"
"It will then diplay them in order of word length \n"
"(Shortest word first)"<<endl;



cout<<"\tPlease enter 3 words\n";
cin>>word1>>word2>>word3;


x=strlen (word1);
y=strlen (word2);
z=strlen (word2);


if (x<y)
{
wordsml = word1;
wordlge = word2;
}


if (x>y)
{
wordsml =word2;
wordlge = word1;
}


ref=strlen(wordlge.c_str());


if (ref<z)
{
wordmed = wordlge;
wordlge = word3;
}


if (ref>z);
wordmed = word3;


cout<<"The order of words from smallest to largest is "<<wordsml<<" and "<<wordmed<<" and "<<wordlge<<endl;


system("pause");
return 0;


}

Edited 3 Years Ago by happygeek: fixed formatting

What error is coming...some logical error might be?? does it happen that it is being printed in the dictionary order??

Hello,
thanks for reply, but no, doesnt seem to be dictionary related.
I just try dork words like wrr and wrrrrrr and wr, and just get it to compare and try to organise by length of string.

show ur input and output ..or exactly the error..

OK,
here are 2 examples, one is good, other not so good...
ATTEMPT1

This program will ask you to enter 3 words.
It will then diplay them in order of word length
(Shortest word first)
Please enter 3 words
wrrrrr
wrrr
wrrrr
The order of words from smallest to largest is wrrr and wrrrr and wrrrrr
Press any key to continue . . .

ATTEMPT2

This program will ask you to enter 3 words.
It will then diplay them in order of word length
(Shortest word first)
Please enter 3 words
wr
wrrr
wrrrrr
The order of words from smallest to largest is wr and wrrrrr and wrrr
Press any key to continue . . .

Hello,
thanks for reply, but no, doesnt seem to be dictionary related.
I just try dork words like wrr and wrrrrrr and wr, and just get it to compare and try to organise by length of string.

Why don't you sort them using a sorting Al-Gore-it-him.

So put the words inside an array or vector first?

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

well I am using a turbo compiler and through it hte following code works

#include<iostream.h>
#include<conio.h>
#include<string.h> 
void main()
{
	 char *p;
	 char* word[3];
 
	 cout<<"Enter the word 1:-->";
	 cin>>p;
	 word[0]=new char[strlen(p)+1];
	 strcpy(word[0],p);
 
	 cout<<"Enter the word 2:-->";
	 cin>>p; 
	 word[1]=new char[strlen(p)+1];
	 strcpy(word[1],p);
 
	 cout<<"Enter the word 3:-->";
	 cin>>p;
	 word[2]=new char[strlen(p)+1];
	 strcpy(word[2],p);
 
	 char *temp;
	 for(int i=0;i<3;i++) 
	 {
		 for(int j=0;j<2-i;j++)
			{
				 if(strlen(word[j])>strlen(word[j+1]))
				 { 
						temp=word[j];
						word[j]=word[j+1];
						 word[j+1]=temp;
				 }
			}
	 }
 
	 for(i=0;i<3;i++)
	 {
		 cout<<word[i]<<endl;
	 }
 
	 getch();
}

So See iamthwee has given the code(pseudo)

To sort three numbers without arrays or loops is quite funny...

I think the best sudo code is as follows:

PROGRAM  Order
   IMPLICIT  NONE

   INTEGER  :: a, b, c

   READ(*,*)  a, b, c

   IF (a < b) THEN                 ! a < b here
      IF (a < c) THEN              !   a < c     : a the smallest
         IF (b < c) THEN           !      b < c  : a < b < c
            WRITE(*,*)  a, b, c
         ELSE                      !      c <= b : a < c <= b
            WRITE(*,*)  a, c, b
         END IF
      ELSE                         !   a >= c    : c <= a < b
         WRITE(*,*) c, a, b
      END IF
   ELSE                            ! b <= a here
      IF (b < c) THEN              !   b < c     : b the smallest
         IF (a < c) THEN           !     a < c   : b <= a < c
            WRITE(*,*)  b, a, c
         ELSE                      !     a >= c  : b < c <= a
            WRITE(*,*)  b, c, a
         END IF
      ELSE                         !   c <= b    : c <= b <= a
         WRITE(*,*)  c, b, a
      END IF
   END IF

END PROGRAM  Order

If you can use loops have a look at hackwizz's solution

[edit] Actually hackwizz's code is probably not the best to look at. Ha ha it's awful[/edit]
[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

To sort three numbers without arrays or loops is quite funny...

I think the best sudo code is as follows:

PROGRAM  Order
   IMPLICIT  NONE

   INTEGER  :: a, b, c

   READ(*,*)  a, b, c

   IF (a < b) THEN                 ! a < b here
      IF (a < c) THEN              !   a < c     : a the smallest
         IF (b < c) THEN           !      b < c  : a < b < c
            WRITE(*,*)  a, b, c
         ELSE                      !      c <= b : a < c <= b
            WRITE(*,*)  a, c, b
         END IF
      ELSE                         !   a >= c    : c <= a < b
         WRITE(*,*) c, a, b
      END IF
   ELSE                            ! b <= a here
      IF (b < c) THEN              !   b < c     : b the smallest
         IF (a < c) THEN           !     a < c   : b <= a < c
            WRITE(*,*)  b, a, c
         ELSE                      !     a >= c  : b < c <= a
            WRITE(*,*)  b, c, a
         END IF
      ELSE                         !   c <= b    : c <= b <= a
         WRITE(*,*)  c, b, a
      END IF
   END IF

END PROGRAM  Order

If you can use loops have a look at hackwizz's solution

[edit] Actually hackwizz's code is probably not the best to look at. Ha ha it's awful[/edit]
[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

HEY,
just tried that.

BAD NEWS.
Order is no good:

Here is the output for 2 trials (one worked ok)

This program will ask you to enter 3 words.
It will then diplay them in order of word length
(Shortest word first)
Please enter 3 words
long
longest
longer
longlongerlongestPress any key to continue . . .

This program will ask you to enter 3 words.
It will then diplay them in order of word length
(Shortest word first)
Please enter 3 words
long
longer
longest
longlongestlongerPress any key to continue . . .

Let me know if you want me to post what I wrote in case you think I changed your stuff....

Let me know if you want me to post what I wrote in case you think I changed your stuff....

It should work, although I can't be bothered to test it.

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/sort.html

Did you translate the sudo code correctly into c++?

In any case, is this really the solution your teacher expects you to hand in? Me thinks not. Use arrays :rolleyes:

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

i tried your initial program and i notice that order problem too however if i change ur cout at the end like this:

cout<<"The order of words from smallest to largest is "<<wordsml<<" and "<<wordlge<<" and "<<wordmed<<endl;

with the order wordsml , wordlge, wordmed there is no problem and it works perfectly.

#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
int ref,x,y,z;
char word1[50] = "word1 string";
char word2[50] = "word2 string";
char word3[50] = "word3 string";
string wordsml;
string wordmed;
string wordlge;
cout<<"This program will ask you to enter 3 words. \n"
"It will then diplay them in order of word length \n"
"(Shortest word first)"<<endl;

cout<<"\tPlease enter 3 words\n";
cin>>word1>>word2>>word3;
x=strlen (word1);
y=strlen (word2);
z=strlen (word2);
if (x<y)
{
wordsml = word1;
wordlge = word2;
}
if (x>y)
{
wordsml =word2;
wordlge = word1;
}
ref=strlen(wordlge.c_str());
if (ref<z)
{
wordmed = wordlge;
wordlge = word3;
}
if (ref>z);
wordmed = word3;
cout<<"The order of words from smallest to largest is "<<wordsml<<" and "<<wordlge<<" and "<<wordmed<<endl;
system("pause");
return 0;
}

that should be the end to your problems.

edit: OK guess not :o

Edited 3 Years Ago by happygeek: fixed formatting

Actually hackwizz's code is probably not the best to look at. Ha ha it's awful

Seeing your posts I believe You are one of the best around and I am here to learn so can you plz guide me to improve my code??

This article has been dead for over six months. Start a new discussion instead.