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

#1 Julia

Julia

    CC Newcomer

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

Posted 23 September 2012 - 12:55 AM

My program. First I wrote the program. That was the easy part. then adding a loop. I have been it for hours. I'm so proud of myself because I figured out the loop. After two times the loop stops and I cant get the loop on the four part.
Part 4 does not say what its suppose to say but then again I have been one it for hours.
I am new to C++ so if my code is perfect do not tear me apart about it. And no mean comments just please help.
I'm using a while loop.
Loops are difficult for me. I get everything else in c++.
I understand loops so much better after doing thisi then when I started.


#include <iostream>
#include <iomanip>
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(choice >=1 && choice <=4){

		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
while(choice >=1 && choice <=4){

	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;



}
		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;

}


	}


// while(choice >=1 && choice <=4){

	switch (choice)
	{


		case TRIANGLE_AREA3:

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




	cin >> base;




		 if ( base < 0 ) {
			cout <<"You must enter a postive number " <<endl;



}
		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

	while(choice >=1 && choice <=4){

	switch (choice)
	{


		case QUIT_WAY2:

		cout << "Enter a length OF THE triangle base" <<endl;
		cin >> base;

	cin >> choice;
	break;

  
}
}
return 0;
}
}
}

  • -1

#2 neo1691

neo1691

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts
  • Location:India
  • Programming Language:C, C++, Assembly
  • Learning:Java, C++, PHP, JavaScript, PL/SQL, Assembly

Posted 23 September 2012 - 02:55 AM

Hi.
I compiled and debugged your code.
I see you have used multiple while loops in your code.

Also as you say the 4th part of your code is wrong because you have not written any code to exit the loop.


while(choice >=1 && choice <=4)
		 {
			 switch (choice)
			 {

			 case QUIT_WAY2:
				 cout << "Enter a length OF THE triangle base" <<endl;
				 cin >> base;
				 cin >> choice;
				 break;

			 }
		 }

Well this is how your code should be structured (I am skipping re-writing the whole code again)


int main
{
    //declare your variables

    //display the menu for the user

    //get the input in a variable called choice

    while(1) //infinite loop
    {
        switch (choice)
        {
        case CIRCLE:
            //code to calculate circle area
            //get input again from user in choice
            break;



        case RECT:
            //code to calculate rectangle area
            //get input again from user in choice
            break;


        case TRIANGLE:
            //code to calculate TRIANGLE area
            //get input again from user in choice
            break;

        case EXIT:
            exit(1); //you programs terminates here

        }
    }
}





Thats all, just one while statement and you are fine. You can also use a do-while loop (it will be much better menu driven program if used).

Hope that helps
  • 1

#3 Julia

Julia

    CC Newcomer

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

Posted 23 September 2012 - 08:47 AM

Thank you so much the program works. Excpet for at the end. I can't seem to find how to exit the loop. I did the exit function but it does not work.
  • 0

#4 neo1691

neo1691

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts
  • Location:India
  • Programming Language:C, C++, Assembly
  • Learning:Java, C++, PHP, JavaScript, PL/SQL, Assembly

Posted 23 September 2012 - 09:45 AM

exit(); function is defined in cstdlib

you need to include that at the beginning of the program
  • 0

#5 Julia

Julia

    CC Newcomer

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

Posted 23 September 2012 - 10:47 AM

Its still not working.
I have exit(1);
life you said.
  • 0

#6 alenn

alenn

    CC Resident

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

Posted 23 September 2012 - 12:25 PM

just put break or return at the end of your EXIT part, with that you will leave EXIT part and since there is no code outside of that part your program will terminate.
  • 0

#7 Julia

Julia

    CC Newcomer

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

Posted 23 September 2012 - 12:38 PM

switch (choice)
	{


		case QUIT_WAY2:


	exit(1);
	break;
}


return 0;


}}
}

That is my ending code. It still will not work. Sorry if this is just a simple answer but its not to me. Definitely a newbie.
  • 0

#8 alenn

alenn

    CC Resident

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

Posted 23 September 2012 - 12:44 PM

I think this simple example will teach you everything you need:

#include<iostream>
using namespace std;
int main(){
int option=1, x;
while(option!=0){
cin >> x;
switch(x){
case 1:{
cout << "bla bla" << endl;
break;}
case 2:{
cout << "bla bla bla" << endl;
break;}
case 3:{
cout << "Now I will exit" << endl;
return 0;}}}}

If you select 3. it will exit, but also if you select 0. it will also exit because then the while loop will end, those are simple ways for solving your problem
  • 0

#9 Julia

Julia

    CC Newcomer

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

Posted 23 September 2012 - 12:53 PM

How would I implement that into the last part of the code?
  • 0

#10 alenn

alenn

    CC Resident

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

Posted 23 September 2012 - 01:00 PM

just use your variables

case EXIT:{
// your code
return 0;}

  • 0

#11 Julia

Julia

    CC Newcomer

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

Posted 23 September 2012 - 01:02 PM

I did.
switch (choice)
	{


		case QUIT_WAY2:


}
It does not work.
  • 0

#12 alenn

alenn

    CC Resident

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

Posted 23 September 2012 - 01:05 PM

no you didn't, try this:

switch (choice)
	{


		case QUIT_WAY2:{
	  
return 0;}


}

  • 0





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