Thread: do...while loop problem

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    6

    do...while loop problem

    Hi there,
    I really can't understand why the do while loop doesn't work for me.
    I have this simple program
    Code:
    //Calculation 1.3: this includes a do...while loop.
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    class Operation
    {
    public:
    void displayMessage()
    	{
    	cout <<"Welcome to Calculation 1.3. \nThis program contains a do...while loop.\n";
    	}
    void displayMessage1()
    	{
    	int a (0);
    	int b (0);
    	int sum;
    	sum=a+b;
    	
    	do
    		{
    		cout <<"Insert your first number: \n";
    	cin >> a;
    	cout <<"Insert your second number: \n";
    	cin >> b;
    	cout << "The sum of your number is: "<< sum;
    		}
    		while (a>=5 && b<=10);
    	}
    };
    
    int main()
    {
    Operation calculation;
    calculation.displayMessage();
    calculation.displayMessage1();
    return 0;
    }
    The actual loop does work but it doesn't calculate the sum, it alway comes up with a sum of 0 and I can't understand why. I have been trying for hours, but I bet it is a silly error.
    Any idea at all?
    thanks

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    int a (0);
    int b (0);
    int sum;
    sum=a+b;
    This is why it always says sum is 0. You never update sum again after this.
    sum stores the result of a+b. It doesn't store the formula itself. So every time you change a or b you have to update sum or it will be inaccurate.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    6
    uhm, I am not sure I understand this, sorry but I am fairly new to C++. Could you be a bit more specific? Is there a line of code I should use to update sum?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Something like
    Code:
    ...
    sum = a + b;
    cout << "The sum of your number is: "<< sum;

  5. #5
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    You must add
    Code:
    sum=a+b;
    inside your loop in order to change the sum value every time you insert the two numbers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    if you are new to C++ why are starting out with functions in classes?? You might wanna hold off on the object oriented side until you can get past functions and loops.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    C++ is not a declarative language. You don't tell it how a variable is related to some other variables and then expect it to do this by itself if and when necessary. In C++ it does only what you tell it to, when you tell it to do it.
    I.e. it only adds 'a' and 'b' before the loop because that's where you told it to.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    6
    Hi guys,
    thank you for your help. I followed your instructions so that my loop now looks like this:
    Code:
    do
    		{
    		cout <<"Insert your first number: \n";
    	cin >> a;
    	cout <<"Insert your second number: \n";
    	cin >> b;
    	sum=a+b;
    	cout << "The sum of your number is: "<< sum;
    		}
    		while (a>=5 && b<=10);
    	}
    I didn't know that I had to add
    Code:
    sum=a+b;
    in the loop, I thought that being into a function was enough. SO basically, even if a loop is inside a function (in my case inside
    Code:
    void displayMessage1()
    function) is a something separate, a different "entity" from the function. Is this for all the types of loops?So I don't even need to say
    Code:
    sum=a+b;
    outside the function, do I?

    On a different note:
    if you are new to C++ why are starting out with functions in classes?? You might wanna hold off on the object oriented side until you can get past functions and loops.
    I have been given as a manual to study C++, "Deitel and Deitel: C++ how to program" and they start with classes straight away. I found this book quite difficult to be honest and I am now learning using internet tutorials (and this website's), so I found myself with a very little knowledge of classes, so I thought to myself, might be worth using them :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  2. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  3. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  4. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM