GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jun-2008, 14:11
TehTruth TehTruth is offline
New Member
 
Join Date: Jun 2008
Posts: 4
TehTruth is on a distinguished road
Question

Something wrong...


I'm a beginner and I created a code to figure out the volume or area of a rectangular prism. It's supposed to ask you if you want area or volume, then ask you for its dimensions and give you what you told it to give you (area or volume)

However, mine gives both the area and the volume, no matter what I put at the beginning. Here's the code:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int CubeV (int length, int width, int height);
int CubeA (int length, int width, int height);

int main () 
{
	string whatYouWant;
	int length = 0;
	int width = 0;
	int height = 0;
	int answer = 0;
	
	cout << "Would you like the volume, or the area of your rectangular prism?\n";
	getline(cin,whatYouWant);	
	cout << "Enter the length of your prism\n";
	cin >> length;
	cout << "Enter the width of your prism\n";
	cin >> width;
	cout << "Enter the height of your prism\n";
	cin >> height;
	
	if (whatYouWant == "volume");
	{
		answer = CubeV (length, width, height);
		cout << "The volume of your cube is " << answer << endl;
	}

	if (whatYouWant == "area");
	{
		answer = CubeA (length, width, height);
		cout << "The area of your cube is " << answer << endl;
	}
	
	cout << "Hello, " << whatYouWant << endl;
		
    return 0;
}

int CubeV (int length, int width, int height)
{
	int v;
	v = length * width * height;
	return v;
}

int CubeA (int length, int width, int height)
{
	int a;
	a = height * ( 2 * (length + width)) + 2 * (length * width);
	return a;
}	


What's wrong? I think it's something to do with

if (whatYouWant == "volume")
  #2  
Old 10-Jun-2008, 15:06
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Something wrong...


You have a couple of semicolons that shouldn't be there.

Here:

CPP / C++ / C Code:
	if (whatYouWant == "volume");

and here

CPP / C++ / C Code:
	if (whatYouWant == "area");

When you put those semicolons at the ends of those lines, that marks the end of the conditionals, and the code below in the curly braces is always executed. Take them out, and it works:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int CubeV (int length, int width, int height);
int CubeA (int length, int width, int height);

int main () 
{
	string whatYouWant;
	int length = 0;
	int width = 0;
	int height = 0;
	int answer = 0;
	
	cout << "Would you like the volume, or the area of your rectangular prism?\n";
	getline(cin,whatYouWant);	
	cout << "Enter the length of your prism\n";
	cin >> length;
	cout << "Enter the width of your prism\n";
	cin >> width;
	cout << "Enter the height of your prism\n";
	cin >> height;
	
	if (whatYouWant == "volume")
	{
		answer = CubeV (length, width, height);
		cout << "The volume of your cube is " << answer << endl;
	}

	if (whatYouWant == "area")
	{
		answer = CubeA (length, width, height);
		cout << "The area of your cube is " << answer << endl;
	}
	
	cout << "Hello, " << whatYouWant << endl;
		
    return 0;
}

int CubeV (int length, int width, int height)
{
	int v;
	v = length * width * height;
	return v;
}

int CubeA (int length, int width, int height)
{
	int a;
	a = height * ( 2 * (length + width)) + 2 * (length * width);
	return a;
}	
__________________
www.blake-foster.com
  #3  
Old 10-Jun-2008, 15:23
TehTruth TehTruth is offline
New Member
 
Join Date: Jun 2008
Posts: 4
TehTruth is on a distinguished road

Re: Something wrong...


Thanks for the help, it works now
  #4  
Old 10-Jun-2008, 15:40
TehTruth TehTruth is offline
New Member
 
Join Date: Jun 2008
Posts: 4
TehTruth is on a distinguished road

Re: Something wrong...


I've got another problem now. I want it to say

Please type "v" or "a".

if the person doesn't type in v or a at the beginning, but it always says it, no matter what I type... It should go on to asking the dimensions of the cube if you type v or a but it doesn't. It's something to do with the != and ||

You only need the start of the code:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

float CubeV (float length, float width, float height);
float CubeA (float length, float width, float height);

int main () 
{
	string whatYouWant;
	float length = 0;
	float width = 0;
	float height = 0;
	float answer = 0;
	
	cout << "Would you like the volume, or the surface area of your rectangular prism? Please type \"v\" or \"a\".\n";
	getline(cin,whatYouWant);
	
	if ((whatYouWant != "v") || (whatYouWant != "a"))
		{
		cout << "Please type \"v\" or \"a\"." << endl;
		}
	else
		{
		cout << "Enter the length of your prism\n";
		cin >> length;
		cout << "Enter the width of your prism\n";
		cin >> width;
		cout << "Enter the height of your prism\n";
		cin >> height;
		
		if (whatYouWant == "v")
		{
			answer = CubeV (length, width, height);
			cout << "The volume of your cube is " << answer << endl;
		}

		else if (whatYouWant == "a")
		{
			answer = CubeA (length, width, height);
			cout << "The surface area of your cube is " << answer << endl;
		}
	}
    return 0;
}

float CubeV (float length, float width, float height)
{
	float v;
	v = length * width * height;
	return v;
}

float CubeA (float length, float width, float height)
{
	float a;
	a = height * ( 2 * (length + width)) + 2 * (length * width);
	return a;
}	
  #5  
Old 10-Jun-2008, 15:47
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Something wrong...


That OR should be an AND.

CPP / C++ / C Code:
if ((whatYouWant != "v") && (whatYouWant != "a"))
__________________
www.blake-foster.com
  #6  
Old 10-Jun-2008, 15:52
TehTruth TehTruth is offline
New Member
 
Join Date: Jun 2008
Posts: 4
TehTruth is on a distinguished road

Re: Something wrong...


Thanks a lot Blake!
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to read BMP files, header information is wrong / offset Algar C++ Forum 5 24-Feb-2008 03:00
String trim problem and wrong node routing agent! newbie06 Computer Software Forum - Linux 8 01-Mar-2007 23:51
Problem with if statement (was: what is wrong???) rubenryhan C Programming Language 2 02-May-2005 08:09
Functions and Classes - Where did I go wrong? redmage C++ Forum 5 10-Apr-2005 18:31
something wrong with this code loon MySQL / PHP Forum 5 07-Jul-2003 05:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:15.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.