a problem with cin......

This is a discussion on a problem with cin...... within the C++ Programming forums, part of the General Programming Boards category; this code works ok until i type say a letter. like if i typed 789 it works fine. if i ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    a problem with cin......

    this code works ok until i type say a letter. like if i typed 789 it works fine. if i type say the letter a or a word or anything with a char in it, it then tells me that the monster doesn't exist, which it should, but then repeatedly keeps looping through my program coming to this function and telling me that the monster doesn't exist. Surely there is a way to make sure a number is typed and not a letter?????

    Thanks for your time.....

    werdy666

    Code:
    void viewmonsterstat()
    {
    int answer=0;
    
    cout << "Which monsters stats would you like to view?" << endl;
    cout << "--------------------------------------------" << endl << endl;
    cout << "1. " << bad[0].name << endl;
    cout << "2. " << bad[1].name << endl;
    cout << "3. " << bad[2].name << endl;
    cout << "4. " << bad[3].name << endl;
    cout << "5. " << bad[4].name << endl << endl << endl;
    
    cin >> answer ;
    
    	switch(answer)
    		{
    		case 1:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 2:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 3:
    		viewcurrentmonster(answer-1);
    		return;	
    		break;
    		
    		case 4:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 5:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 0:
    		default:
    		cout << "That monster doesn't exist!" << endl;
    		Sleep(1000);
    		return;
    		break;
    		}// end switch
    
    } // end function

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    do{
    cin >>answer;
    }while (answer>5);

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    No, thats not it.

    cin is expecting an integer. you don't give it one. cin is sad.

    The problem is, the unintelligible gibberish (your letter) is still in the hopper (stdin) and is waiting to be processed.

    You are gonna want to cin.ignore() it, so that the next time cin looks to stdin, it isn't still waiting there to confound it again.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    33
    yeah the do loop doesn't work, i didn't think it would.

    Could you please show me a quick example of how i was use cin.ignore() to make sure that if letters are typed that it wouldn't go through the loop in my function?

    Where on the net can i find examples/documentation of things like cin.ignore() ??

    Thanks for helping a newbie!

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    wouldn't it just read the character as its integer equivalent?

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    No.. because operator<<(istream&, int) is specially set up to accept integers. Characters just bork it.

    As for cin.ignore(), basically it is this:

    cin.ignore({number}, {delimiter});

    Will dump the first {number} characters or dumb all characters until it hits a {delimiter}, whichever comes first. So a good implementation might be:

    cin.ignore{255, '\n');

    That would dump everything that is stuck in the buffer up until the newline.

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    By the way put that right after cin << answer;

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    correction. cin>>answer;

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    To remove every character from the input stream:

    while (std::cin.rdbuf()->in_avail() > 0) std::cin.get();
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    void viewmonsterstat()
    {
    int answer=0;
    
    do {
       cout << "Which monsters stats would you like to view?" << endl;
       cout << "--------------------------------------------" << endl << endl;
       cout << "1. " << bad[0].name << endl;
       cout << "2. " << bad[1].name << endl;
       cout << "3. " << bad[2].name << endl;
       cout << "4. " << bad[3].name << endl;
       cout << "5. " << bad[4].name << endl << endl << endl;
    
       cin >> answer ;
       if (cin.fail())
       {
          cin.clear();
          cin.ignore();
       }
    } while (answer == 0);
    
    	switch(answer)
    		{
    		case 1:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 2:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 3:
    		viewcurrentmonster(answer-1);
    		return;	
    		break;
    		
    		case 4:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 5:
    		viewcurrentmonster(answer-1);
    		return;
    		break;
    		
    		case 0:
    		default:
    		cout << "That monster doesn't exist!" << endl;
    		Sleep(1000);
    		return;
    		break;
    		}// end switch
    
    } // end function

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    33
    Thankyou swoopy! that works perfect!



    Werdy666

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  2. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 07:43 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with cin
    By ErionD in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 10:27 AM

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