wow, I had no idea this post was still ongoing lol. Here is my code, yours is definitely better esbo.
Code:#include <iostream> #include <string> #include <vector> #include <conio.h> using std::cout; using std::cin; using std::endl; using std::vector; using std::string; int main(){ //asks for list of words cout << "Please give me a list of words:" << endl; string x; vector<string> words; //adds the input to the vector while (cin >> x){ words.push_back(x); } //checks to see if user inputted anything if (words.size() == 0){ cout << "I didn't receive any input." << endl; cout << "Press any key to continue..."; _getch(); return 1; } //the containers string longest = words[0]; string shortest = words[0]; //checks for the shortest and longest string for(int i = 1; i != words.size(); i++){ if(words[i].length() < shortest.length()){ shortest = words[i]; } if(words[i].length() > longest.length()){ longest = words[i]; } } //the output cout << "Shortest: " << shortest << endl; cout << "Longest: " << longest << endl; cout << "Press enter to continue..."; _getch(); return 0; }
I got my 'big' C program working in C++, it seems the 'bug' already existed ( I had done a little processing when I should have checked for end of file first) however it never caused any harm to the C program, however as I had rearranged the position of some functions to get it to compile I think that may have made a harmless action dangerous.
Any by converting it to compile with C++ I uncovered 2 hide bugs, which can't be a bad thing.
I might write the output section in C++ (cout) to make it easier to modify but I am not familiar with the print format of cout yet, if it's the same as printf Iprobably won't bother.
hey guys i have a question, if we also want to output the length of the longest/shortest input, is it just cout << longest/shortest.length? sorry, its probably a noob question..
longest.length() will give you the length of the string, yes.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes, it is shortest/longest.length().
I might write the output section in C++ (cout) to make it easier to modify but I am not familiar with the print format of cout yet, if it's the same as printf Iprobably won't bother.
I/O stream format flags.
You may want to start a separate thread on this. In any case formatting with streams is completely different.
It is awkward to a C programmer, but it is type-safe. E.g you simply can't make the following mistake with std::cout:
If the awkwardness bothers you (formatting output with format strings is just too convenient) you can download boost and use Boost.Format which gets you the best of both worlds: you can use format string but in a type-safe way.Code:int n = 10; prinf("%s", n);
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
thanks..also i was wondering wat does this part do: _getch()
I used _getch() because my compiler closed my cmd window everytime i ran my programs. You can remove that and the cout above it. Don't use it if possible...I read its not exactly industry standard, and affects performance...but atm I'm only running small programs so whatever keeps my cmd window open! =] Its nice to know youre learning c++ too jewelz. Maybe we can learn from each other!
Basically it reads a key from the keyboard, without echoing it to the screen so it is effectively, press any key to continue. It is unbuffered so you don't have to press carridge
return.
When you program exits the window will close so you need something to stop it closing.
I usually run mine from a batch file wiith a pause in it to keep the window open.
I don't think _getc will effect performance at all as it is just waiting for a signal that a key has been pressed.
Last edited by esbo; 01-28-2009 at 09:25 AM.
hey guys, jus wanna say thanks for the help. i really appreciate it =)
ok this is probably the most retarded post ever, but how do i make the output come out within quotation marks?
for example if input was: Hello I am nice.
output:
Longest: Hello
Shortest: I
but i need it to be:
Longest: "Hello"
Shortest: "I"
Code:cout << "Shortest: " << "\"" << shortest << "\"" << endl; cout << "Longest: " << "\"" << longest << "\"" << endl;