If statements :P

This is a discussion on If statements :P within the C++ Programming forums, part of the General Programming Boards category; haha... I'm having trouble figuring out this litte goof around program... haha Code: #include <iostream> using namespace std; int main() ...

  1. #1
    Novice Programmer Pyroteh's Avatar
    Join Date
    Jan 2005
    Location
    Alaska
    Posts
    82

    Question If statements :P

    haha... I'm having trouble figuring out this litte goof around program... haha

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
    	char sex;
    
    	cout << "Are you m or f (male or female)? ";
    	cin >> sex;
    	cin.ignore();
    	if ( sex == m ) {
    		cout << "GO AWAY!!!" << endl;
    	}
    	else if ( sex == f ) {
    		cout << "Hey Baby ;).... call me!! " << endl;
    	}
    	else {
    		cout << "Not sure? LOL!!! " << endl;
    	}
    	cin.get();
    }
    the errors are
    error C2065: 'm' : undeclared identifier

    error C2065: 'f' : undeclared identifier

    warning C4508: 'main' : function should return a value; 'void' return type assumed

    thanks for any help given

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Until you tell the compiler what m and f are, it has no way to know what you are trying to do with them.

    As a human, I understand you are trying to compare the variable sex to the letter m or the letter f, but you have to indicate that to the compiler. Right now it thinks you are trying to compare it to another variable, which you have also not defined yet.
    Code:
    sex == 'm'
    would be telling the compiler "compare the variable sex with the letter m"

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    if ( sex == m ) {
    		cout << "GO AWAY!!!" << endl;
    	}
    	else if ( sex == f ) {
    		cout << "Hey Baby ;).... call me!! " << endl;
    You are trying to compare the input (sex) from the user for the characters m and f. A single character is represented in C/C++ by a single character surrounded by single quotes. Like so...

    Code:
    if ( sex == 'm' ) {
    		cout << "GO AWAY!!!" << endl;
    	}
    	else if ( sex == 'f' ) {
    		cout << "Hey Baby ;).... call me!! " << endl;
    as Jessycat says since you have omitted the single quotes the compiler recognizes m and f as undeclared identifier literals. A common mistake is to confuse a character with a string and use double quotes to represent a character or single quotes for a string. Good programming practice would be to initialize each primitive variable when declared. Like so...

    Code:
    char sex = 'f';  // will change on user input
    char str[100] = "\0"; //initializes each element of array of char with null terminator
    string aStr = ""; // null string
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bigtamscot
    Code:
    char str[100] = "\0"; //initializes each element of array of char with null terminator
    Yes and no. What it actually does is put '\0' in the first character of the array, and since you haven't implicitly initialized the remaining elements of the array, it sets them all to zero. To clarify:
    Code:
    char array[100] = { 1 };
    This sets the first element to one, and all others are defaulted to zero. To further illustrate:
    Code:
    char array[100];
    Here, none of the elements are initialized. The only way you get the default initialization is because you're actually initializing the first (or more) elements manually. But again, it doesn't take whatever the first one is, and fill the rest with that value. It simply zero-fills the rest.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    try it out then This maybe compiler specific, but if I remember correctly, setting the first elements to null will set the remaining elements to null on BB5.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't need to try it out. I was telling you what it did, not asking what it did. It doesn't set them to "null". It sets them to zero.
    Code:
    int array[10] = { 1, 2 };
    This won't set them to "null". It sets the first two to one and two respectively, and all the remaining elements are set to zero. It isn't compiler specific. It is just that in C++ "null" happens to be zero. However, it should be noted that it does in fact set them to zero, and not "null", for correct terminology.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quzah: Have you actually tried that? I'm 98% positive that if an initializer list is too short, the remaining elements are set to the last given value:

    Code:
    //All set to 1
    int a[10] = { 1 };
    
    //Set to 1, 2, 3, 3, 3, 3 ... 3
    int a[10] = { 1, 2, 3 };
    No compiler right now, has to try later.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm 100% positive I'm right and you're not. But just to show you...
    Code:
    #include<iostream>
    int main()
    {
            int array[5] = { 1 };
            int x;
            for( x = 0; x < 5; x++ )
            {
                    std::cout << "array[" << x << "] is: " << array[x] << std::endl;
            }
            return 0;
    }
    
    /*
    g++ -o wrong wrong.cpp -Wall -ansi -pedantic
    ./wrong
    
    array[0] is: 1
    array[1] is: 0
    array[2] is: 0
    array[3] is: 0
    array[4] is: 0
    */
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Novice Programmer Pyroteh's Avatar
    Join Date
    Jan 2005
    Location
    Alaska
    Posts
    82
    haha thanks guys... alot of info for me there... I haven't even started studing arrays :P I was just goofin off with if statements to.... gain a little more experience... haha... the first reply set everything strait and worked my problem beautifuly. thx jessycat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 11:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 12:36 PM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21