Hi all. I should like to avoid adding 9999 at the end of the list. Perhaps using ENTER as CR and validation in the same time (that is only one hit at the end of the number). If you have an idea give it to me. Thanks. Hereis the code:
/* adder.cpp */
#include <iostream>
using namespace std;
//Sum of two numbers
int add (int x,int y)
{
return x + y;//pun in add the sum of x+y
}
int main ()
{
int result = add( 1, 2 );
cout << "The result is: " << result << '\n';
cout << "Adding 3 and 4 gives us: " << add( 3, 4 ) <<endl;// no return to add
cout <<add<<endl;// verify
//Sum of many numbers
cout << "Hit ENTER to continue \n";
cin.get();
cout << "Here addition of many nombers (even with a comma).MAXIMUM OF Numbes=39 : \n";
cout << "When you finish the list of your nombers, hit 9999 for indicating the end of the list \n";
double sum = 0;
double values[ 40 ];
for (int i = 0; i < 40; ++i )
{
cout << "Entrez valeur " << i << ": ";
cin >> values[ i ];
sum += values[ i ];
//if ( values[i].empty() )
//if ( values[ i ] == 0 )
if ( values[ i ] == 9999 )
{
sum -= 9999;
cout << "Here is the sum : " <<sum<<endl;
double nomb=i;
cout << "Here is the average value: " << sum / nomb << endl;
return 0;
}
}
}
You can either (a) ask the user how many numbers to add before the first number is entered and set up a countdown loop or (b) after every number is entered give the user a choice (i) enter another number or (ii) exit entering numbers - in any design you'd have to let the program know you're done entering numbers one way or the other
#include <iostream>
int main()
{
std::cout << "addition of many numbers:\nEnter numbers one by one, seperated by spaces\n"
<< "when you finish the list of your nombers, enter 9999 for indicating end of input\n" ;
longlong sum = 0;
int value ;
int cnt = 0 ;
while( std::cout << "enter value #" << cnt+1 << ": " &&
std::cin >> value && value != 9999 )
{
sum += value ;
++cnt ;
}
std::cout << "sum of " << cnt << " values: " << sum << '\n' ;
if( cnt > 0 ) std::cout << "average: " << double(sum)/cnt << '\n' ;
}
I think now that I can use ctrl+something or alt+something to stop the list. Of course I advertise the user with a cout , at the beginning of the file. But I dont how where to find ASCI code of such combinations.
If you can give me a little example using ASCII code, I should be very happy. Thanks . sylvain
I tried $ instead of 9999. Unfortutately it compiles well but doesn't work. Entries must be in numbers and nothing else. Once again the code, tagged this time.
/* adder.cpp */
#include <iostream>
usingnamespace std;
//Sum of two numbers
int add (int x,int y)
{
return x + y;
}
int main ()
{
int result = add( 1, 2 );
cout << "The result is: " << result << '\n';
cout << "Adding 3 and 4 gives us: " << add( 3, 4 ) <<endl;// no return in add
cout <<add<<endl;// let us verify that add doesn't contain 7
//Sum of many numbers
cout << "Hit ENTER to continue\n";
cin.get();
cout << "Here is the addition of many numbers (even with a .).MAXIMUM OF NUMBERS=39 : \n";
cout << "Once you have finished your list of numbers, hit 9999 for indicating the end \n";
double sum = 0;
double value[ 40 ];
for (int i = 0; i < 40; ++i )
{
cout << "Enter value " << i << ": ";
cin >> value[ i ];
sum += value[ i ];
//if ( value[i].empty() )
//if (static_cast<int>(value[ i ])== 36 )//unfortunately doesn't work(36 is ASCII of $)
if (value[i]==9999)
{
sum -= 9999;
cout << "Here is the sum : " <<sum<<endl;
double nomb=i;
cout << "Here is the average value: " << sum / nomb << endl;
return 0;
}
}
}
When cin is in an error state it does not modify the provide value nor will it further try to read from the stream. In your case the loop will continue till the end without letting the user enter anything.
So you should test the error state and if so reset it with clear() and ignore() the remaining characters in the stream:
You can indicate the end of input by certain keypresses. On a Windows system, it is CTRL-Z and then ENTER. On other systems it is CTRL-D. The code from JLBorges above would work ok that way. Change sum and value to type double if needed.
Another approach might be to read the input as a string.
Here, I've kept the array, though it isn't really necessary for this task:
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
bool getvalue(double & value)
{
string text;
getline(cin, text); // read a line from input
istringstream ss(text); // make a stringstream, to extract values from the line
if (ss >> value) // attempt to read a number
returntrue; // success
returnfalse; // could not read a number (blank line or non-numeric data).
}
int main ()
{
// Sum of many numbers
constint size = 40;
cout << "Here is the addition of many numbers (even with a .).MAXIMUM OF NUMBERS=" << size << " : \n";
cout << "Once you have finished your list of numbers, just press enter to indicate the end \n";
double sum = 0;
double value[ size ];
int count = 0;
while (count < size &&
(cout << "Enter value " << count+1 << ": ") &&
getvalue(value[count]))
{
sum += value[ count ];
++count;
}
cout << "Here is the sum : " << sum << endl;
cout << "Here is the average value: " << sum / count << endl;
}
To Chervil. Fantastic and smart. Your file works very well. It is very simple comparing that to ERROR HANDLING. Error handling has a mountain of information. I should probably work a whole week to solve the problem. Thank you very much. Sylvain