With my console program, I use iostream.h, and therefore cin >> number. However, how would I be able to use words/characters instead of numbers with that, or would I need something aside from iostream?
With my console program, I use iostream.h, and therefore cin >> number. However, how would I be able to use words/characters instead of numbers with that, or would I need something aside from iostream?
nothing really..
Code:#include <iostream.h> char word[20]; // that makes a variable that can hold letters and makes it 20 spaces long int main() { cout << " Type a word under 20 spaces: "; cin >> word; cout << endl; cout << " You typed: " << word; cin.get(); // pauses the program until some one types a key and hits enter return 0; }
What is C++?
Ok got that working... but how do I use it in an if statement? Right now I have something like this:
However, that doesn't work.Code:cin >> word; if(word == "quit") { Code to end loop(not shown here) goes here. }
try a whil loop
Code:while ( word != "quit" ) { cout << "type: "; cin >> word; cout << word; }
What is C++?
I fail to see how a while loop would succeed where an if statement fails...
The problem is not the statement...
If I have declared word to be an integer, the preceding section of code would have worked. However, when I change it to the char type, it does not.Code:cin >> word; if(word == 1) { Code to end loop(not shown here) goes here. }
Instead of unsing
useCode:cin >> word; if(word == 1) { Code to end loop(not shown here) goes here. }
Code:#include <stdio.h> #include <iostream.h> int main() { char word[20]; cout<<"Enter a word : "; cin>> word; if(strcmp(word, "quit")) //strcmp compares two strings //it compares the char word with //quit, just don't forget to include stdio { return 0; } return 0; }
Oi Oi Oi!!!!
you could also using string.h for strcmp
It doesnt work... as in it recognizes strcmp, but it doesn't work...
That right?Code:if(strcmp(command, "quit")) { running = false; }
Try either:
Or:Code:if (strcmp(command, "quit") == 0) { running = false; }
Code:if (!strcmp(command, "quit")) { running = false; }
Attention! Use the string class instead, it supports strings with all relational operators. Thus,
works just fine!Code:string word; cin >> word; if(word == "quit") { //do whatever you want }
string is unidentified blah blah blah...
Try putting this at the top (use string, not string.h).
Code:#include <string> using namespace std;
string is good, but compilers like to b*tch about string a lot for some reason