+ Reply to Thread
Results 1 to 5 of 5

Thread: Look for experoenced man in error handling

  1. #1
    Join Date
    Nov 2016
    Posts
    48
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. /*somme.cpp */
    2. #include <iostream>
    3. using namespace std;
    4. //Sum of two numbers
    5. int failableFunction()
    6. {
    7. double sum =0;
    8. double value[40];
    9. int result=0;
    10. for (int i = 0; i < 40; ++i )
    11. {
    12. cout << "Enter value: " << i << ": ";
    13. cin >> value[ i ];
    14. sum += value[ i ];
    15. }
    16. if (result != 0 )//this line here or in the main ?
    17. {
    18. cout << "Function call failed: "<< result;
    19.  
    20. if (static_cast<int>(value[ i ])== 36 )//36 is ASCII of $//: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
    21.  
    22. {
    23. cout << "Here is the sum : " <<sum<<endl;
    24. double nomb=i;
    25. cout << "Here is the average value: " << sum / nomb << endl;
    26. return 0;
    27. }
    28.  
    29. }
    30. }
    31. int main ()
    32. {
    33. cout << "Here is the addition of many numbers (even with a .).MAXIMUM OF NUMBERS=39 : \n";
    34. cout << "Once you have finished your list of numbers, hit $ for indicating the end \n";
    35. const int result = failableFunction();
    36. if (result != 0 ) cout << "Function call failed: " << result ;//this line here or in tha function ?
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,008
    Thanks
    188
    Thanked 474 Times in 465 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2016
    Posts
    48
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Nov 2016
    Posts
    48
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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.
    Qt Code:
    1. /*stranz.cpp */
    2. #include <iostream>
    3. using namespace std;
    4. //global variables
    5. double sum=0;
    6. double value[40];
    7. int i=0;
    8. int failableFunction()
    9. {
    10. //double value[40]
    11.  
    12. cout << "good morning\n";
    13. for ( i = 0; i < 40; ++i )
    14. {
    15. cout << "Enter value: " << i << ": ";
    16. cin >> value[ i ];
    17. sum += value[ i ];
    18.  
    19. }
    20.  
    21. }
    22.  
    23. int main ()
    24. {
    25. cout << "Here is the addition of many numbers (even with a .).MAXIMUM OF NUMBERS=40\n";
    26. cout << "Once you have finished your list of numbers, hit $ for indicating the end \n";
    27. const int result = failableFunction();
    28. if (result != 0 )
    29. {
    30. cout << "Function call failed: " << result << endl;
    31. cout << value[ 0 ] << ' '<< i <<' '<<value[ i ] << endl;
    32. if (static_cast<int>(value[ i ])== 36 )//36 is ASCII of $
    33. {
    34. cout << "Here is the sum: " << sum<<endl;
    35. double nomb=i;
    36. cout << "Here is the average value: " << sum / nomb << endl;
    37. }
    38. return 0;
    39.  
    40. }
    41.  
    42.  
    43. }
    44. ~
    45. ~
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,008
    Thanks
    188
    Thanked 474 Times in 465 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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 '$'?
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

+ Reply to Thread

Similar Threads

  1. Suggested Error Handling
    By Max Yaffe in forum Qt Programming
    Replies: 6
    Last Post: 15th July 2014, 17:29
  2. QWebPage error handling
    By Guilo in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2010, 05:48
  3. Error handling guidelines?
    By bitflyer in forum Newbie
    Replies: 1
    Last Post: 2nd June 2010, 16:09
  4. QThread Error Handling
    By TheJim01 in forum Newbie
    Replies: 2
    Last Post: 8th April 2010, 14:54
  5. QNetworkReply error handling
    By timmu in forum Qt Programming
    Replies: 5
    Last Post: 25th August 2009, 09:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts