Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,857 people online right now. Registration is fast and FREE... Join Now!




Problem with using isalpha function

 
Reply to this topicStart new topic

Problem with using isalpha function

codemax88
18 Mar, 2007 - 08:11 PM
Post #1

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 33


My Contributions
//The program should terminate input on non-numeric input. But the isalpha function closes my command window before any
results are display. What is causing this problem?

CODE

#include<iostream>
#include<cctype>

const int ArSize =10;

int main(int argc, char **argv)
{
    using namespace std;
    
double donations[ArSize];
double p, average = 0, total;
char q;
int i = 0, count =0, larger_than_average =0;
cin >> p;
                            
while ( ! isalpha (p) ) // using isalpha function
{

donations[i] = p;
total += donations[i];
i++;
cin >> p;
count += 1;  // keeping account of how many inputs
average = ( total /count);

}  

  
  
for (i=0; i< ArSize; i++)   // reading output of array and using loop in helping to determine larger than average
{
  if( donations[i] > average)
  larger_than_average += 1; // keeping account of numbers larger than average
cout << donations[i]<< endl;}
  

  
cout << "Program has been terminated by user"<<endl;


cout << "average " << average <<endl;
cout << " numbers in the array larger than average " << larger_than_average <<endl;







system ("pause");

User is offlineProfile CardPM
+Quote Post

Xing
RE: Problem With Using Isalpha Function
18 Mar, 2007 - 08:42 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
isalpha function takes char as argument not double. Check this
You can also refer to this link

This post has been edited by Xing: 18 Mar, 2007 - 08:43 PM
User is offlineProfile CardPM
+Quote Post

codemax88
RE: Problem With Using Isalpha Function
19 Mar, 2007 - 05:31 PM
Post #3

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 33


My Contributions
QUOTE(Xing @ 18 Mar, 2007 - 09:42 PM) *

isalpha function takes char as argument not double. Check this
You can also refer to this link

Thanks for your help xing! The specs really did not require for me to use the isalpha function but it was in the same chapter and I thought that would do the trick. The following code works just fine with some other adjustment in my posted code:
CODE
while (i != ArSize && cin >> donations[i])

using any undeclared letter will terminate program. Other code in my program that has nothing to do with terminating code should be deleted or modified to make my program to run correctly. Below the completed working code:
CODE
#include<iostream>


const int ArSize =10;

int main(int argc, char **argv)
{
    using namespace std;
    
double donations[ArSize];
double  average = 0, total= 0.0;

int i = 0, count =0, larger_than_average =0;


while (i != ArSize && cin >> donations[i])
{


total += donations[i];


count += 1;  // keeping account of how many inputs
average = ( total /count);
i++;
}  

  
  
int j;
for (j=0; j< i; j++)   // reading output of array and using loop in helping to determine larger than average
{                           // V6 originally  ArSize was use in the for statement like this:
  if( donations[j] > average)// for (i=0; i< ArSize; i++)  but ArSize use this way will read empty
                             // values if the imput did not equal the array max. So i represents
                             // the true number of array values enters
  larger_than_average += 1; // keeping account of numbers larger than average
cout << donations[j]<< endl;}
  

  
cout << "Program has been terminated by user"<<endl;


cout << "average " << average <<endl;
cout << " numbers in the array larger than average " << larger_than_average <<endl;






system ("pause");

return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:21PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month