Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 14th, 2005, 07:49 PM
Registered User
 
Join Date: Jul 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default "C++ lottery" with letters and words

I'm reading Wrox's "Beggining programming" and in chapter seven the author brings some code that works perfectly, both in the compiler and the .exe:

Code:
#include <iostream.h>
void main()
    { 
      int x;
      cout << "Pick an integer: ";
      cin >> x;
      if (x == 7)
      {
        cout << "You win the C++ lottery!" << endl;
        cout << "Thank you for playing the C++ lottery" << endl;
      }
      else
      {  
        cout << "Thank you for playing the C++ lottery" << endl;
      }
    }


My question is, how it is possible to write the same mini-program but picking up a letter instead of a number. Let's say, that by picking up the letter "a" or the word "lottery" I win the "C++ lottery". I tried to write this code (see bold type)

Code:
#include <iostream.h>
void main()
    { 
      int x;
      cout << "Pick a word: ";
      cin >> x;
      if (x == lottery)
      {
        cout << "You win the C++ lottery!" << endl;
        cout << "Thank you for playing the C++ lottery" << endl;
      }
      else
      {  
        cout << "Thank you for playing the C++ lottery" << endl;
      }
    }


And the compiler found this code mistaken . Any suggestions on how to code this properly?

Thank you for your time :D. I would appreciate very much an answer to this .
Reply With Quote
  #2 (permalink)  
Old July 15th, 2005, 01:16 AM
Authorized User
 
Join Date: Jul 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to kumar_raj13
Default

For 'a' you have to use data type char instead of int.

Like


char i;
cout<<"gfgfgfsgs";
cin>>i;
if(i=='a')


and then it will be ok.

rajkumar
Reply With Quote
  #3 (permalink)  
Old July 15th, 2005, 12:07 PM
Registered User
 
Join Date: Jul 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello.

I tried to implement the code the way that you told me, but still doesn't work:

Click on the image on the center to see it properly:
http://img350.imageshack.us/my.php?image=lottery4ib.jpg

Does anybody has a suggestion? It sounds like a simple C++ order or command that I'm missing...

Thank you nevertheless for your time.
Reply With Quote
  #4 (permalink)  
Old July 16th, 2005, 03:02 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

This works OK. Declare x as a char array of 10. Since the word lottery is a string and not a character it must be enclosed in double quotes (").

A better solution would be to define x as a string type.

#include <iostream.h>
void main()
    {
      char x[10];
      cout << "Pick a word: ";
      cin >> x;
      if (x == "lottery")
      {
        cout << "You win the C++ lottery!" << endl;
        cout << "Thank you for playing the C++ lottery" << endl;
      }
      else
      {
        cout << "Thank you for playing the C++ lottery" << endl;
      }
    }

Keep reading the book :-)

Alan


Reply With Quote
  #5 (permalink)  
Old July 16th, 2005, 11:25 PM
Registered User
 
Join Date: Jul 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Alan:

I copied and pasted your code into my compiler and the compiler still shows me some errors :

Click on the image to see it full size:

http://img312.imageshack.us/my.php?i...ottery24pr.jpg

Sounds that you're, however, pretty close to the real solution.... anybody any other solution?

Thank you for your time nevertheless, as I appreciate very much all your input. :D

Have a gr8 day.:D
Reply With Quote
  #6 (permalink)  
Old July 17th, 2005, 02:16 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

The code I posted works OK under Borland Turbo C++ version 3.01 You do not say what errors you get. You do not say what version of C++ compiler you are using.

I changed "void main()" to "int main()" and the program ran under DevC++ version 4.9.9.2

Check that you have not made any typing mistakes.

From your posting I think that you need to do a lot more reading before you try to write any programs. The errors in your code are very basic and you should not expect other people to solve problems that are obviously due to your lack of trying.

What is the point of posting URLs to see your code - being suspicious I have not clicked on the address and I would not recommend any one else to do so. It is better that you post the code that you are trying to compile otherwise you waste other people's time.

Alan


Reply With Quote
  #7 (permalink)  
Old July 17th, 2005, 08:42 AM
Registered User
 
Join Date: Jul 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello:

First of all, let me once again thank you for your time and reply, as I appreciate it very much.

Now, let me answer all your questions one by one:

- I'm using the DevC++ version 4.9.9.2 compiler. It is the only one, so far, that I was able to use in my PC, which runs under the Windows XP OS

- The compile log says: error: "main' must return "int'

- My point in posting the screenshot of the code on the compiler, is to show that I'm not just randomly saying that I couldn't make it work, but that I took the time to implement the solution that was suggested. Also, to show the Compile Log and what errors did it find.

But there's something that I didn't get: in what sense is "suspicious" my posting a URL?

I'm here to learn and I'm a total begginer, I was just curious about how to code that same program with words.

Again, thank you very much for your time and dedication, as I appreciate it very much .

Sincerly,

dovids
Reply With Quote
  #8 (permalink)  
Old July 19th, 2005, 01:21 AM
Authorized User
 
Join Date: Jul 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to kumar_raj13
Default

Hello Friend
Try the Code Written Below.OK

#include <iostream.h>
#include<string.h>
void main()
    {
      char[10] x;
      cout << "Pick a word: ";
      cin >> x;
      if (strcmp(x,"lottery"))
      {
        cout << "You win the C++ lottery!" << endl;
        cout << "Thank you for playing the C++ lottery" << endl;
      }
      else
      {
        cout << "Thank you for playing the C++ lottery" << endl;
      }
    }
Reply With Quote
  #9 (permalink)  
Old July 19th, 2005, 03:11 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

My appologies for my suspicions - I dont like being redirected to unknown sites.

Good Luck!!

Alan

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Lottery Program Help...I'm using 2005 Visual Basic psycoblaster VB.NET 2002/2003 Basics 13 May 23rd, 2007 09:13 PM
Lottery Winners Program Urgent Help Needed! Honour C++ Programming 2 May 19th, 2007 04:47 AM
writing a lottery program in C++ captlem66 VB.NET 2002/2003 Basics 2 October 13th, 2004 02:50 AM
Visual basic.net (Lottery program) Brettvan1 VB.NET 2002/2003 Basics 10 October 1st, 2004 08:26 AM
Help with letters ittorget Classic ASP Basics 3 October 14th, 2003 01:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.