Thread: Need Help looping my program

  1. #1
    n00b
    Join Date
    Jul 2005
    Posts
    4

    Need Help looping my program

    Hi, I have decided to make a area calulator for my first "big" project. I have set it up so when you open it you see four options in the console. I have programed it so when the user hits 1,2,3 or 4 and hits any key to continue it executes the following part of the script. What my question is:

    "How do I make it so the user can press a button and have the program go back to the main screen and have the entrys and calculations that were just done be eresed from the screen?"

    The way I programed the selections on the main screen are using if statements if that helps at all.

    If you would like me to post the source code I will.
    I'm thinking that what I need to use is a while or do while function but I cant seem to get them to work correctly.
    Thanks-
    cchase88
    Last edited by cchase88; 07-25-2005 at 05:48 PM.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yes please post yoru source for a more percise answer.

  3. #3
    n00b
    Join Date
    Jul 2005
    Posts
    4
    Sorry, Here is the code... I have only been program for about 2 weeks and am learning from the book "C++ for dummies"


    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        cout << "           Basic Geometry Calculator v 0.4          " << endl;
        cout << "                  By: CCHASE88           " << endl;
        cout << " " << endl;
        cout << "What would you like to calculate?" << endl;
        cout << "[1]Area of a Triangle" << endl;
        cout << "[2]Area of a Circle" << endl;
        cout << "[3]Area of a Rectangle" << endl;
        cout << "[4]Sphere Calculator" << endl;
        int choice;
        cin >> choice;
        if (choice == 1)
        {
           cout << "What is the base of your triangle?" << endl;
           float base;
           cin >> base;
           cout << "What is the height of your triangle?" << endl;
           float height;
           cin >> height;
           float area = base * height / 2;
           cout << "The area of your triangle is: " << area <<" units squared." << endl; }
        {
        if (choice == 2)
        {
            cout << "What is the radius of your circle?" << endl;
            float radius;
            cin >> radius;
            float area = 3.14 * radius * radius;
            cout << "The Area of your circle is: " << area <<" units squared." << endl;
        }
        if (choice == 3)
        {
             cout << "What is the height of your rectangle?" << endl;
             float height;
             cin >> height;
             cout << "What is the width of your rectangle?" << endl;
             float width;
             cin >> width;
             float area = width * height;
             cout << "The Area of your rectangle is: "<< area <<" units squared." << endl;
        }
        if (choice == 4)
        {
                  cout << "Please enter the radius of your sphere" << endl;
                  float radius;
                  cin >> radius;
                  float area = 4 * 3.14 * radius * radius;
                  float volume = area * radius / 3;
                  cout << "The surface area of your sphere is: " << area << " units squared." << endl;
                  cout << "The volume of your sphere is: " << volume << " units cubed." << endl;
        }
    }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by cchase88; 07-25-2005 at 05:48 PM.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    some peopel will comment on the use of system to
    pause your program, you can look here why


    as for making you program continue untill the user
    what to quit you could do the following

    Code:
    bool quit = false;
    char choice;
    while(quit == false)
    {
       //do stuff
    
       cout << "would you like to quit (y/n)  : ";
       cin >> choice;
       cin.ignore();
       if(choice == 'y' || choice == 'Y')
       {
             quit == true;
       }
    }
    of you could do

    Code:
    char choice;
    for( ; ; )
    {
       //do stuff
    
       cout << "would you like to quit (y/n)  : ";
       cin >> choice;
       cin.ignore();
       if(choice == 'y' || choice == 'Y')
       {
             break;
       }
    }
    also for your choice id recommend using a switch statment.
    Last edited by ILoveVectors; 07-25-2005 at 05:50 PM.

  5. #5
    n00b
    Join Date
    Jul 2005
    Posts
    4
    well I tryed changing it to a switch statement, but I kept getting errors for some reason..

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    int main(void)
    {
        cout << "           Basic Geometry Calculator v 0.4          " << endl;
        cout << "                  By: CCHASE88                      " << endl;
        cout << " " << endl;
        cout << "What would you like to calculate?" << endl;
        cout << "[1]Area of a Triangle" << endl;
        cout << "[2]Area of a Circle" << endl;
        cout << "[3]Area of a Rectangle" << endl;
        cout << "[4]Sphere Calculator" << endl;
        int x;
        cin >> x;
        switch (x) {
        case 1:
           cout << "What is the base of your triangle?" << endl;
           float base;
           cin >> base;
           cout << "What is the height of your triangle?" << endl;
           float height;
           cin >> height;
           float area = base * height / 2;
           cout << "The area of your triangle is: " << area <<" units squared." << endl;
           break;
        case 2:
            cout << "What is the radius of your circle?" << endl;
            float radius;
            cin >> radius;
            float area = 3.14 * radius * radius;
            cout << "The Area of your circle is: " << area <<" units squared." << endl;
            break;
        case 3:
             cout << "What is the height of your rectangle?" << endl;
             float height;
             cin >> height;
             cout << "What is the width of your rectangle?" << endl;
             float width;
             cin >> width;
             float area = width * height;
             cout << "The Area of your rectangle is: "<< area <<" units squared." << endl;
             break;
        case 4:
             cout << "Please enter the radius of your sphere" << endl;
             float radius;
             cin >> radius;
             float area = 4 * 3.14 * radius * radius;
             float volume = area * radius / 3;
             cout << "The surface area of your sphere is: " << area << " units squared." << endl;
             cout << "The volume of your sphere is: " << volume << " units cubed." << endl;
             break;
    }
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    What am I doing wrong?

    The error Dev-C++ keeps getting is "jump to case label"

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try placing the statements within each case in curly brackets if you declare a new variable in that case.
    Code:
    switch (x)
    {
      case 1:
      {
      }
      case 2:
      {
      }
      //etc.
    }
    You're only born perfect.

  7. #7
    n00b
    Join Date
    Jul 2005
    Posts
    4
    oops! thanks.......... it works now thank for your help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not looping
    By abs.emailverify in forum C Programming
    Replies: 9
    Last Post: 11-09-2006, 02:51 AM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM