Do...While loop

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.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// add the following lines at the beginning of the source file
#include <iostream>

using namespace 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;


}
You don't change the value of again inside your loop. Once the loop enters, again will always have the value 'y', so it will never stop iterating.
Last edited on
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.

Aceix.
hello I need help
I'm trying to do simple calc but I'm faling with all my tryings:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float firstnum, secondnum, result;
char sign;
while(true)
{
cout << "first number:\n";
cin >> firstnum;
cout << "What do yo want to do: * / + - ^\n";
cin >> sign;
cout << "second number:\n";
cin >> secondnum;
if(sign == '*')
{
cout << "the result is:" << firstnum * secondnum << "\n\n";
}
if(sign == '+')
{
cout << "the result is:" << firstnum + secondnum << "\n\n";
}
if(sign == '-')
{
cout << "the result is:" << firstnum - secondnum << "\n\n";
}
if(sign == '/')
{
if(secondnum == 0)
{
cout << "invalid denominator" << "\n\n";
}
else
{
cout << "the result is:" << firstnum / secondnum << "\n\n";
}
}
if(sign == '^')
{
result == pow((float) firstnum, secondnum);
cout << result << "\n\n";
}
}
return 0;
}

this is my code (C++code blocks compiler)
and this is the error:
undefined reference to `pow(double, double)'

HELP!!! Thank you!


Last edited on
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/

Please don't post in somebody else's thread.
Last edited on
Thank you AbstractionAnon
I'm not 100% sure but wouldn't this be way easier just using a for loop?
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.
Topic archived. No new replies allowed.