Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,338 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,785 people online right now. Registration is fast and FREE... Join Now!




where have i gone wrong

 
Reply to this topicStart new topic

where have i gone wrong

missy 69
post 1 Apr, 2008 - 08:53 PM
Post #1


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 22

enter 3 word show largest smallest middle to screen

cpp


char word [20];
char word2[20] ;
char word3 [20];


cout << "enter a word "<< endl ;
cin >> word ;

cout << " enter a word " << endl;
cin >> word2;

cout << " enter a word " << endl;
cin >> word3;


if ((word < word2) && (word < word3))
{
cout <<word <<" is the smallest." <<endl;

if ( word2 < word3)
{
cout << word3 << " is the biggest word "<< endl;
cout << word2 << " is in the middle "<< endl;
}
else
{
cout << word2 << " is the biggest word "<< endl;
cout << word3 << " is in the middle "<< endl;
}
}
else if (word2< word3)
{
cout <<word2 <<" is the smallest." <<endl;

if ( word < word3)
{
cout << word3 << " is the biggest word "<< endl;
cout << word << " is in the middle "<< endl;
}
else
{
cout << word << " is the biggest word "<< endl;
cout << word3 << " is in the middle "<< endl;
}
}
else
{
cout <<word3 <<" is the smallest." <<endl;

if ( word < word2)
{
cout << word2 << " is the biggest word "<< endl;
cout << word << " is in the middle "<< endl;
}
else
{
cout << word << " is the biggest word "<< endl;
cout << word2 << " is in the middle "<< endl;
}
}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 1 Apr, 2008 - 09:13 PM
User is offlineProfile CardPM

Go to the top of the page


bodom658
post 1 Apr, 2008 - 08:58 PM
Post #2


D.I.C Head

**
Joined: 22 Feb, 2008
Posts: 113



Thanked 4 times
My Contributions


/you cannot directly compare strings using logical operators, like >, <, !=, etc.

You will want to use the syntax strlen(char[]) to compare the strings of characters. This function returns a decimal integer equal to the number of characters in the string so logical comparisons are fine. just make sure you edit your code like so:

cpp

if (strlen(word) < strlen(word2))
cout << word1 << " is smaller";


get it?

This function is included in the <iostream> library.

This post has been edited by bodom658: 1 Apr, 2008 - 09:15 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 1 Apr, 2008 - 08:59 PM
Post #3


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,268



Thanked 54 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


Without knowing what the problem is... I would take a guess that it's a typo in defining your variables.

CODE

char word [20];  
har word2[20];  // <-- This might be your proverbial "monkey wrench in the gears"
char word3 [20];  
User is online!Profile CardPM

Go to the top of the page

Martyr2
post 1 Apr, 2008 - 09:05 PM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,007



Thanked 169 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Sorry that was a bug in the conversion of tags, he had the "c" there.

I am still trying to figure out what is exactly wrong with this. A good problem description would be nice in this instance. smile.gif
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 1 Apr, 2008 - 09:10 PM
Post #5


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,268



Thanked 54 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


cpp

# out << "enter a word "<< endl;
# cin >> word;
#
# out << " enter a word " << endl;
# cin >> word2;
#
# out << " enter a word " << endl;
# cin >> word3;

I think you chopped a few instances of the letter c.
User is online!Profile CardPM

Go to the top of the page

nirvanarupali
post 1 Apr, 2008 - 09:10 PM
Post #6


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 982



Thanked 2 times

Dream Kudos: 375
My Contributions


What do u mean by "enter 3 word show largest smallest middle to screen"?

You mean the length of each words?
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 1 Apr, 2008 - 09:14 PM
Post #7


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,007



Thanked 169 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


QUOTE(no2pencil @ 1 Apr, 2008 - 10:10 PM) *

cpp

# out << "enter a word "<< endl;
# cin >> word;
#
# out << " enter a word " << endl;
# cin >> word2;
#
# out << " enter a word " << endl;
# cin >> word3;

I think you chopped a few instances of the letter c.


Yeah that is a weird bug in the editor. His formatting was just right so that when it went to the parser it chopped out the letters from the display. They were there all the time though.

confused.gif
User is offlineProfile CardPM

Go to the top of the page

missy 69
post 1 Apr, 2008 - 09:26 PM
Post #8


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 22

QUOTE(nirvanarupali @ 1 Apr, 2008 - 10:10 PM) *

What do u mean by "enter 3 word show largest smallest middle to screen"?

You mean the length of each words?

yes i do mean the lenght of each word but not the actual size i just need to print to screen smallest 1st
largest 2nd 3rd middle word
User is offlineProfile CardPM

Go to the top of the page

missy 69
post 2 Apr, 2008 - 12:43 PM
Post #9


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 22

QUOTE(bodom658 @ 1 Apr, 2008 - 09:58 PM) *

/you cannot directly compare strings using logical operators, like >, <, !=, etc.

You will want to use the syntax strlen(char[]) to compare the strings of characters. This function returns a decimal integer equal to the number of characters in the string so logical comparisons are fine. just make sure you edit your code like so:

cpp

if (strlen(word) < strlen(word2))
cout << word1 << " is smaller";


get it?

This function is included in the <iostream> library.





Do i need to apply if (strlen(word) < strlen(word2)) to every condition i have set or just the first
User is offlineProfile CardPM

Go to the top of the page

KYA
post 2 Apr, 2008 - 03:22 PM
Post #10


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,148



Thanked 47 times

Dream Kudos: 1150
My Contributions


Ideally you would loop through all the strings and compare, otherwise just comparing two isn't going to solve the problem of the smallest, largest and the one in the middle
User is online!Profile CardPM

Go to the top of the page

baavgai
post 2 Apr, 2008 - 04:41 PM
Post #11


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,944



Thanked 94 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


First off, there is a string compare, it's standard.

Given that, the code becomes much easier. To avoid confusion, I'll offer a complete solution, since the initial code basically has all these elements.

cpp

#include<iostream>

using namespace std;

// our standard results, by putting it in a function
// we simplify out order output
void showResults(string &smallest, string &middle, string &biggest) {
cout << smallest <<" is the smallest." <<endl;
cout << biggest << " is the biggest word "<< endl;
cout << middle << " is in the middle "<< endl;
}

// where the logic happens
// I followed the original
void testWords(string &word, string &word2, string &word3) {
if ((word.compare(word2) < 0) && (word.compare(word3) < 0)) {
if (word2.compare(word3) < 0) {
showResults(word, word2, word3);
} else {
showResults(word, word3, word2);
}
} else if (word2.compare(word3) < 0) {
if (word.compare(word3) < 0) {
showResults(word2, word, word3);
} else {
showResults(word2, word3, word);
}
} else {
if (word.compare(word2) < 0) {
showResults(word3, word, word2);
} else {
showResults(word3, word2, word);
}
}
}

// we do this three times, it wants a function
void getWord(string &word) {
cout << "enter a word "<< endl ;
cin >> word;
}

// a short main is a happy main
int main() {
string word, word2, word3;
getWord(word);
getWord(word2);
getWord(word3);
testWords(word, word2, word3);
return 0;
}

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/19/08 03:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month