4 Replies - 2120 Views - Last Post: 19 May 2011 - 11:09 AM Rate Topic: -----

#1 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

c++ isalpha issue: string to int error

Posted 19 May 2011 - 09:40 AM

Hello all, I am having an issue with cleaning up (validating) an old program. I want to check if the input entered is a numeric value instead of string. I would like to only except string. (not numeric value of any size). The problem that I am having is a string to int error at compile time. The program was written to use dynamic alloc. arrays. (array of string e.g. names), and if, say "3" or "3333..." is entered, cout an error message. I am trying the "isalpha()" build in function, but am having trouble using it with the dynamic array of string. Where/how am I failing?
Thank you for any advice.

#include <iostream>
#include <string>
#include <limits> //for numeric validation of separate part of code
#include <cctype> 

using namespace std;

int main()
{ 
      string *name; // pointer to name array	
      int *votes, // pointer to vote array
	 	i,
	    numVotes; 
	 
    votes = new int[numVotes];     // Dynamicly allocate votes
    name = new string[numVotes];  // Dynamicly allocate names

	for(i=0;i < numVotes; i++){
		cout<<"\nEnter last names: ";
		getline(cin,name[i]);
/***************************************************************************/
		while (name[i])
  {
    if (isalpha(name[i])) cout << "character " << name[i] << " is alphabetic\n"; // <-- error C2664: 'isalpha' : cannot //convert parameter 1 from 'std::string' to 'int'
    //else cout << "character " << name[i] <<" is not alphabetic\n";
    i++;
  }
/***************************************************************************/		
		
	}//end for for loop




Is This A Good Question/Topic? 0
  • +

Replies To: c++ isalpha issue: string to int error

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: c++ isalpha issue: string to int error

Posted 19 May 2011 - 10:04 AM

but the isalpha() function is not supposed to be used like that. Name is an array of string like {"Homer", "Flanders", "Moe"}. So name[1] is "Flanders" and name[1][2] would be the character 'a'. On that character you can use the isalpha() function (see here).
Was This Post Helpful? 1
  • +
  • -

#3 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: c++ isalpha issue: string to int error

Posted 19 May 2011 - 10:47 AM

OK, I see your point there, but the way my code is setup, I can not use the isalpha()? I mean the compiler seems to be looking at the (int) isalpha() and is not liking the (string) of name. I would really like to keep the code original, meaning keeping it as dynamic alloc. That's what it was first written for..and would like to have it for reference. I just do not know of another way to check if user inputs a numeric value for a name. )"345" would not seem right as a general last name.) Any ideas? or is there a way to reverse the
cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
to not ignore numeric? Then use a if, or while until no numeric value is found? OR if that is not possible, some other way that I am not seeing. Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: c++ isalpha issue: string to int error

Posted 19 May 2011 - 10:57 AM

You're missing the point. name[i] is a string. isalpha( c ) checks to see if the char c is a letter. You can't pass a string to isalpha, you have to give it a char.

So what you want to do is call
isapha(name[i][0])
to see if the first char of the string name[i] is a letter, because name[i][0] is a char: it's the first character in the string name[i].

And by the way, you have a problem in your main function: on lines 15 and 16 you are using numVotes to specify the size of the arrays, but you haven't assigned a value to numVotes.

This post has been edited by r.stiltskin: 19 May 2011 - 10:58 AM

Was This Post Helpful? 1
  • +
  • -

#5 centerline  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 17-October 10

Re: c++ isalpha issue: string to int error

Posted 19 May 2011 - 11:09 AM

OK got cha! Same as what muballitmitte said, just in a way my brain understood. And as far as the the numVotes, your right. Will initialize and work on the ideas given. Thank you two very much. You have helped greatly, and very appreciated !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1