While Loop Problem

deetlej1

Active Member
Joined
Dec 7, 2008
Messages
38
Reaction score
0
it seems like it is the basic stuff that bits me.... this one is suppose to be easy, it doesn't go in to the while loop and the program is suppose to go through the while loop.

any suggestions how to fix this?

The Question:
Suppose we want to input and validate three integers in a do..while loop. The variable names are n1, n2 and n3. The requirement is that the sum of the three integers is less than 1000 or that n3 is greater than 100. If this condition does not hold, the loop has to be executed again. Write down a correct condition for the do..while loop.

My Coding:

#include <iostream>
using namespace std;

int main()
{
int n1, n2, n3, sum;

do
{
cout<<"Enter a number:";
cin>>n1;
cout<<"Enter a second Number:";
cin>>n2;
cout<<"Enter a third Number:";
cin>>n3;

sum = n1 + n2 + n3;

//loop if sum is less than 1000 OR n3 is greater than 100
} while ((n3 <= 100) && (sum >= 1000));

return 0;

}
 
C#

using System;
using System.Collections.Generic;
using System.Text;

namespace DoWhileProblemCS
{
class Program
{
static void Main(string[] args)
{
int n1, n2, n3, sum;

do
{
Console.Write("Enter number 1: ");
n1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number 2: ");
n2 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number 3: ");
n3 = Convert.ToInt32(Console.ReadLine());

sum = n1 + n2 + n3;
Console.WriteLine(sum.ToString());
}
while ((n3 <= 100) || (sum >= 1000));
}
}
}
 
Last edited:
Unfortunatly the coding must be C++ as I’m using Dev-C++

I tested the program but it still seems like the while is not working. I changed it to

while ((n3 >= 100) || (sum <= 1000));

and still it keep on asking to enter a number

the question states..
the sum of the three integers is less than 1000 or that n3 is greater than 100.
 
C++ cannot evaluate the while brackets like modern .net languages do...
I've written the same app in DevCPP, and it stays in the loop...

You've got your operator sign incorrectly...
The DO { whateva } WHILE { questions } must always execute if the questions are true, which means that n3 must be less than 100 to keep on executing

I've done a single WHERE in C++, and it works - the double doesn't

#include <iostream>
using namespace std;

int main()
{
int n1, n2, n3, sum;

do {
cout << "Number 1: ";
cin >> n1;
cout << "Number 2: ";
cin >> n2;
cout << "Number 3: ";
cin >> n3;

sum = n1 + n2 + n3;

cout << "Total: " << sum << '\n';
} while (n3 <= 100);

return 0;
}
 
And if you had to write it exactly as you would think it?
ie.
while (!((n3 > 100) || (sum < 1000)));

Edit: Wishblade, you just beat me! :)
Edit again: Oh, I see we are thinking a bit differently. :o
 
Last edited:
but with my coding and yours. If N3 is smaller that 100 the program stay in the loop where it is suppose to exit if I understand this correctly?
 
thx to all finaly it work. elysian and wishblade your statement was the best :-)
 
c# will be the next language for me but at this stage I must 1st master the C++ language
 
thx to all finaly it work. elysian and wishblade your statement was the best :-)

Our statements were slightly different. I bet mine was the correct one. :p

Edit. Maybe I should try it first. Don't feel like it now. haha
 
Last edited:
I think this forum is the best as people actually helping others out. hope I can do the same one day for other people.
 
Done in Visual C++ 2008:

#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
int n1, n2, n3, sum;

do {
std::cout << "Number 1: ";
std::cin >> n1;
std::cout << "Number 2: ";
std::cin >> n2;
std::cout << "Number 3: ";
std::cin >> n3;

sum = n1 + n2 + n3;

std::cout << "Total: " << sum << '\n';

} while ((n3 <= 100) && (sum <= 1000));

return 0;
}
 
Test 1: 5, 6, 7 = 18 sum = stays in loop
Test 2: 100, 100, 100 = 300 sum = stays in loop

*Test 3: 101, 101, 101 = 303 sum = quits program
*Test 4: 999, 10, 10 = 1019 sum = quits the program
*Test 5: 999, 10, 101 = 1110 = quits the program
 
There is a big diffrence between && (AND) and || (OR)
If you change my code to ||, Test 3 + 4 stays in loop

false OR false = false
true OR false = true
true OR true = true

false AND false = false
true AND false = false
true AND true = true
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X