Thread: scanning for numbers - not letters

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    scanning for numbers - not letters

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int array[10];
    
    	int i = 0;
    	int count = 0;
    
    	cout << "Enter up to ten numbers" << endl;
    
        while (cin >> array[i++] && i < 10)
    	{
    		while (isalpha(array[i]))
    			cout << "Don't enter letters" << endl;
    		    
    		count++;
    	}
    
    	cout <<"\nYour numbers are:" << endl;
    
    	for (i = 0; i < count; i++)
    		cout << array[i] << endl;
    
    	return 0;
    
     }
    The array only holds numbers, so if a letter or anything else is entered, the program immediately exits.

    How do I detect if a number wasn't entered? isalpha isn't working.

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    example

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	int age;
    
    	while (cout << "Enter age: " && !(cin >> age) || (age < 1 || age > 100))
    	{
    		cout << "\nInvalid input or out of range\n";
    		cin.clear();
    		cin.ignore();
    	}
    		cout << "An integer was recieved!" << endl;
    
    		cout << age << endl;
    
    	return 0;
    }

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    As usual...isn't there an easier way to do that? I know nothing about argc and argv.

    I want an easy example like this:
    Code:
    cout << "Enter a number" << endl;
    cin  >> number;
    
    (if number != *a number*) //I'm not sure how to do this part
    cout << "You didn't enter a number" << endl;
    
    else
    cout << "You entered a number!" << endl;

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >> I know nothing about argc and argv.

    They have nothing to do with checking the input, those are main arguments. Just pay attention too

    Code:
    while (cout << "Enter age: " && !(cin >> age)

    I forgot i had a range check in there. This line checks for valid user input. This is how i do it, i dont know any other ways, this is a played out topic, search it.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    27
    If it's an if loop your looking then try changing around the code. This is basically just a variation of RoD's code, but here:
    Code:
          cout << "Enter age: ";
          if(!(cin >> age))
          cout<<"Invalid data"<<endl;
          else
          cout<<"Valid data!"<<endl;
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  6. #6
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    Hi! There is another way to do that. First, why don't you use for instead while?
    so:
    Code:
    while (isalpha(array[i]))
    			cout << "Don't enter letters" << endl;
    Try this:
    Code:
    if(isalpha(array[i])==0)
              cout << "ERROR";

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    he was having problems with isalpha, therefore i didnt suggest it. Also, i do believe my method can be used for any data type.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    isalpha() is meant to check to see if a char entered is within the ASCII range of 'A'-'z'. So isalpha(67) will actually return true. It isn't meant to check integers to see if a number or character was entered, you will still get the same error and problem (and probably more!) that you would get without it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. need to count individual letters in input
    By epox in forum C Programming
    Replies: 12
    Last Post: 05-22-2006, 06:32 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. scanning in with gets() but not over inputing
    By stephanos in forum C Programming
    Replies: 1
    Last Post: 09-09-2002, 03:38 PM
  5. stopping some scanning
    By aoe in forum C Programming
    Replies: 4
    Last Post: 06-09-2002, 01:50 PM