Homework help needed. cin.get issues.

This is a discussion on Homework help needed. cin.get issues. within the C++ Programming forums, part of the General Programming Boards category; I'm taking an entry level C++ programming course and I'm totally stuck on this last homework assignment. You're supposed to ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    37

    Homework help needed. cin.get issues.

    I'm taking an entry level C++ programming course and I'm totally stuck on this last homework assignment. You're supposed to take an unsigned integer into a variable of type unsigned short int. I'm not allowed to use "cin >>" inside the program.

    Everything works with my code, it just doesn't output the correct answer. If the user inputs a number, its supposed to output the number. However, I only get the first digit. I skip the white spaces and give an error if user inputs a character, but when everything is okay I get hung up during my last while statement.

    Code:
    // Name: David Hart
    // Date: 04/07/2010
    // Class: C101 MW 3:00
    // Homework#: Ten
    // Source File: HW10problem1.cpp
    // Action: Recreate the extraction operator.
    
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    
    int ReadInt(unsigned short int&);
    
    
    void main () 
    {
      char Continue;
      unsigned short int UserInput = 0;
      int Error;
    
      do
      {
        cout << "This program will immitate the extraction operator as used on integers." << endl;
        Error = ReadInt(UserInput);
    	           
        if (Error == 1)
          cout << "Error code 1:  Please enter a number, not a character.";
        else if (Error == 2)
          cout << "Error code 2:  The number must be less than 65535.";
        else if (Error == 0)
          cout << UserInput;
    
      cin.putback(UserInput);
      cin.ignore (100, '\n');
    
      cout << "\n\nWould you like to continue?  N or n to stop ==> ";
      cin >> Continue; 
      		
      cin.putback(UserInput);
      cin.ignore (100, '\n');
    
      UserInput = 0;
     }
      
      while
        ((Continue != 'N') && (Continue != 'n'));
    
    }
    
    
    int ReadInt(unsigned short int &UserInput)
    {
      char ch;
      
      cout << "\nEnter a number ==> ";
      cin.get(ch);
    
      while (ch == ' ')
        ch = cin.get();
      
      if (isdigit(ch) == 0)
        return 1;
    
      while (isdigit(ch) == 1)
        ch = cin.get();
        int X = ch - '0';
        
      if ((ch * 10) + X > 65535)
        return 2;
       
      else 
        UserInput = UserInput * 10 + X;
        cout << UserInput;
        return 0;
       }
    Thanks in advance for any help and sorry if its confusing : /
    Last edited by DHart07; 04-13-2010 at 09:55 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I like your indentation. However, your indentation needs to be supported by open-curly-braces and close-curly-braces. Just because you have several lines all indented underneath a while statement does not mean that they all belong to the while statement. (And you'll want to be careful about your current setup, which appears to throw away the first digit.)

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    if i change it to

    Code:
    while (isdigit(ch) == 1)
      {ch = cin.get();
    	int X = ch - '0';
      
    	if ((ch * 10) + X > 65535)
    	  return 2;
    	else 
    	  UserInput = UserInput * 10 + X;
              cout << UserInput;
    	  return 0;
      } }
    i get a "warning C4715: 'ReadInt' : not all control paths return a value" warning and if i run the program i get nothing shown on the console for my UserInput.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You have a lot of conditionals (if, while etc) that aren't using any braces. This will most likely lead to some strange things happening, especially if you expect your code to behave as you have indented. My advice is that you always use braces when using conditionals.

    Also, use int main instead of void main.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DHart07 View Post
    if i change it to

    Code:
    while (isdigit(ch) == 1)
      {ch = cin.get();
    	int X = ch - '0';
      
    	if ((ch * 10) + X > 65535)
    	  return 2;
    	else 
    	  UserInput = UserInput * 10 + X;
              cout << UserInput;
    	  return 0;
      } }
    i get a "warning C4715: 'ReadInt' : not all control paths return a value" warning and if i run the program i get nothing shown on the console for my UserInput.
    Repeat previous comment. And then you have to decide where things should go -- after all, this while loop, as you have it indented (although not the way you have it actually written with braces) will never read any extra digits, since you return (from the function) either way that if-check happens, so the while loop can never actually loop.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    I see what you mean about having my return too early. I notice that when I run through the debugger it skips over the last while statement, and doesn't loop through. When I run the program if I put any whitespaces it skips them. If I put a character in first, it errors out like it should, but if I type in "343" i currently get just "0". Am I running my "isdigit" check wrong?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Technically, isdigit is not required to return 1 (it can return any nonzero number), so you may want to be less restrictive. (You can just do "if (isdigit(ch))".) (For instance, my Ubuntu system returns 2048 for isdigit('0'). There's probably a reason why, I just don't know what it is.)
    Last edited by tabstop; 04-13-2010 at 10:32 PM.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Good call on isdigit not returning a 1. I originally had it as != 0 but the tutor at school told me a 1 : / I made some changes to everything and this is what I currently have.

    Code:
    // Name: David Hart
    // Date: 04/07/2010
    // Class: C101 MW 3:00
    // Homework#: Ten
    // Source File: HW10problem1.cpp
    // Action: Recreate the extraction operator.
    
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    
    int ReadInt(unsigned short int&);
    
    int main () 
    {
      char Continue;
      unsigned short int UserInput = 0;
      int Error;
    
      do
      {
        cout << "This program will immitate the extraction operator as used on integers." <<  endl;
        Error = ReadInt(UserInput);
    	           
        if (Error == 1)
          {cout << "Error code 1:  Please enter a number, not a character.";}
        else if (Error == 2)
          {cout << "Error code 2:  The number must be less than 65535.";}
        else if (Error == 0)
          {cout << "\nNo errors.";}
    
        cout << "\n\nWould you like to continue?  N or n to stop ==> ";
        cin >> Continue; 
      		
        cin.ignore (100, '\n');
    
    	UserInput = 0;
      }
      
      while
        ((Continue != 'N') && (Continue != 'n'));
    }
    
    
    int ReadInt(unsigned short int &UserInput)
    {
      char ch;
      
      cout << "\nEnter a number ==> ";
      cin.get(ch);
    
      while (ch == ' ')
        {ch = cin.get();}
      
      if (isdigit(ch) == 0)
        {return 1;}
    
      while (isdigit(ch) != 0)
      {
       ch = cin.get();
       int X = ch - '0';
      
       if ((ch * 10) + X > 65535)
       return 2;
       
       else 
       UserInput = UserInput * 10 + X;
      }
      
      cout << UserInput;
      return 0;
    }
    And it seems to run alot smoother, at least it runs and spits out more than a digit or a 0 at least. But when if I enter in a 5, as I'm watching the debugger my "ch" gets changed at line "int X = ch - '0';" after that my UserInput ends up getting all jacked up. Any tips? And thanks for the help so far by the way.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Generally, when you see a highlighted line in your debugger, the program just finished the line above that line. And the line above that line is
    Code:
    ch = cin.get();
    which seems like a very good reason for ch to suddenly become something else.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    ch is what the user types in.
    int X = ch - '0'; is used to change the character '5' into an actual 5.
    if ((ch * 10) + X > 65535) is a check to make sure its an unsigned short int
    UserInput = UserInput * 10 + X; is used to put all of the digits together to equal one whole number.

    I'm not understanding why ch = cin.get() changes things? The whole unbuffered input thing is throwing me off I think.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, what do you think ch = cin.get() does? It goes and gets another piece of input and puts it in ch.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Should I have it at the end of my while loop then instead of the beginning?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You surely can't get more input until you're finished with what you've got.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Yup, that was my problem. I changed the "if ((ch * 10) + X > 65535)" to "if ((UserInput * 10) + X > 65535)" and everything works as intended. Thanks tons for the help man. I appreciate it, and sorry for the noobness. Take care and thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Better spacing issues
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2008, 04:46 PM
  2. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 11:14 AM
  3. G++ Makefile Help Needed
    By OWD2k7 in forum Linux Programming
    Replies: 17
    Last Post: 04-30-2007, 07:43 PM
  4. External Functions and Compiler linking issues
    By collymitch in forum C Programming
    Replies: 2
    Last Post: 04-07-2005, 05:17 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM

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