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
4
Medium Priority
?
301 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
[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
  • Learn & ask questions
  • 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 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

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
Article by: evilrix
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…
Video by: Grant
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.

610 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