Jump to content

String comparison

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Macoder

Macoder

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
I've looked all over, and I've found several ways to do this, but none seem to work right. Here's what I'm at so far:

#include <iostream>

#include <vector>

#include <string>


using namespace std;


int main (int argc, char * const argv[]) {

    vector<string> words;

	int vectorlength = 0;

	

	string input;

	cout << "Enter \"done\" when done.\n";

	cin >> input;

	while (input.compare("done") != 0);

	{

		vectorlength++;

		words.resize(vectorlength, input);

		cin >> input;

	}

    return 0;

}


What I'm trying to do is store string values into a vector until the user enters done. But right now they can enter whatever they want and when they enter done, nothing happens. The cursor just moves to a new line. What's really strange is that if the first thing you enter is "done" and then you enter something else after it, the program quits.

Does anyone know why this is happening?

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

while (input.compare("done") != 0)[B][COLOR="red"][U];[/U][/COLOR][/B]

{

	vectorlength++;

	words.resize(vectorlength, input);

	std::cin >> input;

}


Might want to get rid of that semicolon. :)

Edited by dargueta, 14 July 2011 - 04:41 PM.

sudo rm -rf /

#3
Macoder

Macoder

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts

dargueta said:

Might want to get rid of that semicolon. :)

Oh. Duh. It works now. Is adding the semicolon some feature? Because it has a very unique way of functioning with it.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

Macoder said:

Oh. Duh. It works now. Is adding the semicolon some feature? Because it has a very unique way of functioning with it.

You've an infinite loop if you do not enter "done"

The semicolon will end the statement, so if input is not "done" then it will infinitely compare input to "done" and do nothing.

The braces afterwards {} are unique to C++, and will define its own scope. For example if a variable is defined within those braces it will deallocate afterwards. This is why if you enter "done" and then another thing it will quit, the code within the brace has finished.

It must be connected with the while() statement to have that code ran each loop, rather than just the comparison each time.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You can have the same problem with for and if statements as well - be careful!
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users