Comparing Values

I recently decided to try and learn how to make simple programs.

I found the tutorials on here and started running through them but I am having problems with this part of C++ right now.

I'm trying to figure out how to use a character or a string input from a user to exit a loop. Everything I do to compare two strings ends up with an error of some sort.

I figured I'd start of with a character then try to use a string.

Here is some code, maybe somebody could assist me.

Thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 // letter echoer

#include <iostream>
using namespace std;

int main ()
{
  char Letter;
  do {
    cout << "Enter a character: (x to exit) ";
    cin >> Letter;
    cout << "You entered: " << Letter << "\n";
  } while (Letter != "x");
  return 0;
}
Last edited on
Single quotes: 'x' is a character literal.
Double quotes: "x" is a string literal.

The simple rule is... if you have a single char, you use single quotes... if you have a string or char* (char pointer), you use double quotes.
closed account (ivDwAqkS)
 
while (Letter != 'x');//erase the double quotes and use single quotes 


That fixed my error on that one. Thanks.

This is where it gets hard for me, comparing stings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// string echoer

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

int main ()
{
  string Word;
  do {
    cout << "Enter a word: ('Exit' to exit)";
    getline (cin, Word);
    cout << "You entered: " << Word << "\n";
  } while (Word != "Exit");
  return 0;
}


edited, i think i need getline but it's still not working.


derp, i figured out my problem sorry. I had String capitalized.
Last edited on
Topic archived. No new replies allowed.