Am I on the right track

This is a discussion on Am I on the right track within the C++ Programming forums, part of the General Programming Boards category; This is my second assignment and I need to determine if the letter inputed is a Upper-Case, Lower-CAse, Digit, or ...

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

    Am I on the right track

    This is my second assignment and I need to determine if the letter inputed is a Upper-Case, Lower-CAse, Digit, or none of the above...can someone tell me if I am on the right track here? thanks

    Bryan

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	float A;						
    
    
    	cout << "Enter any character then press enter:" << endl; 
    	cin >> A;											
    	
    	if 	(statement1)
    												
    		cout << "The character you entered was a Upper-Case Letter" << endl;		
    		
    	else if	(statement2)
    												
    		cout << "The character you entered was a Lower-Case Letter" << endl;		
    		
    	else if (statement3)
    		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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You should be reading into an integer. Then use functions like "isalpha", "isdigit", etc to test the key read. You could read into a string and test that way.

    You don't want to read keystrokes into a floating point number.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Look at the commented lines of where I edited your code. I did the first statement for you, look at http://www.asciitable.com for info on the rest...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	int A;	//int not float					
    
    
    	cout << "Enter any character then press enter:" << endl; 
    	cin >> A;											
    	
    	if 	(A>=65 && A<=90)  //because ascii characters 65 through 90 are Upper Case letters.
    												
    		cout << "The character you entered was a Upper-Case Letter" << endl;		
    		
    	else if	(statement2)
    												
    		cout << "The character you entered was a Lower-Case Letter" << endl;		
    		
    	else if (statement3)
    		cout << "The character you entered was a Digit" << endl;
    		
    		else											
    		cout << "The character you entered was none of the above" << endl;	
    
    
    return 0;
    }

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Actually, if the user typed a character it will not work. You need to read a variable of type char, not int. If you were reading an int, and you typed "C", it would fail the read, it would NOT return the ASCII code for C.

    Here's the first part:

    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;

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    romeoz, I must insist that your subject lines are specific, and you do not give us homework to solve.

    Rules

    You have not specified an actual problem or question, you just asked people to look at your code. You'll get better responses if you hone down a little on the problem. Ie, does your program give the desired output? If not, what is the output? Try debugging a bit as well, so you can be specific about which code is giving you trouble.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by bennyandthejets
    You have not specified an actual problem or question, you just asked people to look at your code.
    Actually, the question has been asked:

    "Am I on the right track?"

    And answered:

    "Yes, but don't read into a float when you're trying to read a character."



    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I am not trying to get soemone to do my homework at all...I take online classes and the Prof does not help much so I found this site and people that will help. I just want to know if I am doing it right if not maybe direct me towards the right way, I dont want people doing my homework, then I will never learn, I just need help since this is my first programming class and its online. If it sounded like I am trying to get my assignment done by people then I apoligize for that because that is not my intentions.

    Bryan

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Quzah, asking if "I'm on the right track" is not really a good question, even if it is grammatically a question. The nature of the desired program means that romeoz could have asked a much more specific question. Ie, he/she could have attempted to run the program, then stated what went wrong, if anything did. It saves time for everybody, including romeoz.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    well I can tell you what wrong I first put for statement 1 if (a > Z) that would equal Upper Case, and I did one for lower case and digit and I found out they are all Uppercase answers, so I figured my statements were wrong so I thought I would post that code without statements to see if I was even in the right direction.

    Bryan

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by bennyandthejets
    Quzah, asking if "I'm on the right track" is not really a good question, even if it is grammatically a question. The nature of the desired program means that romeoz could have asked a much more specific question. Ie, he/she could have attempted to run the program, then stated what went wrong, if anything did. It saves time for everybody, including romeoz.
    Usually I'd agree. However, you're just being a whiner. The question had been answered three times by the time you showed up. It isn't "wasting your time" to answer a question that has already been answered.

    There was no need to be specific in this case. They presented their take on it, and since it was small enough to fit on a single page, it was easily taken in in a glance.

    With said glance, it was obvious where the potential problem would occur, and the answer was given.

    They didn't need to give an error message, because the question wasn't about an error. The question was really on theory. "Is this the right approach to this problem?"


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Lighten up a bit. He just asked for a general yes/no answer essentially, and unlike many, he was upfront about the fact that it was an assignment. I take it the intent was simply so he'd know what part of the program to put his efforts into, and there's nothing wrong with that.

    Anyways, you are on the right track. Another possibility is reading in a string (std::string preferably, or char*), and then using functions from <cctype> such as isalpha(), et al. (Note, these use char*'s, so to work with std::string, you'd have to use the c_str() member function.)

    Should be able to find references to all of these functions here: http://cppreference.com/

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    oops I am sorry, I dont know what I was thinking writing int .

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by o0obruceleeo0o
    oops I am sorry, I dont know what I was thinking writing int .
    I know what I was thinking: C. In C, all character based functions (other than character pointer based functions) take integers rather than characters, because they test for things like EOF.

    In C++ it doesn't work because the way the operators are overloaded. It sees an integer and wants an actual number.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to track every copy event
    By manav in forum Windows Programming
    Replies: 3
    Last Post: 06-05-2008, 11:37 AM
  2. Keeping track of words inputted
    By death_coder in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2006, 09:05 PM
  3. Keeping track of static external structure
    By pwilfred in forum C Programming
    Replies: 6
    Last Post: 03-13-2003, 06:23 PM
  4. F1 track analyser
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 09:48 AM
  5. keeping track of # of files
    By blind_collision in forum C Programming
    Replies: 1
    Last Post: 10-13-2001, 10:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21