Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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
 
Old May 12th, 2004, 04:36 PM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to HateMe
Default Taking both numbers and letters as input

Hello.

I have this simple program, but it won't take in letters as input. I know you use the char for letters, and int for numbers, just for an example.

Here is my program:

// Test program

#include <iostream>
using namespace std;

int main()

{

int hash = 0;

cout << endl

     << "Write the first letter or number: ";

cin >> hash;

cout << endl;


if(hash == 1)

    cout << endl

         <<"Write the second letter or number: ";

cin >> hash;

cout << endl;

{

if(hash == 'a')


       cout << "The first letter is a" << endl;

}


return 0;

}

So if the user first types 1, then afterwards types a, the program outputs: The first letter is a. The problem is that the program doesn't like the if(hash == 'a') part. When i run the program, and type in 1, and then when i type in the a, the program just ends, doesn't go any further. If i change the if(hash == 'a') to if(hash == 2) it works.
 
Old May 13th, 2004, 11:33 PM
Authorized User
 
Join Date: May 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello.
   I don't think the problem is that the program doesn't like the if(hash == 'a') part.If you first types 1, then afterwards types 97,I think the program will like it.
   I think the reason is that cin refuse to give the value of char to a int number,but the '=' can.So when you input a char,cin give 0 to the int number and when we input 97,the value of "hash=='a'" is not zero,is true.We can use hash = 'a' to give hash a valid value,it's 97,but we can't use cin>>hash to do this.
  Try this:
  type 1.2,then see the value of hash is what.It's 1.
  The reason is that to float number,'=' and cin both convert it to a int number and give the result to hash.

  Clear?We can't use cin>>hash to give a char's value to hash,we can only use it to give a int number's value to hash.


niuwei





Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare numbers and letters in same cell EricB123 Excel VBA 1 January 21st, 2007 03:30 PM
Latin letters itHighway Classic ASP Professional 3 July 20th, 2006 12:14 AM
Remove letters from numbers Corey Access 7 December 18th, 2005 09:09 PM
fillin array with letters from a to Z and numbers sajid C# 10 May 3rd, 2005 03:38 PM
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.