LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-08-2004, 05:18 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
C++ boolean in cout statement


Hey all

Well i now kinda know C and Java (quite a bit of both) and at the moment i'm thinking that C++ might even be an option so i'm doing a bit of playing around... I don't want to start another poll about it, in my opinion each has a use.

One thing i'm used to in both C and Java is boolean expressions in "out" statements, i.e.
System.out.println(getMessage < 1 ? "Nothing" : "Something");

This is what i've got with the game that i'm recreating in C++ (A thing I do when I start a language):

Code:
#include <iostream>
#include <time.h>
                                                                                                                               
using namespace std;
                                                                                                                               
int main(int argc, char *argv[]) {
  int userGuess = 0;                            // Storage for the users guess
  int lowerGuess = 0, higherGuess = 100;        // Limits for guesses
  int guessCount = 1;
                                                                                                                               
  srand(time(0));                               // Initiate the random number
  int randomNumber = 1 + rand() % 100;          // Get random number
                                                                                                                               
  cout << "RN: " << randomNumber << endl;
                                                                                                                               
  do {
    cout << "Min: " << lowerGuess << "\tMax: " << higherGuess << endl;
                                                                                                                               
    cout << "Please enter your guess: ";
    cin >> userGuess;
                                                                                                                               
    if(userGuess < randomNumber) {
       if(userGuess <= lowerGuess && lowerGuess != 0) {
         cout << "### Ignored: Your lowest is already " << lowerGuess <<" ###" << endl;
       } else {
         lowerGuess = userGuess;
         ++guessCount;
         userGuess = 0;
       }
    } else if(userGuess > randomNumber) {
      if(userGuess >= higherGuess && higherGuess != 0) {
        cout << "### Ignored: Your highest is already " << higherGuess << " ###" << endl;
      } else {
        higherGuess = userGuess;
        ++guessCount;
        userGuess = 0;
      }
    }
  } while(userGuess != randomNumber);
                                                                                                                               
  cout << "You got it in " << guessCount << (guessCount < 2) ? " tries :)" : " try :)" << endl; 
                                                                                                                               
return 0;
}
So is this possible in C++? When I compile it I get:
game.cpp: In function `int main(int, char*)':
game.cpp:41: error: invalid operands of types `const char[8]' and `<unknown
type>' to binary `operator<<'

Last edited by AMMullan; 06-08-2004 at 05:20 AM.
 
Old 06-08-2004, 07:40 AM   #2
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
Change this line:
Code:
cout << "You got it in " << guessCount << (guessCount < 2) ? " tries " : " try " << endl;
To this:
Code:
cout << "You got it in " << guessCount << ((guessCount < 2) ? " try" : " tries") << endl;
 
Old 06-08-2004, 12:44 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Original Poster
Rep: Reputation: 30
Well that was easy - thanks kooch
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
boolean question! xemous General 3 03-18-2005 03:40 AM
C++ compiler handling of boolean variables R00ts Programming 2 02-16-2005 06:32 PM
unknown boolean httpd_tty_comm philio Fedora 2 02-06-2005 09:48 AM
Unknown boolean error mebrelith Fedora 4 01-24-2005 03:47 AM
Boolean Algebra question gdbugger General 5 10-23-2003 06:54 PM


All times are GMT -5. The time now is 02:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration