C++ do while loop not working right

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1510 Views - Last Post: 04 October 2012 - 07:49 PM Rate Topic: -----

#1 dashonna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 06

C++ do while loop not working right

Posted 04 October 2012 - 05:17 AM

My assignment is to write a program that will display the sequence of integers from that number to 100, the program will also calculate the sum of the integers from that num to 100. This is what I have, it keeps repeating at "Enter an integer between 1 and 100". I have tried changing the while statement several times. Can someone please tell me if this code looks right or if I am missing something? Thanks


#include <iostream>
using namespace std;

int main()
{
  int j, numb, sum;
    do 
    {
    cout << "Enter an integer between 1 and 100" << endl;
        
        cin >> numb;
    }
    
    while ( (numb >=1)||(numb <= 100) ); 
    
    sum = 0;
    
    for ( j=numb; j>=0; j=j+1 )
    {
        cout <<"\n <<j<< " << endl;
            sum = sum  - j;
    }
    cout << "\n Sum = " << sum << endl;

    cin.get();
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: C++ do while loop not working right

#2 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1291
  • Posts: 2,859
  • Joined: 30-January 11

Re: C++ do while loop not working right

Posted 04 October 2012 - 05:22 AM

You need to change your conditionals a bit. At the moment it will keep looping for as long as you enter a valid number. You want it to loop for as long as you have an invalid number i.e it's out of range.
Was This Post Helpful? 0
  • +
  • -

#3 Null::Void()  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-October 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 06:30 AM

#include <iostream>

using namespace std;

int main()
{
    //Variables
    int userInput;
    int summationOfNumbers;
    //

    cout << "Starting Number:";
    cin >> userInput;

    for (int i = userInput; i < 100; i++){
        userInput++;
        summationOfNumbers = summationOfNumbers + userInput;
        cout << userInput << endl;
    }
    cout << summationOfNumbers << endl;
    return 0;
}



Not really sure what the rule is about spoonfeeding code around here since i just joined to help but it was easy and you should use a for loop if you need to count something. It does what you need it to do

#include <iostream>

using namespace std;

int main()
{
    //Variables
    int userInput;
    int summationOfNumbers = 0;
    //

    cout << "Starting Number:";
    cin >> userInput;

    for (int i = userInput; i < 100; i++){
        userInput++;
        summationOfNumbers = summationOfNumbers + userInput;
        cout << userInput << endl;
    }
    cout << summationOfNumbers << endl;
    return 0;
}





Omg whoops forgot so assign the summation variable to 0, plese use this instead.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 183
  • View blog
  • Posts: 615
  • Joined: 11-June 11

Re: C++ do while loop not working right

Posted 04 October 2012 - 06:50 AM

 while ( (numb >=1)||(numb <= 100) );

Well! Here you are asking the user to re-enter the number again if he/s enters something between 1 and 100. So, if with this condition it asks the user to enter the number again then if you flip it then it won't... ;)
for ( j=numb; j>=0; j=j+1 )
    {
        cout <<"\n <<j<< " << endl;
            sum = sum  - j;
    }


Man! What are you trying to do here? You are asked to print the sequence from that number till 100 and also sum up that sequence. Did you do that?

Your loop must start from that number as you did already and go till 100. Print the number, and add the current index to the total.

You messed up here also:
sum = sum  - j;


Why use - if you want the sum?

Hope this Helps.... :)
Was This Post Helpful? 0
  • +
  • -

#5 Blaylok  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 19-August 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 06:50 AM

Hold on... Just chillax!! I'll help you out! :D

The right way for the condition is putting && between the conditions. This is because, -1 will be less than 100, so one of the conditions will result true and the other will be false. So if you put || instead of && then the loop will take numb as -1. But if we put &&, then both conditions have to be satisfied. So the loop will again ask for the number.

Now, I tried with &&, but this loop still behaves in the same way. So it is better to write with an if condition.


INPUT:
  cout << "Enter an integer between 1 and 100" << endl;
  cin >> numb;

	if(!((numb>=1)&&(numb<=100)))
	{
		goto INPUT;
	}




This works perfectly. But I think it wont show the correct answer as there are more errors following it.

|I| for (j=numb; j>=0; j++)

This will lead you to infinite loop. As j (numb) will always be greater than 0! So the condition you should put is:

for (j=numb; j<=100; j++) //or j=j+1 (in the updation statement


|II| cout <<"\n <<j<< " << endl;

I have no idea what you are trying to do in this statement. But if you want to show the value of j every time then it should have been

 cout << "\n" << j << endl; 


|III| sum = sum - j;

It should be sum = sum + j;. We are adding numbers and not subtracting.

------------------------------------------

So, I'll give you a corrected one.

#include <iostream.h>

int main()
{

  int j, numb, sum;

  INPUT:
  cout << "Enter an integer between 1 and 100" << endl;
  cin >> numb;

	if(!((numb>=1)&&(numb<=100)))
	{
		goto INPUT;
	}

  sum = 0;
 
  for ( j=numb; j<=100; j=j+1 )

	 {

	     cout <<"\n"<< j << endl;

	     sum = sum  + j;

	 }

	 cout << "\n Sum = " << sum << endl;



	 cin.get();

}



This post has been edited by jimblumberg: 04 October 2012 - 08:36 AM
Reason for edit:: Removed email address.

Was This Post Helpful? -2
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 08:02 AM

View PostNull::Void(), on 04 October 2012 - 06:30 AM, said:

Not really sure what the rule is about spoonfeeding code around here since i just joined to help but it was easy ...


Thanks for coming to help! Please help people learn by guiding towards the answer rather than just giving them the answer.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 08:14 AM

View PostBlaylok, on 04 October 2012 - 06:50 AM, said:

The right way for the condition is putting && between the conditions. This is because, -1 will be less than 100, so one of the conditions will result true and the other will be false. So if you put || instead of && then the loop will take numb as -1. But if we put &&, then both conditions have to be satisfied. So the loop will again ask for the number.

Now, I tried with &&, but this loop still behaves in the same way.


As you obviously found out, just replacing the && with || was incorrect.


View PostBlaylok, on 04 October 2012 - 06:50 AM, said:

So it is better to write with an if condition.


INPUT:
  cout << "Enter an integer between 1 and 100" << endl;
  cin >> numb;

	if(!((numb>=1)&&(numb<=100)))
	{
		goto INPUT;
	}




This works perfectly.


Please do not encourage people to use goto's. That could have have been written using a do-while loop like the original code.


View PostBlaylok, on 04 October 2012 - 06:50 AM, said:

So, I'll give you a corrected one.


Do not just give the answers. Help people learn on their own.
Was This Post Helpful? 1
  • +
  • -

#8 Blaylok  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 19-August 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 09:09 AM

View PostSkydiver, on 04 October 2012 - 08:44 PM, said:

Please do not encourage people to use goto's. That could have have been written using a do-while loop like the original code.


I used the goto statement because the do while statement somehow did not work, and the while statement was not the best one for that.
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1291
  • Posts: 2,859
  • Joined: 30-January 11

Re: C++ do while loop not working right

Posted 04 October 2012 - 09:14 AM

goto is never the solution.
Was This Post Helpful? 4
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 09:57 AM

View PostRyano121, on 04 October 2012 - 09:14 AM, said:

goto is never the solution.


+1!

Unless you just have the craving for congee... goto. But even then it's a heterogeenous mixture, rather than a solution (homogeneous mixture).
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 10:07 AM

View PostBlaylok, on 04 October 2012 - 09:09 AM, said:

View PostSkydiver, on 04 October 2012 - 08:44 PM, said:

Please do not encourage people to use goto's. That could have have been written using a do-while loop like the original code.


I used the goto statement because the do while statement somehow did not work, and the while statement was not the best one for that.


For me this works nicely as a do-while loop, and the intent is clear:
int input;
do
{
    cout << "Enter an integer in the range [-5, 5]: ";
    cin >> input;
} while((-5 <= input && input <= 5) == false);


Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: C++ do while loop not working right

Posted 04 October 2012 - 10:28 AM

I'll pitch in a defend GoTo. When you compile down all those fancy conditional statements and constructions, I bet you'll find at least 1 goto / jmp instruction.

In C#5 and VB11 GoTo is used extensively ( inside the scenes compiler generated state-machine), to power the Async & Await feature.

When it comes to the next version of C++11, I'd put money on it that it'll also use GoTo.

Edit: @Blaylok I upvoted that post, cos I'm feeling generous.

This post has been edited by AdamSpeight2008: 04 October 2012 - 10:33 AM

Was This Post Helpful? -1
  • +
  • -

#13 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: C++ do while loop not working right

Posted 04 October 2012 - 11:37 AM

AdamSpeight2008, what is done internally post-compilation is immaterial. What is paramount in code for general consumption is readability and maintainability. And GOTO is a readability and maintenance nightmare.

Does that mean GOTO should never be used? No, but it does mean it should NOT be used where another better solution exists, such as this case. It is extremely rare that GOTO is the right decision.
Was This Post Helpful? 2
  • +
  • -

#14 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: C++ do while loop not working right

Posted 04 October 2012 - 12:35 PM

Quote

display the sequence of integers from that number to 100


Set j to start at the number the user enters. And loop to 100.

for(j = num; j < 100; j++)



Quote

the program will also calculate the sum of the integers from that num to 100.


Why are you subtracting j from sum if we want the sum. Use addition
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: C++ do while loop not working right

Posted 04 October 2012 - 01:15 PM

I'm with Jack here. It really doesn't matter that things get compiled down to jumps and gotos. The point of using a high level language is so I don't have to use low-level syntax. If I wanted to write jump instructions I'd write code in assembly. But since I have high-level constructs to obfuscate the confusing and unmaintainable low-level ones, I use them.

There are a few times where goto might be worth using. A bare few. But I submit that if you find yourself in that situation, you probably could refactor the code around it to make goto less necessary in the first place.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2