Teen Programmers Unite  
 

 

Return to forum top

C++ HELP ?

Posted by mattlynam2002 [send private reply] at September 24, 2002, 12:22:57 PM

hello friends !
Does anyone know how to check if the user has put in a word or a number ?
eg: You asked for someone's name and they put in a number. I want it to check it ,if you get me.
Please help, thanx

Posted by regretfuldaydreamer [send private reply] at September 24, 2002, 12:59:37 PM

#include <iostream>


using std::cout;
using std::cin;


void main(){
std::cout << "Enter your string: ";
char input[20];
cin >> input;
bool ischar;
for(int i = 0; i < 20; i++){
if(input[i] >= 'a' && input[i] <= 'Z'){
ischar = true;
}
}
if(ischar){
cout << "It is a string containing characters\n";
}
else{
cout << "This string does not contain characters\n";
}
char c;
cout << "Press any key to exit\n";
cin >> c;
}

Posted by CViper [send private reply] at September 24, 2002, 01:09:03 PM

minor bug: as long as a single token is a character, that app will return "true". (there are some other things aswell...)

you have to do it the other way around; test for a digit/special token, and if there is one, set a flag/return false

bool verify( const char* ptr )
{
  int i = 0;

  while( ptr[i] != 0 )
  {
    if( !(ptr[i] >= 'a' && ptr[i] <= 'Z') )
      return false;

    ++i;
  }

  return true;
}


Note: this assumes that RDD got the ASCII stuff right (eg. that all chars are in between 'a' and 'Z')
Posted by regretfuldaydreamer [send private reply] at September 24, 2002, 01:19:42 PM

I hope its right.
/me programs in Java, and takes a passing interest in C++.

And shouldn't this be in "programming"?

Posted by unknown_lamer [send private reply] at September 24, 2002, 01:25:14 PM

You should also use a std::string intead of a character array.

So,

 
#include <string>
#include <cctype>
#include <iostream>


int main (int argc, char **argv)
{
  std::string input;
  bool isnum = false;
  std::cin >> input;
  for (std::string::const_iterator i = input.begin ();
       i != input.end ();
       ++i)
  {
     if (std::isdigit (*i))
       {
         isnum = true;
         break;
       }
  }
  // isnum will now be true if the number is a number, otherwise false
  // ...

  return 0;
}
Posted by regretfuldaydreamer [send private reply] at September 24, 2002, 01:28:52 PM

I haven't got to the standard libary section of my c++ book yet. I will do though, some time.

Posted by CViper [send private reply] at September 24, 2002, 01:31:19 PM

nah, i tend to use char arrays. it's kinda a habit (nothing bad if you manage using them)

Posted by unknown_lamer [send private reply] at September 24, 2002, 01:32:32 PM

No, char arrays will eventually create a buffer overflow. Using std::string makes your life easier because it will automatically resize. If you are worried about speed for fixed sized strings, just make sure to reserve() the number of chars that will be in the string before using it.

Posted by CViper [send private reply] at September 24, 2002, 01:39:36 PM

i use "old" functions like sprintf() & co quit often; std::string dosen't solve the problems there anyway.

as i said, it's all about habit/style

Posted by Psion [send private reply] at September 24, 2002, 02:04:54 PM

Not really, CViper. There is no good reason to do what you do. Obviously, you shouldn't be using C or C++ at all, to take it further.

Posted by unknown_lamer [send private reply] at September 24, 2002, 09:20:57 PM

#include <stringstream>...now you can use a stringstream and output stuff (or input from) to a string.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.