A simple != problem

This is a discussion on A simple != problem within the C++ Programming forums, part of the General Programming Boards category; Code: //This program illustrates the use of logical operators #include <iostream> using namespace std; int main() { char year; float ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    A simple != problem

    Code:
     //This program illustrates the use of logical operators
    
    
    
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
    
    	char year;
    	float gpa;
    
    	cout << "What year student are you ? " << endl;
    	cout << "Enter 1 (freshman), 2 (Sophomore), 3 (junior), or 4 (senior)"
    		 << endl << endl;
    	cin >> year;
    
    	cout << "Now enter your GPA " << endl;
    	cin >> gpa;
    
    	if  (gpa >= 2.0 && year == '4')
    		cout << "It is time to graduate soon" << endl;
    	
    	else if (year != '4' || gpa <2.0)
    		cout << "You need more schooling" << endl;
    
    	return 0;
    
    }
    I am fairly new to C++, as you can probably tell from the difficulty, or lack of, of this program.

    I need to know how to make the line in bold work correctly using the NOT operator, I have exhausted all options I can think of and can't seem to get the program to work correctly.

    -Ryan

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Why do you need to use not? Doesn't it work just fine as is? You could do gpa!<=2.0 or what I just typed w/o the equals.

    Edit: You can also do something like !(gpa<=2.0) and !(!year=='4')
    There is all kinds of different ways to do it.
    Last edited by cerin; 02-09-2005 at 10:31 PM.
    My computer is awesome.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, if you want essentially the same test but using the ! operator somewhere, basically what you'd do is do the 'opposite' comparisons, and then stick it in () and tack a ! in front For example, the condition in the 'else if' is exactly the opposite of the condition in the 'if' block (note that these conditions are copied and pasted from your own code!):
    Code:
    (year != '4' || gpa <2.0)
    is equivalent to
    !(gpa >= 2.0 && year == '4')
    Aside from that, the condition in the 'else if' is unnecessary, since it basically covers all possibilities that the 'if' condition misses. Since this is the case, an 'else' will do the exact same thing as your 'else if' but more efficiently.
    Last edited by Hunter2; 02-09-2005 at 10:36 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Ya I should have been more clear, it works fine as is, my professor gave me the extra task of making the program work in the same fashion using the not operator in this line of code.

    I can get it to work using the != but when you enter 4 as the grade and enter a GPA under 2.0, it still says "It's time to graduate soon".

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, what exactly is the condition you tried? It's kind of hard to tell what's wrong with it unless you show us what it is Also, if you read between the lines in my earlier post, you'll find pretty much the solution to your question.

    *P.S. Welcome to the boards at CProg
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-17-2010, 12:00 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 08:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 06:33 PM

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