Solved

How can i print my result correctly c++

Posted on 2011-09-11
4
287 Views
Last Modified: 2012-05-12
The program should prompt the user for the total number of rooms in the house, then allow the user to enter the length and width of each room individually. After all the data entry has been performed, the program should display the total square footage of the house. I'm practing from a book and i got my program to run , but when it calculates the total squrefeet it only gives me the last room entered.. for example say a user has 2 rooms and room 1 total length is 4 (2 by 2) and room 2 is 4 as well it should give me total squrefeet as 8 instead it only gives me 4 ..

// ============================================================================
// Description:
//     The program should prompt the user for the total number of rooms in the house,
//     then allow the user to enter the length and width of each room individually.
//     After all the data entry has been performed, the program should display the total square $
//
//
//
//
// ============================================================================

#include <iostream>
using namespace std;



//main function:

#include <iostream>
using namespace std;


// function prototypes:

void CalcSquareFeet(int rooms);
double CalcArea(double L, double W);
void display();

//main function:
int main(void)
{
display();
int Number_of_rooms;

cout << "How many rooms? " <<endl;
cin >> Number_of_rooms;

CalcSquareFeet(Number_of_rooms);




}


//function CalcSquareFeet:
void CalcSquareFeet(int rooms)
{

double length;
double width;
double squarefeet;

for(int i = 1; i<=rooms; ++i)
{
   cout <<"Room #" <<i <<"\n"  <<endl;
   cout <<"Enter Length: " ;
   cin >> length;
   cout <<"Enter Width: " ;
   cin >> width;
   CalcArea(length,width);
   cout <<endl;
   
}

squarefeet = CalcArea(length,width);
cout <<squarefeet;

}

//function
double CalcArea(double L, double W)
{
	double total;
	total =(L*W);
	return total;
}

void display()
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(4);

}

Open in new window

0
Comment
Question by:businessesatoz
  • 3
4 Comments
 

Author Comment

by:businessesatoz
Comment Utility
here is an example of what it should do

How many rooms? 5

Room #1

Enter length: 20

Enter width: 35

Room #2

Enter length: 18.7

Enter width: 25.3

Room #3

Enter length: 12.1

Enter width: 15

Room #4

Enter length: 28.6

Enter width: 22.2

Room #5

Enter length: 26

Enter width: 11.4

The total square feet is 2285.9300

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
In the loop, you have this line :

>>    CalcArea(length,width);

but you don't do anything with the result.

Keep a variable that contains the total area, and initialize it to 0 before the loop. Then, add the area of the current room for every room to that total. And after the loop, you can then print the total.
0
 

Author Comment

by:businessesatoz
Comment Utility
Thank you for your help.. i can't believe i did not notice that.. i feel so stupid
0
 

Author Closing Comment

by:businessesatoz
Comment Utility
THANKS!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now