Home > Enterprise >  How to get the loops and selection part of this program correct?
How to get the loops and selection part of this program correct?

Time:01-06

heres my program, im tasked to create a calculator that can calculate either the volume or surface based on the users input. there are supposed to be 8 different geometry shapes (A-G) in which in supposed to calculate but im not able to get pass the first one.the user needs to input the code(A-G) and option to calculate either surface oor area (1 or 2) correctly if not the program will reloop .once the program finsihes calculating the program will ask the user if they want to reuse the program or exit te program. i also encounter some erros with my password. the program is supposed to quit after 3 wrong attempts but instead it quits after 4.

#include <iostream>
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    char code, option;
    int n, op, s, l, w, r, h;
    float pi=3.14, total = 0;
    string password, username;
    int attempts_left = 3;
    n=0;

    cout << "Enter password: ";
    getline(cin, password);

    cout << "Enter username: ";
    getline(cin, username);

    while (attempts_left > 0 && (password != "abc" || username != "abc")) {
    cout << "Invalid password or username. You have " << --attempts_left << " attempts left.\n";

    cout << "Enter password: ";
    getline(cin, password);

    cout << "Enter username: ";
    getline(cin, username);
    }

    if (attempts_left > 0) {
        cout << "Access granted.\n";
        // Making a table
        cout << " ____________________________________________"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|  CODE |           3D GEOMETRY              |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   A   |              CUBE                  |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   B   |         RECTANGLE SOLID            |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   C   |             SPHERE                 |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   D   |     RIGHT CIRCULAR CYLINDER        |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   E   |       RIGHT CIRCULAR CONE          |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   F   |         SQUARE PYRAMID             |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
        cout << "|       |                                    |"<<endl;
        cout << "|   G   |      REGULAR TETRAHEDRON           |"<<endl;
        cout << "|_______|____________________________________|"<<endl;
    } else {
        cout << "Access denied.\n";
    }


    while (code !='A' || code !='B' || code !='C' || code !='D' || code !='E' ||code !='F' || code !='G') 
    {

        cout << "Enter code (A-G): ";
        cin >> code;

        

        if (code=='A') 
        { while (option != 1 || option != 2) {
        
            cout <<"Enter value: ";
            cin >> s;
            
            if (option == 1 ) {
                total = s*s*s;
                cout << total;
            }
            
            else 
            { 
            total = 6*s*s;
            cout << total;
            }
            }
        
        }
     
    }
}

im expecting an output screen something like this (say i selected A with option 1 my value s being 2)

answer = 8

Would you like to continue the program? (Y/N)

your text

CodePudding user response:

quick scan

  • code is not initalized before that while
  • code !='A' || code !='B' || code !='C' || code !='D' || code !='E' ||code !='F' || code !='G' is always going to be true.

CodePudding user response:

I've fixed your main loop issues, regarding password, main options (A-G) and continue program Y/N, this should get you most of the way there:

int main() {
   char code, option, continueProgram;
   int n, op, s, l, w, r, h;
   float pi = 3.14, total = 0;
   string password, username;
   int attempts_left = 3;
   n = 0;

   cout << "Enter password: ";
   getline(cin, password);

   cout << "Enter username: ";
   getline(cin, username);

   attempts_left--;
   while (attempts_left > 0 && (password != "abc" || username != "abc")) {
       cout << "Invalid password or username. You have " << attempts_left << " attempts left.\n";

       cout << "Enter password: ";
       getline(cin, password);

       cout << "Enter username: ";
       getline(cin, username);

       attempts_left--;
   }

   if (attempts_left >= 1) {
       cout << "Access granted.\n";
       // Making a table
       cout << " ____________________________________________" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|  CODE |           3D GEOMETRY              |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   A   |              CUBE                  |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   B   |         RECTANGLE SOLID            |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   C   |             SPHERE                 |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   D   |     RIGHT CIRCULAR CYLINDER        |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   E   |       RIGHT CIRCULAR CONE          |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   F   |         SQUARE PYRAMID             |" << endl;
       cout << "|_______|____________________________________|" << endl;
       cout << "|       |                                    |" << endl;
       cout << "|   G   |      REGULAR TETRAHEDRON           |" << endl;
       cout << "|_______|____________________________________|" << endl;

   }
   else {
       cout << "Access denied.\n";
       return 0;
   }


   cout << "Enter code (A-G): ";
   cin >> code;

   continueProgram = 'Y';
   while (continueProgram == 'Y')
   {
       while (code != 'A' && code != 'B' && code != 'C' && code != 'D' && code != 'E' && code != 'F' && code != 'G')
       {
           cout << "Invalid code\n";
           cout << "Enter code (A-G): ";
           cin >> code;
       }

       if (code == 'A')
       {
           cout << "Enter value: ";
           cin >> s;
       
           total = s * s * s;
       }
       else if (code == 'B')
       {
           // Do something
       }

       cout << "Result = " << total << '\n';

       cout << "Would you like to continue the program? (Y/N)";
       cin >> code;
   }
    
}
  •  Tags:  
  • c
  • Related