Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How can i print my result correctly c++

Posted on 2011-09-11
Medium Priority
301 Views
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);
}

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

}
``````
0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

Author Comment

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

Infinity08 earned 2000 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

ID: 36518574
Thank you for your help.. i can't believe i did not notice that.. i feel so stupid
0

Author Closing Comment

ID: 36518576
THANKS!
0

## Featured Post

Question has a verified solution.

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

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data definâ€¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
###### Suggested Courses
Course of the Month7 days, 22 hours left to enroll