Look for experoenced man in error handling
HI all! With the book of Alex Allain I try to finish this file. I think, being alone, I will never succeed. May someone help me. Here is the code:
Code:
/*somme.cpp */
#include <iostream>
using namespace std;
//Sum of two numbers
int failableFunction()
{
double sum =0;
double value[40];
int result=0;
for (int i = 0; i < 40; ++i )
{
cout << "Enter value: " << i << ": ";
cin >> value[ i ];
sum += value[ i ];
}
if (result != 0 )//this line here or in the main ?
{
cout << "Function call failed: "<< result;
if (static_cast<int>(value[ i ])== 36 )//36 is ASCII of $//: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
{
cout << "Here is the sum : " <<sum<<endl;
double nomb=i;
cout << "Here is the average value: " << sum / nomb << endl;
return 0;
}
}
}
int main ()
{
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 $ for indicating the end \n";
const int result = failableFunction();
if (result != 0 ) cout << "Function call failed: " << result ;//this line here or in tha function ?
}
Re: Look for experoenced man in error handling
Sounds like a homework assignment to me. I'm not going to give you the code, but here are some things to think about:
1 - In main(), you say "up to 39 numbers", but in your function, the loop goes from 0 - 39 (40 numbers), and requires the user to enter all 40 of them.
2 - In main(), you say the stopping condition is if the user enters a "$". But in your function, you don't check for a "$" character until after the user has been forced make 40 entries, even if they have been entering "$" for the last 20 times.
3 - In line 23, the variable "i" is undefined because it is only defined inside the scope of your for() loop. Even if it was still defined, it would have the value 40, which is outside the range of your "value" array (which has index values from 0 - 39).
4 - Your function is supposed to return an integer result, except there are at least three places where the function can exit, but only one of them returns anything.
That is only the start. I suggest you re-read what you say in lines 33 and 34 in main() and think hard about how to re-write your function so it accomplishes that goal.
Re: Look for experoenced man in error handling
I never thought I had a reply on Qt, since I put it in General C++ Programming. It is very kind of you. I got a file on General C++ , it works well but has nothing to do with my file (it is also a big file). So I am very happy to try your proposition. Wait please some one or two days before I announce you that I succeded. Thanks
Re: Look for experoenced man in error handling
I have a new file. It compiles well. The thing that goes wrong is that the loop goes to 40, even if I enter $. So the last i=40. Therefore my control of the ASCII cannot work. I hope I will have some aid of you. Thanks.
Code:
/*stranz.cpp */
#include <iostream>
using namespace std;
//global variables
double sum=0;
double value[40];
int i=0;
int failableFunction()
{
//double value[40]
cout << "good morning\n";
for ( i = 0; i < 40; ++i )
{
cout << "Enter value: " << i << ": ";
cin >> value[ i ];
sum += value[ i ];
}
}
int main ()
{
cout << "Here is the addition of many numbers (even with a .).MAXIMUM OF NUMBERS=40\n";
cout << "Once you have finished your list of numbers, hit $ for indicating the end \n";
const int result = failableFunction();
if (result != 0 )
{
cout << "Function call failed: " << result << endl;
cout << value[ 0 ] << ' '<< i <<' '<<value[ i ] << endl;
if (static_cast<int>(value[ i ])== 36 )//36 is ASCII of $
{
cout << "Here is the sum: " << sum<<endl;
double nomb=i;
cout << "Here is the average value: " << sum / nomb << endl;
}
return 0;
}
}
~
~
Re: Look for experoenced man in error handling
Look, this is a Qt forum. We aren't here to teach you how to write your first C++ program. It is apparent that you really need to learn some C++ first before you even think about how to write Qt programs.
Here's another hint: Look at lines 13 - 19. Is there anything in those lines that says, "Hey, if the user enters a '$', stop asking for more values"?
And making "i" a global variable doesn't solve the bad logic in your program, it just hides it. Even when you make it a global variable, it will still hold the value 40 when the for() loop exits, which is one more than the last valid index for your array.
And even if you do subtract 1 from "i" so you check value[39] instead of value[40] for a '$', what happens if the user actually does enter 40 valid numbers and never enters a '$'?