loop

closed account (zT7X92yv)
I want...

cout << "continue ? (y/n)";

If yes i want my cin<<(numberx2)

every-time someone hit's yes it doubles.

if no, i want my program to close.


I am new to c++.
here is what I have so far:

1
2
3
4
5
6
 if (choice=='y' || choice=='n' continue)
cin>>choice;

cout<<(number*2);

else break; 


Thank You in advance!
Last edited on
Your code will output 2*number, it will not double number for the next round. Also, the continue in your if statement isn't used correctly. Try something like this:
1
2
3
4
5
6
7
8
9
10
11
while(true)
{
    cout << "continue ? (y/n)";
    cin >> choice;

    if (choice != 'y')    break;
    if (choice != 'n')    continue;

    number *= 2;
    cout << number;
}
Last edited on
closed account (zT7X92yv)
thank you..

I tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int choice,number;
    while(true)
{
    number=2;
    cout << "continue ? (y/n)";
    cin >> choice;

    if (choice != 'y')    break;
    if (choice != 'n')    continue;

    number *= 2;
    cout << number;
}
    system("PAUSE");
    return EXIT_SUCCESS;
} 


and it doesn't double, I also want my (yes) to continue and my number to double.
while(true)
{
cout << "continue ? (y/n)";
cin >> choice;

if (choice != 'y') break;
if (choice != 'n')
{
number *= 2;
cout << number;
}
}

'continue' will continue to the next iteration of the loop without executing further statements.
if (choice != 'n') continue;
This will go to the while loop again and check the condition again.
So
number *= 2;
cout << number;
is never executed in case we use continue.
closed account (zT7X92yv)
How do I make it that my number doubles every time I hit Y ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
number=2
cout << "continue ? (y/n)";
cin >> choice;

if (choice != 'y') break;

while (number==y; y++) //not sure how to properly use this

if (choice != 'n') 
{
number *= 2;
cout << number;
} 


i want my number to double for every (y) until i hit (n)

for ex: 2, 4, 8,16......

thanks!
This is what you need. Added comments at the places I modified your program.

int main()
{
int number;
char choice; //choice should be char and not int, to compare it properly
with 'y' or 'Y'
number=2; //so that it does not reassign it to 2 each time the loop iterates.

while(true)
{
cout << "continue ? (y/n)";
cin >> choice;

if (choice != 'y' && choice != 'Y') break; //Added additional 'Y' to ensure y and Y both are accepted
else
{
number *= 2;
cout << number<<endl;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
closed account (zT7X92yv)
Thank You sir!

one last thing :) then I won't bother 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
#include <iostream> 
#include <string> 
#include <cmath> 
#include <iomanip> 

using namespace std; 

double pi_approx(int n)
{ 
        double area = 0.; 
        double h = (1. / n); 
        
        for(int x = 1; x <= n; x++){ 
                double i = (h * x) - (h / 2.); 
                double x = sqrt(1 - i * i); 
                area += h * x; 
        } 
        
        return 4 * area; 
} 

int main () { 
        cout << "Approximation of Pi is"; 
        int n; 
        char choice; 
        n=8;
        assert(n >= 0); 
        double pi = pi_approx(n); 
        cout << setprecision(11); 
        
        cout << pi<<endl<<endl;
        
        while(true)
{
cout << "continue ? (y/n)";
cin >> choice;

if (choice != 'y' && choice != 'Y') break; 
{
n *= 2;
cout << pi<<endl<<endl;
}
}
        system("PAUSE");
        return 0; 
} 


my number does not double. i want my n=8; to go from 8 to 16 to 32 which will bring me closer to the actual pi number.

why isn't it working?

thank you!
Last edited on
closed account (zT7X92yv)
I cannot see why my n=8; doesn't double every-time i input Y

It worked with number=2;
How do you know its not doubling ?
cout << pi<<endl<<endl; you are only printing pi, whose value is not changed.
print n also and check.
cout << n<<endl;
Your program works for me.
closed account (zT7X92yv)
oh my n does change but it does not affect the pi ugh.

When i manually put 8 then 16 then 32 into the n my pi changes, and when i try to create the loop it doesn't =(
You might want to call pi_approx(n); inside the loop..
closed account (zT7X92yv)
I am not sure how to call pi_approx(n);
Last edited on
Exactly what is it you want to do with this program ?
closed account (zT7X92yv)
I want to find the value pi, every-time there is a (yes) the n=8 doubles, it continues to double until there is a (no)

so 8 becomes 16, 32, 64...

pi goes closer and closer to the 3.41.....
Last edited on
Topic archived. No new replies allowed.