Hello! Thank you for your response in advance.. My program is C++ and I use Codeblocks to run my code. My question is what am I doing wrong in my do while loop? I can get the program to "run" but it repeatedly asks me the enter a positive number and does not recognize my negative entry or "y" to do again. Is it necessary to include a call statement? Thank you again.
// add the following lines at the beginning of the source file
#include <iostream>
usingnamespace std;
// create translation from generic keywords to C++
#define Declare
#define ByRef ref
#define Call
#define Constant const
#define Declare
#define Display cout <<
#define Eof cin.eof()
#define Function
#define Input cin >>
#define InputLine(x) getline(cin, x)
#define Integer int
#define Module
#define Newline endl
#define Real double
#define String string
#define Set
int main()
{
int sum = 0;
int number;
char again = 'y';
do {
cout << "Enter a positive number \n";
cout << "then a negative number after your last entry." << endl;
cin >> number;
sum = sum + number;
}while (again == 'y' || again == 'Y');
cout << "Enter a positive number \n";
cout << "then a negative number after your last entry." << endl;
cin >> number;
cout << "The sum of your numbers are: " << sum << endl;
cout << "Do you want to do this again?";
cin >> again;
return 0;
}
1. Your loop depends on whether again=Y or y. But outside the loop, you made it equal to y(line 29), and do not allow the user to change it, so you loop becomes an "eternal" one.
2.
1 2 3
cout << "Enter a positive number \n";
cout << "then a negative number after your last entry." << endl;
cin >> number;
Here you are intending to accept two integers(if I'm right), but providing one variable for input. You could do something like:
1 2 3 4 5 6
cout << "Enter a positive number \n";
cin >> number;
sum=number;
cout << "then a negative number after your last entry." << endl;
cin>>number;
sum += number;
If you still want to maintain the nymber of variables you're using.
Line 41: You're using the comparison operator (==), not the assignment operator (=).
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
For loops are normally used where a fixed number of iterations are desired. The OP has coded an infinite loop.
An infinite loop can be coded as either while (true) or for (;;) Either idiom works. I personally prefer the while (true) construct for an infinite loop.