Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ loop help

c++ loop

  • Please log in to reply
16 replies to this topic

#13 Julia

Julia

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Learning:C++, PHP

Posted 23 September 2012 - 01:15 PM

I did I really did try that. :(
  • 0

#14 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 23 September 2012 - 01:24 PM

can you show me how your code looks like, now after those changes.
  • 0

#15 Julia

Julia

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Learning:C++, PHP

Posted 23 September 2012 - 01:26 PM

#include <iostream>

#include <iomanip>

#include <stdlib.h>

#include <cstdlib>

#include <process.h>

using namespace std;

int main()

{

// contains the variables



// variables

	double AREA;

	double circle;

	double Triangle;

	int Quit;

	int choice;

	double pi; //radius

		pi =  3.14159;

	double radius;



/// rectangle

	double Rectangle;

	double area;

	double number;

	double length;

	double width;



//triangle



	double TRIANGLE;

	double AREA3;

	double base;

	double height2;

	double area2;



// quit



	double WAY2;

	double answer;

	char input;

	char n;

//asks the user which one  and displays it







//constants for menu choices





	const int  CIRCLE_AREA1 = 1,

			Rectangle_AREA2 = 2,

			TRIANGLE_AREA3 = 3,

			QUIT_WAY2 = 4;





															

// display the choices



			cout << "1. Calculate the Area of a Circle\n\n"

			 << "2. Calculate the area of a Rectangle \n\n"

			 << "3. Calculate the Area of a Triangle \n\n"

			 << "4. Quit \n\n";



					 cin >> choice;  

			





	





		 if  (choice >=1 && choice <=4){ //if choice between 1-4

			 cout << "Thanks for you choice" <<endl;

	





}

while(1)

  {

		switch (choice) //circle area		



		{

		case CIRCLE_AREA1:



		  cout << "enter any number" <<endl;

			  cin >> radius;





		   if	  (radius  < 0 )



		  {

		cout << "You must enter a positive number" <<endl;

		  }

		   else

		  {	





		AREA = radius * radius * pi;



		cout << "The radius of the circle is " << AREA <<endl;	

		cout << "Enter a number 1-4 to continue" <<endl;

		cin >> choice;







		   break;



}

}





//rectangle area



	switch (choice)

	{





		case Rectangle_AREA2:



		cout << " Put a length of a rectangle " <<endl;					









	cin >> length;









		 if ( length < 0 ) {

			cout <<"You must enter a postive number " <<endl;



break;



}

		else

		{

			cout << "Enter a   width of a rectangle" <<endl;

			cin >> width;				



				area = length * width;



			cout << "The area of the rectangle is " << area <<endl;

					cout << "Enter a number 1-4 to continue" <<endl;



			cin >> choice;



break;



}





	}









	switch (choice)

	{





		case TRIANGLE_AREA3:



		cout << " enter any number " <<endl;					









	cin >> base;









		 if ( base < 0 ) {

			cout <<"You must enter a postive number " <<endl;



break;



}

		else

		{

			cout << "Enter a height of a triangle base" <<endl;

			cin >> height2;				



				area2 = base * height2 * 0.5;



			cout << "The area of the triangle is " << area <<endl;

					cout << "Enter a number 1-4 to continue" <<endl;



			cin >> choice;



break;



}





	

//quit choice





	switch (choice)

	{





		case QUIT_WAY2:{







return 0;





}}}}}

  • 0

#16 alenn

alenn

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Programming Language:C++, C#
  • Learning:C++

Posted 23 September 2012 - 01:37 PM

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main()
{
// contains the variables
// variables
    double AREA;
    double circle;
    double Triangle;
    int Quit;
    int choice;
    double pi; //radius
	    pi =  3.14159;
    double radius;
/// rectangle
    double Rectangle;
    double area;
    double number;
    double length;
    double width;
//triangle
    double TRIANGLE;
    double AREA3;
    double base;
    double height2;
    double area2;
// quit
    double WAY2;
    double answer;
    char input;
    char n;
//asks the user which one  and displays it

//constants for menu choices

    const int  CIRCLE_AREA1 = 1,
		    Rectangle_AREA2 = 2,
		    TRIANGLE_AREA3 = 3,
		    QUIT_WAY2 = 4;

   
// display the choices
		    cout << "1. Calculate the Area of a Circle\n\n"
			 << "2. Calculate the area of a Rectangle \n\n"
			 << "3. Calculate the Area of a Triangle \n\n"
			 << "4. Quit \n\n";
					 cin >> choice; 
		   

   

		 if  (choice >=1 && choice <=4){ //if choice between 1-4
			 cout << "Thanks for you choice" <<endl;
   

}
while(1)
  {
	    switch (choice) //circle area	   
	    {
	    case CIRCLE_AREA1:{
		  cout << "enter any number" <<endl;
			  cin >> radius;

		   if	  (radius  < 0 )
		  {
	    cout << "You must enter a positive number" <<endl;
		  }
		   else
		  {   

	    AREA = radius * radius * pi;
	    cout << "The radius of the circle is " << AREA <<endl;   
	    cout << "Enter a number 1-4 to continue" <<endl;
	    cin >> choice;
		   break;
}
}

//rectangle area
  
   

	    case Rectangle_AREA2:{
	    cout << " Put a length of a rectangle " <<endl;				   


    cin >> length;


		 if ( length < 0 ) {
		    cout <<"You must enter a postive number " <<endl;
break;
}
	    else
	    {
		    cout << "Enter a   width of a rectangle" <<endl;
		    cin >> width;			   
			    area = length * width;
		    cout << "The area of the rectangle is " << area <<endl;
				    cout << "Enter a number 1-4 to continue" <<endl;
		    cin >> choice;
break;
}

    }
case TRIANGLE_AREA3:{
	    cout << " enter any number " <<endl;				   


    cin >> base;

		 if ( base < 0 ) {
		    cout <<"You must enter a postive number " <<endl;
break;
}
	    else
	    {
		    cout << "Enter a height of a triangle base" <<endl;
		    cin >> height2;			   
			    area2 = base * height2 * 0.5;
		    cout << "The area of the triangle is " << area <<endl;
				    cout << "Enter a number 1-4 to continue" <<endl;
		    cin >> choice;
break;
}
//quit choice
	    case QUIT_WAY2:{
return 0;
}}}}}

Now it works. You should google for switch/case statements in c++, you can't use switch after every case, you use switch only ones. For example, if you want check if number is equal 5:
switch(number){
case 1:
cout << "NUmber is not 5";
case 2: ...
case 3: ...
case 4: ...
case 5:
cout << "Number is 5".

I hope you understand
  • 1

#17 Julia

Julia

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Learning:C++, PHP

Posted 23 September 2012 - 01:43 PM

That was the problem. I get it. I was using switch after every statement. I get it now. Thanks so much for all your help.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download