Thread: help using subfunctions with if/else

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    help using subfunctions with if/else

    I'm trying to write a program that calculates the area of a square or a triangle. The program should only recognize the entries 's' or 't' (for square / triangle) and must not recognize capital letters or any other symbol.
    I'm still in the very early stages of writing this and am confused.
    If I understand correctly int refers to an integer, but if I ask the user to enter 's' or 't' what would I use?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The reserved word char stands for character.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    This is what I have so far......


    [code]
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;

    void instructions(); //Instructions to the user.
    void calculations(); //Calculates the area of a triangle or square.

    int main ()
    {
    // Instructions for the user.
    instructions();

    // Calculates the area.
    calculations();

    return 0;
    }

    // Instructions.
    void instructions()
    {
    cout << "This program calculates the area of "
    << "either a square or a triangle. You will" << endl;
    cout << "first be asked to enter a letter "
    << "(s for square, or t for triangle) to identify" << endl;
    cout << "which kind of figure you want the area computed for. "
    << "Be sure to type a " << endl;
    cout << "lower-case letter, and not a capital letter. "
    << "Then you will be asked for " << endl;
    cout << "dimensions of the figure. Always hit the <Enter> "
    << "key after typing whatever " << endl;
    cout << "has been asked for." << endl << endl;

    } // End instructions.

    // Calculations.
    void calculations()

    {
    int squ1, tri1;
    float let1;
    char s, t, let1;

    cout << "Type the letter s to compute the area of a square, "
    << "or type t for a triangle: ";
    cin >> let1 >> endl;
    if (let1=s);
    cout << "Enter the length of a side for the square: "
    cin >> squ1;
    else
    if (let1=t);
    cout << "Enter the length of the base for the triangle: "
    cin >> tri1;
    else (let1 != s, t);
    cout << "This program can only recognize the "
    << "lower-case letters s and t. Sorry.: << endl;







    return;
    } // End calculations.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    cout << "Type the letter s to compute the area of a square, "
    << "or type t for a triangle: ";
    cin >> let1 >> endl;
    if (let1=='s');
    {
    cout << "Enter the length of a side for the square: "
    cin >> squ1;
    }
    else
    if (let1=='t');
    {
    cout << "Enter the length of the base for the triangle: "
    cin >> tri1;
    }
    else /*(let1 != s, t);*/
    {
    cout << "This program can only recognize the "
    << "lower-case letters s and t. Sorry.: << endl;
    }
    Make sure you use the == operator to compare two variables, otherwise you are assigning a variable and seeing if that variable is non-zero or not...use ' ' quotes when checking against a character, or set your char s and t to the character you want:
    Code:
    char s = 's';
    char t = 't';
    Also the last if statement wasn't necessary--you can simply have else {/*...*/ } to cover any other possibilities
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    I made some changes as you suggested....

    Code:
    	// Calculations.
    	void calculations()
    	
    {
    	int squ1, tri1;
    	float let1;  	
    	char s = 's';
    	char t = 't';
    
    	cout << "Type the letter s to compute the area of a square, "
    		 << "or type t for a triangle: ";
    	cin >> let1;
    	if (let1=='s');
    		cout << "Enter the length of a side for the square: ";
    		cin >> squ1;
    		else
    		if (let1=='t');
    		cout << "Enter the length of the base for the triangle: ";
    		cin >> tri1;
    			else /*(let1 != s, t);*/
    				cout << "This program can only recognize the "
    					 << "lower-case letters s and t.  Sorry.: << endl;
    
    	
    
    
    
    	
    	
    	return;
    }	// End calculations.
    but I'm getting the error message

    warning C4390: ';' : empty controlled statement found; is this the intent?
    for "Enter the length for the side of a square:'

  6. #6
    root
    Join Date
    Sep 2003
    Posts
    232
    >warning C4390: ';' : empty controlled statement found; is this the intent?
    No, it's not the intent, but it's certainly what you asked for:
    Code:
    if (let1=='s');
    if statements don't end with semicolons in C++.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Lol I knew i would miss something
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    it's funny when I wrote it out on paper I didn't put any semi-colons, but when I typed it into the program, I did!
    So much for paying attention.

    Now it's telling me

    error C2181: illegal else without matching if

    but it looks they way we did it in class

    else
    if (let1=='t')

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    okay, I got it to run with 0 errors & 0 warnings, but even if I enter a lower-case 's' or 't' it tells me I entered a non-choice.

    can anyone tell me why, please.

    Code:
    include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    void instructions();	//Instructions to the user.
    void calculations();	//Calculates the area of a triangle or square.
    
    int main ()
    {
    	// Instructions for the user.
    	instructions();
    
    	// Calculates the area.
    	calculations();
    
    	return 0;
    }
    
    // Instructions.
    	void instructions()
    {	
    	cout << "This program calculates the area of "
    		 << "either a square or a triangle.  You will" << endl;
    	cout << "first be asked to enter a letter " 
    		 << "(s for square, or t for triangle) to identify" << endl;
    	cout << "which kind of figure you want the area computed for.  "
    		 << "Be sure to type a " << endl;
    	cout << "lower-case letter, and not a capital letter.  "
    		 << "Then you will be asked for " << endl;
    	cout << "dimensions of the figure.  Always hit the <Enter> "
    		 << "key after typing whatever " << endl;
    	cout << "has been asked for." << endl << endl;
    
    }  // End instructions.
    
    
    
    
    	// Calculations.
    	void calculations()
    	
    {
    	int squ1, tri1;
    	float let1;  	
    	char s = 's';
    	char t = 't';
    
    	cout << "Type the letter s to compute the area of a square, "
    		 << "or type t for a triangle: ";
    	cin >> let1;
    	if (let1=='s'){
    		cout << "Enter the length of a side for the square: ";
    		cin >> squ1;}
    	else 
    		if (let1=='t'){
    		cout << "Enter the length of the base for the triangle: ";
    		cin >> tri1;}
    		else (let1 != s, t);{
    			cout << "This program can only recognize the "
    				 << "lower-case letters s and t.  Sorry." << endl;}
    
    	return;
    }	// End calculations.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    okay, I got it to run with 0 errors & 0 warnings, but even if I enter a lower-case 's' or 't' it tells me I entered a non-choice.

    can anyone tell me why, please.

    Code:
    include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    void instructions();	//Instructions to the user.
    void calculations();	//Calculates the area of a triangle or square.
    
    int main ()
    {
    	// Instructions for the user.
    	instructions();
    
    	// Calculates the area.
    	calculations();
    
    	return 0;
    }
    
    // Instructions.
    	void instructions()
    {	
    	cout << "This program calculates the area of "
    		 << "either a square or a triangle.  You will" << endl;
    	cout << "first be asked to enter a letter " 
    		 << "(s for square, or t for triangle) to identify" << endl;
    	cout << "which kind of figure you want the area computed for.  "
    		 << "Be sure to type a " << endl;
    	cout << "lower-case letter, and not a capital letter.  "
    		 << "Then you will be asked for " << endl;
    	cout << "dimensions of the figure.  Always hit the <Enter> "
    		 << "key after typing whatever " << endl;
    	cout << "has been asked for." << endl << endl;
    
    }  // End instructions.
    
    
    
    
    	// Calculations.
    	void calculations()
    	
    {
    	int squ1, tri1;
    	float let1;  	
    	char s = 's';
    	char t = 't';
    
    	cout << "Type the letter s to compute the area of a square, "
    		 << "or type t for a triangle: ";
    	cin >> let1;
    	if (let1=='s'){
    		cout << "Enter the length of a side for the square: ";
    		cin >> squ1;}
    	else 
    		if (let1=='t'){
    		cout << "Enter the length of the base for the triangle: ";
    		cin >> tri1;}
    		else (let1 != s, t);{
    			cout << "This program can only recognize the "
    				 << "lower-case letters s and t.  Sorry." << endl;}
    
    	return;
    }	// End calculations.

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Why is let1 a float? If you are expecting the user to input a character, you should declare let1 as a char. This is true especially considering you comapre let1 to 's' and 't', which are both chars.

    Also, JaWiB gave you two options, and it looks like you used both, but you only need one. So remove the char s = 's'; and char t = 't';

    Finally - congrats on going through your program a little at a time. Its refreshing to see that you are making this work first before attempting to implement the calculations.

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    I changed float let1; to char let1;
    but now it tells me 's' undeclared identifier and 't' undeclared identifier.

    I didn't think I'd be able to do the whole program at once, so I thought I'd try to get the easier part to work then add in the math after.

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Earlier you commented out the last "else if" and made it into an "else", but in your most recently posted code it looks like the comments went away. Change:
    Code:
    	else (let1 != s, t);{
    to:
    Code:
    	else {
    and see if that works.

    And it is almost always a great idea to go a small piece at a time and get it to compile and work, even if you think you can do the whole thing at once.

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Yes!!! That made it work.

    Now to move on to the next part.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. subfunctions returning different types together
    By muzihc in forum C Programming
    Replies: 38
    Last Post: 10-08-2008, 01:19 PM
  2. Issues with using CHAR with IF/ELSE conditions.
    By TheBlackVeil in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2007, 10:58 AM
  3. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM
  4. returning error on if/else in function
    By xion in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2004, 03:45 AM
  5. If/Else Problem
    By Vireyda in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 02:00 PM