Jump to content

How to end a for loop with a character

- - - - -

  • Please log in to reply
3 replies to this topic

#1
spasticjustice

spasticjustice

    Newbie

  • Members
  • Pip
  • 5 posts
So I've recently been attempting to end a for loop with a character and I'm not really sure how to get started. This is what I currently have.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    const int ARRAY_PASSES = 100;
    const int SIZE = 81;
    char apartment[COUNT];
    int count = 0;
    double apartment_sq[SIZE], apartment_value[SIZE];
    
    do
    {
        for (int COUNT = 0; COUNT <= ARRAY_PASSES; count++)
        {
            cout << "Please enter the name of the first apartment, or enter q to quit: \n";
            cin.getline >> (apartment, SIZE);
            cout << "Please enter the square footage of " << apartment[SIZE] << ": \n";
            cin >> apartment_sq[COUNT];
            cout << "Please enter the rent value of " << apartment_value[SIZE] << ": \n";
            cin >> apartment_value[COUNT];
        }
    }
    while (apartment != 1 || apartment_sq != 81 || apartment_value || 81);
That is being derived from this.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

double apartment_sq_func(double, double);
double apartment2_sq_func(double, double);
double apartment3_sq_func(double, double);
double apartment4_sq_func(double, double);

int main()
{
    int x= 0, count;
    const int APARTMENT_ARRAY = 5;
    double apartment_per_sq[APARTMENT_ARRAY];
    double apartment_square[APARTMENT_ARRAY];
    double apartment_value[APARTMENT_ARRAY]; 
    double bed[APARTMENT_ARRAY];
    double bath[APARTMENT_ARRAY];
    
    const int SIZE = 81;
    char 
        apartment_name[SIZE], apartment2_name[SIZE], apartment3_name[SIZE], apartment4_name[SIZE];
        
    cout << "Please enter the name of first apartment: ";
    cin.getline(apartment_name, SIZE);
    cout << "Please enter the name of second apartment: ";
    cin.getline(apartment2_name, SIZE);
    cout << "Please enter the name of third apartment: ";
    cin.getline(apartment3_name, SIZE);
    cout << "Please enter the name of fourth apartment: ";
    cin.getline(apartment4_name, SIZE);
    for (count = 1; count < APARTMENT_ARRAY; count++)
    {
    do
    {
        if (count ==1)
        {
            cout << "Please enter the square footage of " << apartment_name << ": ";
        }
        else if (count == 2)
        {
            cout << "Please enter the square footage of " << apartment2_name << ": ";
        }
        else if (count == 3)
        {
            cout << "Please enter the square footage of " << apartment3_name << ": ";
        }
        else
        {
            cout << "Please enter the square footage of " << apartment4_name << ": ";
        }
        cin >> apartment_square[count];
        x = 0;
    while (apartment_square[count] <= 0)
        {
            cout << "Please enter a valid square footage greater than 0: ";
            cin >> apartment_square[count];
        }
    if (count ==1)
        {
            cout << "Please enter the rent value of " << apartment_name << ": ";
        }
        else if (count == 2)
        {
            cout << "Please enter the rent value of " << apartment2_name << ": ";
        }
        else if (count == 3)
        {
            cout << "Please enter the rent value of " << apartment3_name << ": ";
        }
        else
        {
            cout << "Please enter the rent value of " << apartment4_name << ": ";
        }
    cin >> apartment_value[count];
    while (apartment_value[count] <= 0)
        {
            cout << "Please enter a valid rent value greater than 0: ";
            cin >> apartment_value[count];
        }
    if (count ==1)
        {
        cout << "Please enter the number of bedrooms for " << apartment_name << ": ";
        }
        else if (count == 2)
        {
        cout << "Please enter the number of bedrooms for " << apartment2_name << ": ";
        }
        else if (count == 3)
        {
        cout << "Please enter the number of bedrooms for " << apartment3_name << ": ";
        }
        else
        {
        cout << "Please enter the number of bedrooms for " << apartment4_name << ": ";
        }
    cin >> bed[count];
    while (bed[count] <= 0)
        {
            cout << "Please enter a valid number of bedrooms greater than 0: ";
            cin >> bed[count];
        }
    if (count ==1)
        {
        cout << "Please enter the number of bathrooms for " << apartment_name << ": ";
        }
        else if (count == 2)
        {
        cout << "Please enter the number of bathroooms for " << apartment2_name << ": ";
        }
        else if (count == 3)
        {
        cout << "Please enter the number of bathrooms for " << apartment3_name << ": ";
        }
        else
        {
        cout << "Please enter the number of bathrooms for " << apartment4_name << ": ";
        }
    cin >> bath[count];
    while (bath[count] <= 0)
        {
            cout << "Please enter a valid number of bathrooms greater than 0: ";
            cin >> bath[count];
        }
    x = 1;
    } while (x < 1 );
    }
    apartment_per_sq[0] = apartment_sq_func(apartment_square[1], apartment_value[1]);
    apartment_per_sq[1] = apartment2_sq_func(apartment_square[2], apartment_value[2]);
    apartment_per_sq[2] = apartment3_sq_func(apartment_square[3], apartment_value[3]);
    apartment_per_sq[3] = apartment4_sq_func(apartment_square[4], apartment_value[4]);
    cout << left;
    cout << setw(25);
    cout << "Apartment Names" << setw(25) << "Value Per Sq. Foot" << "Bed / Bath\n";
    cout << setw(25);
    cout << setprecision(2);
    cout << fixed << showpoint;
    cout << apartment_name << "$ " <<  setw(20) << showpoint << setprecision(2) << apartment_per_sq[0];
    cout << noshowpoint << setprecision(0) << bed[1] <<" Bed /" << bath[1] << " Bath" << endl;
    cout << setw(25);
    cout << apartment2_name << "$ " << setw(20) << showpoint << setprecision(2) << apartment_per_sq[1];
    cout << noshowpoint << setprecision(0) << bed[2] << " Bed /" << bath[2] << " Bath" << endl;
    cout << setw(25);
    cout << apartment3_name << "$ " << setw(20) << showpoint << setprecision(2) << apartment_per_sq[2];
    cout << noshowpoint << setprecision(0) << bed[3] << " Bed / " << bath[3] << " Bath" << endl;
    cout << setw(25);
    cout << apartment4_name << "$ " << setw(20) << showpoint << setprecision(2) << apartment_per_sq[3];
    cout << noshowpoint << setprecision(0) << bed[4] << " Bed / " << bath[4] << " Bath" << endl;
}

