Solved

How can i print my result correctly c++

Posted on 2011-09-11
4
294 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
ID: 36518546
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
ID: 36518553
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
ID: 36518574
Thank you for your help.. i can't believe i did not notice that.. i feel so stupid
0
 

Author Closing Comment

by:businessesatoz
ID: 36518576
THANKS!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can i compile this github project?? 2 99
object oriented programming comparison 5 86
Autosar OS Multicore Share Resources confusion ? 2 108
No module found pypyodbc, 3 45
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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