Do-While Loops and switch statement

Cannot return to beggining.

Page 1 of 1

4 Replies - 2991 Views - Last Post: 10 March 2009 - 05:31 PM Rate Topic: -----

#1 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • 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  Icon User is offline

  • D.I.C Head

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

Re: Do-While Loops and switch statement

Posted 09 March 2009 - 07:22 PM

View Postno2pencil, 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  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • 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  Icon User is offline

  • D.I.C Head

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

Re: Do-While Loops and switch statement

Posted 10 March 2009 - 05:31 PM

View Postno2pencil, 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