Do-While Loops and switch statement

Page 1 of 1

4 Replies - 7933 Views - Last Post: 10 March 2009 - 05:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=91490&amp;s=42bc4bcb6564974de78c538f7a3f0830&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mvalor

• D.I.C Head

Reputation: 0
• Posts: 71
• Joined: 16-November 08

Do-While Loops and switch statement

Posted 08 March 2009 - 08:20 PM

I am trying to calculate a number raised to an exponent.
The problem is when I need to have the prgram return to the top.
(The actual problem lies when pres "n" within the switch statement)
This is eluding me and need some advice.

Here is the code.
```#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float t, v;
float result;
char option;
do {
cout << "Enter a number to be raised to an exponent : ";
cin >> t;
cout << "Enter the exponent : ";
cin >> v;
result = pow(t,v);
cout << "Result is " << result <<"\n";
//Entering the choice to quit or not.
cout<< "Do you wish to quit?  Please enter Yes (y) or No (n)	" << "\n\n";
cin >>option;
switch (option)
{
case 'y':
cout << "\n\n";
cout<< "Hasta la vista Baby.....";
break;
case 'n':
cout << "\n\n";
cout<< "Play it again Sam..."  << "\n\n";
continue;
break;
}
}
while(option == 'y' && option != 'y');

system ("pause");
return(0);

}

```

Anyone?

MV*/

Is This A Good Question/Topic? 0

Replies To: Do-While Loops and switch statement

#2 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

Re: Do-While Loops and switch statement

Posted 08 March 2009 - 08:22 PM

How can option both equal, & not equal y?

```while(option == 'y' && option != 'y');

```

Was This Post Helpful? 0

#3 mvalor

• D.I.C Head

Reputation: 0
• Posts: 71
• Joined: 16-November 08

Re: Do-While Loops and switch statement

Posted 09 March 2009 - 07:22 PM

no2pencil, on 8 Mar, 2009 - 07:22 PM, said:

How can option both equal, & not equal y?

```while(option == 'y' && option != 'y');

```

Well.... If I do place option==n as part of the code, it will still behave the same way.

What do you suggest?

MV*/
Was This Post Helpful? 0

#4 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

Re: Do-While Loops and switch statement

Posted 09 March 2009 - 07:38 PM

```#include <iostream>
#include <cmath>
#include <cstring> // toupper
using namespace std;
int main(void) {
float t, v;
float result;
char option = 'N';
int i=0;
while(toupper(option)=='N') {
if(i>0) cout<< "Play it again Sam..."  << "\n\n";
i++;
cout << "Enter a number to be raised to an exponent : ";
cin >> t;
cout << "Enter the exponent : ";
cin >> v;
result = pow(t,v);
cout << "Result is " << result <<"\n";
//Entering the choice to quit or not.
cout<< "Do you wish to quit?  Please enter Yes (y) or No (n)    " << "\n\n";
cin >> option;
}
cout<< "Hasta la vista Baby....." << "\n\n";

//system ("pause");
return(0);
}

```

The value of option is default to n (for no). Check option, while it's equal to n (for no) run through the loop. Gather the new value for option. Repeat.

There really is no need to have the switch in there as well, thus checking it twice. So you make an integer to check how many times the process has looped, as long as it's more than once, you can output your "repeat text", & then at exit, output the exit text.
Was This Post Helpful? 0

#5 mvalor

• D.I.C Head

Reputation: 0
• Posts: 71
• Joined: 16-November 08

Re: Do-While Loops and switch statement

Posted 10 March 2009 - 05:31 PM

no2pencil, on 9 Mar, 2009 - 06:38 PM, said:

```#include <iostream>
#include <cmath>
#include <cstring> // toupper
using namespace std;
int main(void) {
float t, v;
float result;
char option = 'N';
int i=0;
while(toupper(option)=='N') {
if(i>0) cout<< "Play it again Sam..."  << "\n\n";
i++;
cout << "Enter a number to be raised to an exponent : ";
cin >> t;
cout << "Enter the exponent : ";
cin >> v;
result = pow(t,v);
cout << "Result is " << result <<"\n";
//Entering the choice to quit or not.
cout<< "Do you wish to quit?  Please enter Yes (y) or No (n)    " << "\n\n";
cin >> option;
}
cout<< "Hasta la vista Baby....." << "\n\n";

//system ("pause");
return(0);
}

```

The value of option is default to n (for no). Check option, while it's equal to n (for no) run through the loop. Gather the new value for option. Repeat.

There really is no need to have the switch in there as well, thus checking it twice. So you make an integer to check how many times the process has looped, as long as it's more than once, you can output your "repeat text", & then at exit, output the exit text.

I have to admitt, being stubborn on making something work when it is not supposed to, does not pay in coding.

I appreciate the help on this.

Many thanks.

MV*/
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }