Thread: figured it out but have one question

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    figured it out but have one question

    I just did my 2nd one and I pretty much did it the same but I used the float A; then I used A as the cin>>. So of course it didnt work, I then put Char C as directed by soemone else and kept the same stuff and wow, it worked. So how come I had to use a Char C? I was reading but I'm still a little fuzzy on the details, it worked for digits, letters but what is the difference vs. int or float? Thanks

    Bryan Oh ya, here is my new code

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	char C;						
    
    
    	cout << "Enter any character then press enter:" << endl; 
    	cin >> C;											
    	
    	if 	(C>='A' && C<='Z')  
    												
    		cout << "The character you entered was a Upper-Case Letter" << endl;		
    		
    	else if	(C>='a' && C<='z')
    												
    		cout << "The character you entered was a Lower-Case Letter" << endl;		
    		
    	else if (C>='0' && C<='9')
    		cout << "The character you entered was a Digit" << endl;
    	
    	else											
    		cout << "The character you entered was none of the above" << endl;	
    	
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    since I am using a character from the keyboard it needs a char variable? Is that what I am reading? I am just making sure I am understanding it correctly? thanks

    Bryan

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    "char" holds any one single character. It can be a single digit, a letter, or another symbol entirely.

    "float" can only hold numbers, it could never hold a letter or anything else besides a number. So if you type a letter when you are trying to read a float, the read will fail. A float can never, ever hold anything besides a number.

    Same with an "int" -- it, too, can only hold numbers and nothing else.

    Essentially, in your program, you were trying to determine if a CHARACTER was a letter, number, or other, so you wanted to read a char. If you had tried to read an int, and the user had entered "C", your variable would NOT hold 'C', it would hold an unknown integer (the read would fail and the number might be anything, but it will always be a number).

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I understand that now, and I remember that back in the first 2 chapters..thanks..I do have one more question, sorry. Instead of putting c>= for my statements I could have put inputChar >=...I take it these two mean the same thing?

    Bryan

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you called the variable inputChar, then you'd use that; C is just a variable name, you could call it "sam" if you want and it would work just fine.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    ok..so if I put char InputChar; then that means InputChar is the variable and if I put char Testing; then testing would be the variable. I think I got it. I appreciate all the help everyone gives me.

    Bryan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  3. question once again..
    By sunnycyboid in forum C Programming
    Replies: 1
    Last Post: 11-08-2002, 04:57 AM
  4. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM