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!
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!
This post has been edited by Martyr2: 1 Apr, 2008 - 09:13 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.
This post has been edited by bodom658: 1 Apr, 2008 - 09:15 PM
# 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.
# 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.
/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
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
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; }