#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Problem with simple if statement at end of stucture project


    So I made a program with a structure that contains information about a job applicant, and just outputs it back to them at the end and asks them if its correct.

    The issue at hand is just the if statement at the end involving the character variable named answer aka 'y' or 'n'.

    Even if I put 'n' for the answer character's value, it still gives me the answer I placed inside the if statement for the 'y' case.

    Here's my code:

    Code:
    #include <iostream>
    #include <string>
    
    
    // Project Goal: Make a job database program that stores information about 3 candidates 
    // in terms of race, years of experience, highest level of education achieved 
    // (how fitting), annual income, marital status, and religion affiliation called 
    // PersonalStructure
    
    // Make it so the user inputs all the information for you, and have it print the entire
    // structure of the user as well if they want
    
    // Status: Works, but if I enter no, it still gives me the information under the yes
    // statement
    
    using namespace std;
    
    int main(){
    	char answer;
    
    struct personalStructure{
    
    	int yrsexp;
    
    	string lvledu;
    
    	int annualincome;
    
    	string race;
    
    	string maritalstatus;
    
    	string religiousaffiliation;
    
    };
    	personalStructure person;
    
    
    	cout << "Please enter your race or ethnicity: " << endl;
    	cin >> person.race;
    
    	cout << "Please enter your current marital status: " << endl;
    	cin >> person.maritalstatus;
    
    	cout << "Please enter your religious affiliation: " << endl;
    	cin >> person.religiousaffiliation;
    
    	cout << "Please enter the years you have been in the field: " << endl;
    	cin >> person.yrsexp;
    
    
    
    	cout << "Please enter your current level of education: " << endl;
    	cin >> person.lvledu;
    
    	cout << "Please enter your current annual income: " << endl;
    	cin >> person.annualincome;
    
    
    	cout << "________________________________________________________________________________";
    	cout << endl << endl;
    	cout << "Here is the current information we have for you on file: " << endl;
    	cout << "Race: " << person.race << endl;
    	cout << "Marital Status: " << person.maritalstatus << endl;
    	cout << "Religious Affiliation: " << person.religiousaffiliation << endl;
    	cout << "Years of Experience: " << person.yrsexp << endl;
    	cout << "Current Level of Education: " << person.lvledu << endl;
    	cout << "Annual Income: " << person.annualincome << endl;
    	cout << endl << endl;
    	cout << "________________________________________________________________________________";
    	
    	cout << endl << endl << endl;
    	cout << "Is this information correct (y or n)? " << endl;
    	cout << endl << endl;
    	cin >> answer;
    
    	if(answer = 'y'){
    
    		cout << endl << endl
    			 << "Thank you for applying for this position! \n"
    			 << "We will be contacting candidates within the next two weeks. \n"
    			 << "If you do not receive a response back, please note the immense amount of applications we receive daily and be patient for a response. \n"
    			 << endl << endl;
    
    	}
    	
    
    	else if(answer = 'n'){
    		cout << "Please go back to the beginning of the prompt to enter your information again " << endl;
    	}
    
    	system("PAUSE");
    	return 0;
    
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Examine the expressions highlighted in red:
    Code:
    	if(answer = 'y'){
    
    		cout << endl << endl
    			 << "Thank you for applying for this position! \n"
    			 << "We will be contacting candidates within the next two weeks. \n"
    			 << "If you do not receive a response back, please note the immense amount of applications we receive daily and be patient for a response. \n"
    			 << endl << endl;
    
    	}
    	
    
    	else if(answer = 'n'){
    		cout << "Please go back to the beginning of the prompt to enter your information again " << endl;
    	}
    When you turn all warnings on and up, what warnings do you get? Because your compiler certainly must have thrown warnings at you about those expressions in red. If you did not have warnings turned on and up, then you are a fool. If you did get warnings, then you are an even worse fool for having ignored them.

    Added By Edit:
    {
    Here is what I got when I compiled your code (MinGW gcc under WinXP):
    C:TEST>g++ -Wall haunt3.cpp
    haunt3.cpp: In function `int main()':
    haunt3.cpp:77: warning: suggest parentheses around assignment used as truth value
    haunt3.cpp:88: warning: suggest parentheses around assignment used as truth value

    C:TEST>
    The lines 77 and 88 pointed to contain the expressions that are highlighted in red above.

    With gcc, the -Wall option displays all warnings. That is an example of turning warnings on and up.
    }

    The assignment operator is "=", a single equal sign. The relational operator to test for equality is "==", two equal signs. They are not even remotely the same thing and they do most definitely mean something different.

    Every expression evaluates to a value, even an assignment expression. An assignment evaluates to the value that is assigned.

    In an if-statement, the condition is false if the expression evaluates to a zero and it is true if the expression evaluates to a non-zero.

    The value of a character is equal to its ASCII code (or whatever encoding you're using). The only character that has a zero value is the null-terminator, '\0'. The characters 'y' and 'n' both have non-zero values, 121 and 110 respectively.

    I will leave it for you to piece all those facts together.
    Last edited by dwise1_aol; August 30th, 2013 at 04:04 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Originally Posted by dwise1_aol
    Examine the expressions highlighted in red:
    Code:
    	if(answer = 'y'){
    
    		cout << endl << endl
    			 << "Thank you for applying for this position! \n"
    			 << "We will be contacting candidates within the next two weeks. \n"
    			 << "If you do not receive a response back, please note the immense amount of applications we receive daily and be patient for a response. \n"
    			 << endl << endl;
    
    	}
    	
    
    	else if(answer = 'n'){
    		cout << "Please go back to the beginning of the prompt to enter your information again " << endl;
    	}
    When you turn all warnings on and up, what warnings do you get? Because your compiler certainly must have thrown warnings at you about those expressions in red. If you did not have warnings turned on and up, then you are a fool. If you did get warnings, then you are an even worse fool for having ignored them.

    Added By Edit:
    {
    Here is what I got when I compiled your code (MinGW gcc under WinXP):

    The lines 77 and 88 pointed to contain the expressions that are highlighted in red above.

    With gcc, the -Wall option displays all warnings. That is an example of turning warnings on and up.
    }

    The assignment operator is "=", a single equal sign. The relational operator to test for equality is "==", two equal signs. They are not even remotely the same thing and they do most definitely mean something different.

    Every expression evaluates to a value, even an assignment expression. An assignment evaluates to the value that is assigned.

    In an if-statement, the condition is false if the expression evaluates to a zero and it is true if the expression evaluates to a non-zero.

    The value of a character is equal to its ASCII code (or whatever encoding you're using). The only character that has a zero value is the null-terminator, '\0'. The characters 'y' and 'n' both have non-zero values, 121 and 110 respectively.

    I will leave it for you to piece all those facts together.
    Oh wow, I forgot about the double equal signs for assignment statements involving if statements.

    Such a simple mistake, oh wow. That totally fixed it, thanks a bunch!
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Which is why I sound like a broken record as I keep on teaching the vital importance of warnings. The warnings immediately pointed us directly to the problem. If you had had warnings turned on and had heeded the warnings, you would have solved that problem in twelve seconds rather than have to wait for twelve hours. When you're working under a dead-line, 12 seconds instead of 12 hours can mean the difference between passing and failing.

    Always turn warnings on and up! Never ignore warnings.

IMN logo majestic logo threadwatch logo seochat tools logo