atoi issue

using VC++

Page 1 of 1

2 Replies - 507 Views - Last Post: 26 July 2009 - 08:56 PM Rate Topic: -----

#1 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

atoi issue

Posted 26 July 2009 - 07:56 PM

if( argc != 3 ) cout << "Usage: gamem -r [name/id]." << endl;
			else
			{

				[b]if( atoi(argv[2]) )[/b]
				{

					int tid = atoi(argv[2]);

					if( tid > gameslist.size() - 1 ) cout << "Error: ID out of range." << endl;
					else
					{

						cout << "Debug Msg: calling system with " << gameslist[tid].path << endl;
						system(gameslist[tid].path.c_str());

					}

				}
				else
				{

					int last_id_match = -1;

					for( unsigned int i(0); i < gameslist.size(); i++ )
					{

						if( !strcmp( argv[2], gameslist[i].name.c_str() ) )
						{

							system(gameslist[i].path.c_str());
							i = gameslist.size();
							cout << argv[2] << " found in games database at ID " << i << endl;

						}

						if( instr(argv[2],gameslist[i].name) )
						{

							last_id_match = i;

						}

					}

					if( last_id_match == -1 ) cout << "Error: " << argv[2] << " not found in game database in partial or full name search." << endl;
					else
					{

						system(gameslist[last_id_match].path.c_str());
						cout << argv[2] << " found in game database based on partial match with ID " << last_id_match << endl;

					}

				}

			}



I have a problem with the bold line. For some reason, atoi never works, even if the input on the command line is 0. As I understand, atoi returns 0 (false) if it can't convert the number, but is this incorrect? Passing argv[2] to a string and then using c_str() doesn't fix it either.

Also, unrelated question, but when I include cctype I get an isnumeric is unidentified error. Is that a VC++ thing or is it the wrong library for it?

Edit: Ignore the atoi to question. it just occured to me why it never evaluates to true if 0 is the input. -_-

This post has been edited by ccubed: 26 July 2009 - 07:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: atoi issue

#2 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: atoi issue

Posted 26 July 2009 - 08:12 PM

I don't think there's a built in function called isnumeric in C++.
But you can accomplish the same task with the isdigit function in cctype.

http://www.cplusplus...cctype/isdigit/

This post has been edited by Zerobu: 26 July 2009 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: atoi issue

Posted 26 July 2009 - 08:56 PM

Functions like atoi should be called, set a local value then use if you are going to use it more then once! Each call takes time, and storing the result in a local value allows for easier debugging!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1