Problem with string question

I've read the string tutorial on this site and i understand how most thing in c++ works however, the following question is giving me problems. I don't want answers just some guidance as to where i should be heading . Thanks.

Question:
Write a c++ program that reads 2 words, stores them in variables of string data type, and displays the shrotest length of these 2 words.

What I've done so far:

#include <isotream>
#include <string>
using namespace std;

int main ()
string mystring = "what's up"; // the 2 words as asked in question
cout << mystring;

now is where im stuck:(
What you've done is put two words (well, three if you don't count contractions) into a string and outputted it. That's not what the question is asking. What you want is to have two string variables inputted by the user (using cin). You need to find the length of each of these strings and output the shortest length (not the string but the length). Also, remember to begin and end your main function with brackets. This should get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <isotream>
#include <string>
using namespace std;

int main ()
{
   string word1, word2;

   //1. ask the user to enter the first word
   //2. read word1

   //3. ask the user to enter the second word
   //4. read word2

   //5. determine the length of word1

   //6. determine the length of word2

   //7. determine which word has the shortest length

   //8. output the shortest length

   return 0;
}

Everything shacktar said above is right, but don't use cin, use getline for std::strings like this:
 
getline(input stream, location to store, delimiting character);

In your case you'll probably want it set up like this:
1
2
getline(std::cin, word1, '\n');
getline(std::cin, word2, '\n');


Hint: I recommend using the length() function :)
Last edited on
after reading my book more and understanding what u guys said , here it goes:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string word1,word2;
cout << "Enter first word :";
cin>>word1;
cout << " Enter second word:";
cin>> word2;

int small = word1.length()<word2.length();

cout << "the shortest word is: "<< small <<endl;
return 0;
}
Your statement
 
int small = word1.length() < word2.length();

will return a boolean value. Therefore small will have a value of either 0 or 1. All you have to do is use if statements right? Just say
1
2
3
4
if(word1.length() < word2.length())
	std::cout << word1.length();
else
	std::cout << word2.length();


Also, did you just ignore everything I posted above? You don't want to get input for strings using cin. See my above post for how to use getline, it's the best way to get console input for strings.
Last edited on
Hello there, I am a student at UAT (I was told to put this). I was curious about why "cin" does not work as well as getline(). I have always used cin to set a string, so this is pretty new to me. Now the getline() uses "cin" in it, but also sets the place to store it. The only difference as far as I can tell is the delimiting character. I am guessing that it adds something to the end of the string?

for instance, getline(std::cin, word1, ' ') would be what you want if you are going to use the word in a sentence, right?
getline() will remove the delimiting character, whereas operator >> will leave it there.
Not quite. There are several problems with cin. It can lead to serious problems if the wrong data type is entered, and it leaves a newline character ('\n') in the input stream. This can cause problems when chaining input. Also, cin prevents you from more than one word. If I were to input "Hello, world!" and the data was collected using cin, then the string the data was stored in would only contain "Hello,". If you used getline instead, wherever the data was stored would contain the full message, "Hello, world!". Basically, avoid cin as much as you can, it's very inefficient and lead to lots of problems.

The delimiting character is the character that tells the program to stop reading from the input stream when it is found. The default is \n which represents a newline. For example with the following statement
 
getline(std::cin, randomString, '\n');

input would continue to be asked of the user until a newline character was found. Basically it means that it would keep reading until the user pressed the Enter or Return key on their computer. If I were to make this statement:
 
getline(std::cin, randomString, '*');

Input would continue to be read and stored into randomString even over multiple lines of input until a * was found. This means I can enter multiple lines like this:
Mary had a little lamb
Who's fleece was white
as snow*

And when the * was found in the third line input would stop. In your example of saying
 
getline(std::cin, word1, ' ');

This would read from the input stream until a space was found, so it has the opposite effect of what you said, only returning a single word.

Also while you said "Now the getline() uses "cin" in it." this is true, but you can replace it with any other stream. The most common alternative is to read from a file. For example if you opened a filestream called fileStream you could say the following:
 
getline(fileStream, line1, '\n');

This would retrieve the first line from the file.
Last edited on
Topic archived. No new replies allowed.