Probelm when entering a string

This is a discussion on Probelm when entering a string within the C++ Programming forums, part of the General Programming Boards category; I've got a bit of code working so it will accept the number you put it and use it else ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Probelm when entering a string

    I've got a bit of code working so it will accept the number you put it and use it else where (no need to show you this), but if a string/letter is entered then the program crashes. I'd like it to come up with an error if a string is entered.

    Not an essential part of code for the program, but i'd like to get it as bug free as possible, and I cant seem to find how to fix this anywhere.

    Code:
     
    valid = 0;
    
      do
      {
    	  cout << "\n  enter the number of songs you wish to enter: ";
    	cin >> songs;
    	if (number < 1 || number > 100)
    	{
    		cout << "\n  enter again between 1 and 100";
    	}
    	else
    	{
    		valid++;
    	}
      }while (valid < 1);
    Anyone help?

    Thanks in advance.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    > valid = 0;

    is that global? If so, in your loop use ::valid++
    It makes it clearer, if it really is a global variable that is, hard to
    tell as the indentaton is off a bit,

    If you want to test if a stirng is entered, then create a sperate condition in the
    do-while loop that tests this issue. You could use a char[] for this
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    To handle bad input, you just need to check the return value of the cin >> operation. A common way to do it is like this:
    Code:
    while (!(cin >> songs))
    {
      cin.clear();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      // error message and re-prompt
    }
    You should #include <limits> for numeric_limits and <ios> for streamsize. That line makes sure all of the bad input is ignored.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    cin >> songs;
    if (number < 1 || number > 100)

    You change the variable songs and check the value of number. Seems strange to me
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Quote Originally Posted by vart
    cin >> songs;
    if (number < 1 || number > 100)

    You change the variable songs and check the value of number. Seems strange to me
    Yeah it is actually that, just a typo.

    Thanks guys got it working now.

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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