Help with for loop

I'm making a code that will recieve the dimensions for four rooms and then add them together and give me variable totalSqFeet. I have to use a for loop to achive this and idk how to take the separate values from each room. It's hard to explain here is the coding i have so far any help?


// Ethan Hernandez COSC-1436-2001
#include <iostream>
#include <string>
using namespace std ;
int main ( )
{
double width ;
double length ;
double room ;
double totalSqFeet ;
const double carpetWidth = 15 ;
const double sqFeetPerYard = 9 ;
double squareYards ;
double linearFeet ;
int counter ;

cout << " \t Ethan Hernandez Assignment L \n Ethan's Carpet Store " ;

for ( counter = 0 ; counter < 4 ; counter ++ )
{
cout << " \n \n What is the width in feet? " << counter + 1 << " \t " ;
cin >> width ;
cout << " What is the length ? " << counter + 1 << " \t " ;
cin >> length ;
if ( width < 10 || width > 50 )
{
cout << " \n Width is not between 10 and 50 " ;
width = 0 ;
}
if ( length < 10 || length > 50 )
{
cout << " \n Length is not between 10 and 50 " ;
length = 0 ;
}

room = width * length ;

cout << " Area of room is " << room ;

}

system("Pause") ;
return 0 ;
}


P.S. there is more to this program that is why their is more variables but i just need help on this section.
use code tag next time.
ok now let's look at this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std ;
int main ( )
{
double width ;
double length ;
double room ;
double totalSqFeet ;
const double carpetWidth = 15 ;
const double sqFeetPerYard = 9 ;
double squareYards ;
double linearFeet ;
int counter ;

cout << " \t Ethan Hernandez Assignment L \n Ethan's Carpet Store " ;

for ( counter = 0 ; counter < 4 ; counter ++ )
{
cout << " \n \n What is the width in feet? " << counter + 1 << " \t " ;
cin >> width ;
cout << " What is the length ? " << counter + 1 << " \t " ;
cin >> length ;
if ( width < 10 || width > 50 )
{
cout << " \n Width is not between 10 and 50 " ;
width = 0 ;
}
if ( length < 10 || length > 50 )
{
cout << " \n Length is not between 10 and 50 " ;
length = 0 ;
}

room = width * length ;

cout << " Area of room is " << room ;

}

system("Pause") ;
return 0 ;
}


if u need total square of 4 rooms then u should create array of 4 fooms
so: double room [4];

then if user's input is not correct you should add goto statement to repeat asked until answer is corrct ( or better is to use while than goto)

once u putt all squares into room array make sum of that array.
Last edited on
oh i'm sorry about that...
1. Don't be sorry about making mistakes, that's why the forums was created.

2.NEVER USE A GOTO. If you need to know why, google Goto Statement Dangerous.

3.Why is the carpet width a const if the area of the rooms changes? Is the width of the rooms combined maxed at 15, or is it for each room?
1
2
3
4
5
6
7
8
9
10
if ( width < 10 || width > 50 )
{
cout << " \n Width is not between 10 and 50 " ;
width = 0 ;
}
if ( length < 10 || length > 50 )
{
cout << " \n Length is not between 10 and 50 " ;
length = 0 ;
}


Firstly, your input validation for the length and width just poses the question whether they are within the constraints, what you should do is use a while loop.. Like this

1
2
3
4
5
While ( length < 10 || length > 50 )
{
  cout << "Length is not ... blah blah";
  cin >> length;
}


You we're on the right track, but you want to make sure that you still get the Input from them despite that they entered a wrong number.


Topic archived. No new replies allowed.