Results 1 to 2 of 2

Thread: Hi . I'm still a beginner in c++ struggling with some code . thx for ur help

  1. #1
    Join Date
    Apr 2017
    Posts
    5

    Default Re: Hi . I'm still a beginner in c++ struggling with some code . thx for ur help

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. void readValues (int & ,int &); // 2 values A and B
    5. int factorial (int &); // A
    6. int divCount (int); // A
    7. void evSum (int &, int &,long); // A and B and I should have the long sum
    8.  
    9. int main ()
    10. {
    11. int A,B; int choice;
    12.  
    13. do {
    14. cout<<"Enter a positive integer A :"<<endl; // how do I use call by reference to ask the user for A and B?
    15. cin>>A;
    16. cout<<"Enter a positive integer B :"<<endl;
    17. cin>>B;
    18. }while (A<=0 || B<=0 || A>B);
    19.  
    20. do {
    21. cout<<"Choose a computation from the following menu :"
    22. <<"1- Compute sum of the Factorials of "<<A<<"."
    23. <<"2- Count all divisors of "<<A<<"."
    24. <<"3- Compute the sum of all even integers between "<<A<<" and "<<B<<"."
    25. <<"4- Exit the program.";
    26. cin>>choice;
    27.  
    28. switch (choice) {
    29.  
    30.  
    31. case 1 : {
    32. cout<<"The following program computes the sum of factorials of "<<A<<"."<<endl;
    33.  
    34. long int sumFactorial = 0; //I want to compute 1! + 2! + 3! .....A! should I put this in main or not?
    35. for (int K=1; K<=A;K++);
    36. sumFactorial += factorial (A); // error saying that overloaded
    37.  
    38. cout<<"The sum of factorials of "<<A<<" is equal to :"<<sumFactorial<<endl;
    39.  
    40. break; }
    41.  
    42. case 2 : {
    43. cout<<"The following program counts all the divisors of "<<A<<"."<<endl;
    44.  
    45. int divcount;
    46. divcount = divCount (A); // It should count all divisors of A just count them and output the number
    47.  
    48. cout<<"The number of divisors of "<<A<<" is equal to : "<<divcount<<endl;
    49.  
    50. break; }
    51.  
    52. case 3 : {
    53.  
    54. cout<<"The following program computes sum of even integers between "<<A<<" and "<<B<<"."<<endl;
    55.  
    56. long int evsum = 0; // it should count the even integers between A and B.
    57. evSum (A,B,evsum);
    58.  
    59. cout<<"The sum of even integers between "<<A<<" and "<<B<<" is equal to :"<<endl;
    60.  
    61. break; }
    62.  
    63. case 4 : {
    64. cout<<"Exit the program"<<endl;
    65.  
    66. break; }
    67.  
    68. default : {
    69. cout<<"Wrong imput , Please choose a number from the menu."<<endl;
    70.  
    71. break; }
    72.  
    73. return 0;
    74. }
    75. }while (choice != 4);
    76. }
    77.  
    78. long int factorial (int a) { // I need to use call by reference in these function to call the values
    79.  
    80. if(a == 0)
    81.  
    82. return 1; // I have some error that I can't understand . it's not working
    83. else
    84.  
    85. return (a) * factorial(a - 1);
    86.  
    87.  
    88. int divCount (int a) { // error on this brace
    89.  
    90. for (int i=1;i<=a;i++)
    91. if (a % i == 0)
    92.  
    93. divCount++; // same in here I should call the values . I have error on "divCount" saying local function definitions are illegal.
    94.  
    95. return divCount;
    96.  
    97. void evSum (int a,int b) {
    98.  
    99. for ( int i>=a,i<=b,i++)
    100. if (i % 2 == 0)
    101. int evSum = 0; // I don't know if I should use "evsum" from main or "evSum"
    102. evSum += i;
    103.  
    104. return evSum;
    105. }
    To copy to clipboard, switch view to plain text mode 

    So as mentioned in the menu , the user should choose from the following and I should use functions with call by reference to do it . ur help will be appreciated thx alot
    Last edited by wysota; 22nd April 2017 at 20:10. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Hi . I'm still a beginner in c++ struggling with some code . thx for ur help

    Is this some kind of homework or school project?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. struggling with QT JAVA
    By sanjeet in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2013, 12:03
  2. Struggling qith Qt/Phonon
    By burlyjez in forum Qt Programming
    Replies: 0
    Last Post: 27th November 2010, 14:02
  3. Struggling with interface classes
    By JPNaude in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2010, 10:24
  4. Struggling with QThread...
    By TemporalBeing in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2009, 21:46
  5. Struggling with value space
    By UnicycleBloke in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 29th May 2008, 00:26

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.