Solved

How can i print my result correctly c++

Posted on 2011-09-11
4
291 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

822 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