Results 1 to 5 of 5

Thread: Look for experoenced man in error handling

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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 '$'?
    <=== The Great Pumpkin says ===>
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.