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

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




using non-numeric input to terminate program

 
Reply to this topicStart new topic

using non-numeric input to terminate program, My program is written but using isalpha() fuction closes my command wi

codemax88
12 Mar, 2007 - 05:28 PM
Post #1

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 33


My Contributions
//program is to read up to 10 donation values into an array of double.
//The program should terminate input on non-numeric input. It should report the
//average of numbers and also report how many numbers in the array are larger
//than the average. Using isalpha function closes my command window beofer any results is display. I put the isalpha function in my comment field below illustrating how I wanted to use it.

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;
                            // want to use isalpha  here
while (i != ArSize) // using isalpha function like this  while (!= isalpha(p))
{

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");

return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Non-numeric Input To Terminate Program
12 Mar, 2007 - 05:37 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Usage should be
CODE

while (!isalpha(p))

User is online!Profile CardPM
+Quote Post

codemax88
RE: Using Non-numeric Input To Terminate Program
12 Mar, 2007 - 05:56 PM
Post #3

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 33


My Contributions
QUOTE(Amadeus @ 12 Mar, 2007 - 06:37 PM) *

Usage should be
CODE

while (!isalpha(p))



My command window still closes with the corrected changes. I can enter double values but as soon as I enter nonumeric lettes such as the letter "q', my window closes without displaying any output.


User is offlineProfile CardPM
+Quote Post

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

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