double apartment_sq_func(double arg1, double arg2)
{
    double answer;
    
    answer = arg2 / arg1;
    return answer;
}

double apartment2_sq_func(double arg1, double arg2)
{
    double answer;
    
    answer = arg2 / arg1;
    return answer;
}
    
double apartment3_sq_func(double arg1, double arg2)
{
    double answer;
    
    answer = arg2 / arg1;
    return answer;
}
    
double apartment4_sq_func(double arg1, double arg2)
{
    double answer;
    
    answer = arg2 / arg1;
    return answer;
}
My attempt is to make the code more efficient by implementing better loops, as well as making it seemingly infinite in terms of entering data into the array. I just started about 3 weeks ago so I'm assuming I've made quite a few mistakes. If any of those could be pointed out as well I'd be grateful. I'm trying to learn as much as possible. I also know the code is a bit overboard for it's content. I'm just trying to implement as much as I can into a program to help burn into my brain what I've learned.

-Thanks in advanced,
-spastic.

Edited by WingedPanther, 26 October 2011 - 05:42 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
When you reversed your loops, you put your error checking outside of your loop. So you input a bunch of data, and then do a validity check on the LAST item entered.

Before you optimize code, be sure you know what it's doing.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
spasticjustice

spasticjustice

    Newbie

  • Members
  • Pip
  • 5 posts
Not sure what you're referring to at all...

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
One code has:
do {
  for {
  }
}
The other has
for {
  do {
  }
}
The loops are reversed.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users